Example usage for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFCell getNumericCellValue.

Prototype

public double getNumericCellValue() 

Source Link

Document

Get the value of the cell as a number.

Usage

From source file:de.bund.bfr.knime.openkrise.db.imports.GeneralXLSImporter.java

License:Open Source License

private Boolean manageBoolean(PreparedStatement ps, PreparedStatement psUpdate, int lfdCol, HSSFCell cell)
        throws SQLException {
    Boolean result = null;/*from  www  . j  av  a 2  s. co  m*/
    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
        if (ps != null)
            ps.setNull(lfdCol, java.sql.Types.BOOLEAN);
        if (psUpdate != null)
            psUpdate.setNull(lfdCol, java.sql.Types.BOOLEAN);
    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
        result = cell.getNumericCellValue() != 0;
        if (ps != null)
            ps.setBoolean(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setBoolean(lfdCol, result);
    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
        result = cell.getStringCellValue().equalsIgnoreCase("true");
        if (ps != null)
            ps.setBoolean(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setBoolean(lfdCol, result);
    } else {
        result = cell.getBooleanCellValue();
        if (ps != null)
            ps.setBoolean(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setBoolean(lfdCol, result);
    }
    //ps.setNull(lfdCol, java.sql.Types.BOOLEAN);      
    return result;
}

From source file:de.ma.it.common.excel.ExcelFileManager.java

License:Open Source License

/**
 * /*from   www.  j  av a 2  s  .co m*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Boolean readCellAsBoolean(HSSFRow row, int cellIdx) throws IllegalArgumentException {
    HSSFCell cell = getCell(row, cellIdx);
    if (cell == null) {
        return null;
    }

    int cellType = cell.getCellType();
    // First evaluate formula if present
    if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
        cellType = evaluator.evaluateFormulaCell(cell);
    }

    Boolean result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_BOOLEAN:
        result = cell.getBooleanCellValue();
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        result = cell.getNumericCellValue() == 1;
        break;
    case HSSFCell.CELL_TYPE_STRING:
        String stringCellValue = cell.getStringCellValue();
        result = "1".equalsIgnoreCase(stringCellValue) || "true".equalsIgnoreCase(stringCellValue)
                || "yes".equalsIgnoreCase(stringCellValue);
        break;
    default:
        result = null;
        break;
    }

    return result;
}

From source file:de.ma.it.common.excel.ExcelFileManager.java

License:Open Source License

/**
 * /*from  www. j  a va  2 s .  c  o  m*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Double readCellAsDouble(HSSFRow row, int cellIdx) throws IllegalArgumentException {
    HSSFCell cell = getCell(row, cellIdx);
    if (cell == null) {
        return null;
    }

    int cellType = cell.getCellType();
    // First evaluate formula if present
    if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
        cellType = evaluator.evaluateFormulaCell(cell);
    }

    Double result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        result = Double.valueOf(numericCellValue);
        break;
    case HSSFCell.CELL_TYPE_STRING:
        String stringCellValue = cell.getStringCellValue();
        if (!StringUtils.isNumeric(stringCellValue)) {
            throw new IllegalArgumentException("Value " + stringCellValue + " is not numeric!");
        }
        result = Double.valueOf(stringCellValue);
        break;
    default:
        result = Double.MIN_VALUE;
        break;
    }

    return result;
}

From source file:de.ma.it.common.excel.ExcelFileManager.java

License:Open Source License

/**
 * //from w  w  w  . j a v  a  2s  .  co  m
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Float readCellAsFloat(HSSFRow row, int cellIdx) throws IllegalArgumentException {
    HSSFCell cell = getCell(row, cellIdx);
    if (cell == null) {
        return null;
    }

    int cellType = cell.getCellType();
    // First evaluate formula if present
    if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
        cellType = evaluator.evaluateFormulaCell(cell);
    }

    Float result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        if (numericCellValue > Float.MAX_VALUE) {
            throw new IllegalArgumentException("Value " + numericCellValue + " to big for integer!");
        }
        result = Double.valueOf(numericCellValue).floatValue();
        break;
    case HSSFCell.CELL_TYPE_STRING:
        String stringCellValue = cell.getStringCellValue();
        if (!StringUtils.isNumeric(stringCellValue)) {
            throw new IllegalArgumentException("Value " + stringCellValue + " is not numeric!");
        }
        result = Double.valueOf(stringCellValue).floatValue();
        break;
    default:
        result = Float.MIN_VALUE;
        break;
    }

    return result;
}

From source file:de.ma.it.common.excel.ExcelFileManager.java

License:Open Source License

/**
 * /*from  w  w  w.  ja v a2  s  . com*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Integer readCellAsInt(HSSFRow row, int cellIdx) throws IllegalArgumentException {
    HSSFCell cell = getCell(row, cellIdx);
    if (cell == null) {
        return null;
    }

    int cellType = cell.getCellType();
    // First evaluate formula if present
    if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
        cellType = evaluator.evaluateFormulaCell(cell);
    }

    Integer result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        if (numericCellValue > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("Value " + numericCellValue + " to big for integer!");
        }
        result = Double.valueOf(numericCellValue).intValue();
        break;
    case HSSFCell.CELL_TYPE_STRING:
        String stringCellValue = cell.getStringCellValue();
        if (!StringUtils.isNumeric(stringCellValue)) {
            throw new IllegalArgumentException("Value " + stringCellValue + " is not numeric!");
        }
        result = Double.valueOf(stringCellValue).intValue();
        break;
    default:
        result = Integer.MIN_VALUE;
        break;
    }

    return result;
}

From source file:de.ma.it.common.excel.ExcelFileManager.java

License:Open Source License

/**
 * /*from   w ww.j a v a2  s .c  om*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Long readCellAsLong(HSSFRow row, int cellIdx) throws IllegalArgumentException {
    HSSFCell cell = getCell(row, cellIdx);
    if (cell == null) {
        return null;
    }

    int cellType = cell.getCellType();
    // First evaluate formula if present
    if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
        cellType = evaluator.evaluateFormulaCell(cell);
    }

    Long result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        if (numericCellValue > Long.MAX_VALUE) {
            throw new IllegalArgumentException("Value " + numericCellValue + " to big for integer!");
        }
        result = Double.valueOf(numericCellValue).longValue();
        break;
    case HSSFCell.CELL_TYPE_STRING:
        String stringCellValue = cell.getStringCellValue();
        if (!StringUtils.isNumeric(stringCellValue)) {
            throw new IllegalArgumentException("Value " + stringCellValue + " is not numeric!");
        }
        result = Double.valueOf(stringCellValue).longValue();
        break;
    default:
        result = Long.MIN_VALUE;
        break;
    }

    return result;
}

From source file:de.viaboxx.nlstools.formats.MBExcelPersistencer.java

License:Apache License

private Object getValue(HSSFCell cell, CellType cellType) {
    switch (cellType) {
    case NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {// w  ww . jav a  2 s  .  c  om
            return cell.getNumericCellValue();
        }
    case FORMULA:
        return getValue(cell, cell.getCachedFormulaResultTypeEnum());
    case BOOLEAN:
        return cell.getBooleanCellValue();
    case STRING:
        return cell.getStringCellValue();
    case ERROR:
        return cell.getErrorCellValue();
    default:
        return null;
    // do not handle Formular, Error, Blank, ...
    }
}

From source file:eafit.cdei.asignacion.input.ReadCurrentCourses.java

public List<Teacher> loadCurrentOffering() throws Exception {

    try {//from   w w w.ja v  a  2s .c o  m
        FileInputStream fileInputStream = new FileInputStream("current_classes.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("currentOffering");

        boolean keepDoing = true;
        for (int i = 0; i < worksheet.getLastRowNum() + 1; i++) {

            HSSFRow row1 = worksheet.getRow(i);

            HSSFCell cellF1 = row1.getCell((short) 5);
            String f1Val = ((int) cellF1.getNumericCellValue()) + "";

            HSSFCell cellW1 = row1.getCell((short) 22);
            String w1Val = cellW1.getStringCellValue();

            HSSFCell cellX1 = row1.getCell((short) 23);
            String x1Val = cellX1.getStringCellValue();

            HSSFCell cellY1 = row1.getCell((short) 24);
            String y1Val = cellY1.getStringCellValue();

            HSSFCell cellAU1 = row1.getCell((short) 46);
            String au1Val = ((int) cellAU1.getNumericCellValue()) + "";

            HSSFCell cellAW1 = row1.getCell((short) 48);
            String aw1Val = cellAW1.getStringCellValue();

            if (f1Val == null || f1Val.length() == 0 || f1Val.equals("0")) {
                keepDoing = false;
            }

            Course c = new Course();

            c.setHourStart(f1Val);
            c.setHourEnd(au1Val);
            c.setDowList(generateDaysList(aw1Val));
            c.setNameCourse(x1Val);
            c.setCodeCourse(w1Val);
            c.setCodeCourse(y1Val);

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HHmm");
            if (au1Val.length() == 3) {
                au1Val = "0" + au1Val;
            }

            if (f1Val.length() == 3) {
                f1Val = "0" + f1Val;
            }

            c.setHourEndLocal(LocalTime.parse(au1Val, dtf));
            c.setHourStartLocal(LocalTime.parse(f1Val, dtf));

            c.setDateStartLocal(LocalTime.parse(au1Val, dtf));
            c.setDateEndLocal(LocalTime.parse(aw1Val, dtf));

            System.out.println(c);

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:eafit.cdei.asignacion.input.ReadCurrentOffering.java

public List<Teacher> loadCurrentOffering() throws Exception {

    try {/*from  w  w  w .  j  a  va 2 s. c  o m*/
        FileInputStream fileInputStream = new FileInputStream("currentOffering.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("currentOffering");

        boolean keepDoing = true;
        for (int i = 0; i < worksheet.getLastRowNum() + 1; i++) {

            HSSFRow row1 = worksheet.getRow(i);

            HSSFCell cellF1 = row1.getCell((short) 5);
            String f1Val = ((int) cellF1.getNumericCellValue()) + "";

            HSSFCell cellW1 = row1.getCell((short) 22);
            String w1Val = cellW1.getStringCellValue();

            HSSFCell cellX1 = row1.getCell((short) 23);
            String x1Val = cellX1.getStringCellValue();

            HSSFCell cellY1 = row1.getCell((short) 24);
            String y1Val = cellY1.getStringCellValue();

            HSSFCell cellAU1 = row1.getCell((short) 46);
            String au1Val = ((int) cellAU1.getNumericCellValue()) + "";

            HSSFCell cellAW1 = row1.getCell((short) 48);
            String aw1Val = cellAW1.getStringCellValue();

            HSSFCell cellAB = row1.getCell((short) 27);
            String abVal = cellAB.getStringCellValue();

            HSSFCell cellAC = row1.getCell((short) 28);
            String acVal = cellAC.getStringCellValue();

            if (f1Val == null || f1Val.length() == 0 || f1Val.equals("0")) {
                keepDoing = false;
            }

            Course c = new Course();

            c.setHourStart(f1Val);
            c.setHourEnd(au1Val);
            c.setDowList(generateDaysList(aw1Val));
            c.setNameCourse(x1Val);
            c.setCodeCourse(w1Val);
            c.setCodeCourse(y1Val);

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HHmm");
            if (au1Val.length() == 3) {
                au1Val = "0" + au1Val;
            }

            if (f1Val.length() == 3) {
                f1Val = "0" + f1Val;
            }

            c.setHourEndLocal(LocalTime.parse(au1Val, dtf));
            c.setHourStartLocal(LocalTime.parse(f1Val, dtf));

            DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyyMMdd");

            //c.setDateStart(LocalDate.parse(abVal,dtf1));
            //c.setHourStartLocal(LocalTime.parse(f1Val,dtf));

            System.out.println(c);

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:eafit.cdei.asignacion.input.ReadTeacherPreferences.java

public List<Teacher> loadTeacherPreferences() throws Exception {

    try {//from   w w w  .j av a 2 s  .c o m
        FileInputStream fileInputStream = new FileInputStream("TeacherPreferences.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("Preferences");

        boolean keepDoing = true;
        for (int i = 1; i < worksheet.getLastRowNum() + 1; i++) {

            HSSFRow row1 = worksheet.getRow(i);
            HSSFCell cellA1 = row1.getCell((short) 0);
            String a1Val = cellA1.getNumericCellValue() + "";
            HSSFCell cellB1 = row1.getCell((short) 1);
            String b1Val = cellB1.getStringCellValue();
            HSSFCell cellC1 = row1.getCell((short) 2);
            String c1Val = cellC1.getStringCellValue();

            String d1Val = "";

            HSSFCell cellD1 = row1.getCell((short) 3);

            if (cellD1 != null) {
                d1Val = cellD1.getStringCellValue();
            }

            HSSFCell cellE1 = row1.getCell((short) 4);
            double e1Val = cellE1.getNumericCellValue();

            if (a1Val == null || a1Val.length() == 0 || a1Val.equals("0")) {
                keepDoing = false;
            }

            Teacher t = new Teacher();
            t.setFullName(b1Val);

            t.setMaxCourses((int) e1Val);

            String[] preferedCourses = c1Val.split(",");

            ArrayList<LevelCourse> preferedCoursed = new ArrayList<LevelCourse>();

            for (String preferedCoursed1 : preferedCourses) {
                LevelCourse lc = new LevelCourse();
                lc.setLevelName(preferedCoursed1);
                preferedCoursed.add(lc);
            }

            String[] preferedTimes = d1Val.split(",");
            ArrayList<String> preferedTime = new ArrayList<>();

            for (String pf : preferedTimes) {

                preferedTime.add(pf);
            }

            t.setListPreferedCourses(preferedCoursed);

            System.out.println(t);

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}