Example usage for org.apache.poi.ss.usermodel CellValue getBooleanValue

List of usage examples for org.apache.poi.ss.usermodel CellValue getBooleanValue

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel CellValue getBooleanValue.

Prototype

public boolean getBooleanValue() 

Source Link

Usage

From source file:com.phucdk.emailsender.utils.ExcelUtils.java

public static String getCellValueAsString(int row, int column, XSSFWorkbook myWorkBook) {
    XSSFSheet mySheet = myWorkBook.getSheetAt(1);
    Cell cell = getCell(row, column, mySheet);
    String strCellValue = "";
    FormulaEvaluator evaluator = myWorkBook.getCreationHelper().createFormulaEvaluator();
    if (cell != null) {
        CellValue cellValue = null;
        try {//from  w w  w . j  a v  a  2s .  c  om
            cellValue = evaluator.evaluate(cell);
        } catch (Exception ex) {
            log.error("Error when evaluate cell value", ex);
        }

        if (cellValue != null) {
            switch (cellValue.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                strCellValue = String.valueOf(cellValue.getNumberValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                strCellValue = String.valueOf(cellValue.getBooleanValue());
                break;
            case Cell.CELL_TYPE_STRING:
                strCellValue = String.valueOf(cellValue.getStringValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                //strCellValue = String.valueOf(cellValue.get());
                break;
            }
        }

    }
    return strCellValue;
}

From source file:com.phucdk.emailsender.utils.ExcelUtils.java

public static Object getCellValue(int row, int column, XSSFWorkbook myWorkBook) {
    XSSFSheet mySheet = myWorkBook.getSheetAt(1);
    Cell cell = getCell(row, column, mySheet);
    Object cellValueObject = "";
    FormulaEvaluator evaluator = myWorkBook.getCreationHelper().createFormulaEvaluator();
    if (cell != null) {
        CellValue cellValue = null;
        try {/*from   ww w .  j a va2s.co m*/
            cellValue = evaluator.evaluate(cell);
        } catch (Exception ex) {
            log.error("Error when evaluate cell value", ex);
        }

        if (cellValue != null) {
            switch (cellValue.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                cellValueObject = cellValue.getNumberValue();
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cellValueObject = cellValue.getBooleanValue();
                break;
            case Cell.CELL_TYPE_STRING:
                cellValueObject = cellValue.getStringValue();
                break;
            case Cell.CELL_TYPE_FORMULA:
                //strCellValue = cellValue.getErrorValue();
                break;
            }
        }

    }
    return cellValueObject;
}

From source file:csv.impl.ExcelReader.java

License:Open Source License

/**
 * Returns the evaluated cell content.// w  w  w  .ja v  a 2  s .  c om
 * This assumes the cell contains a formula.
 * @param cell cell to evaluate
 * @return cell value
 */
public Object evaluateCellValue(Cell cell) {
    FormulaEvaluator evaluator = getFormulaEvaluator();
    CellValue value = evaluator.evaluate(cell);
    switch (value.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return value.getStringValue();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return DateUtil.getJavaDate(value.getNumberValue());
        } else {
            return value.getNumberValue();
        }
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return value.getBooleanValue();
    case Cell.CELL_TYPE_ERROR:
        return value.getErrorValue();
    default:
        System.out.println("type=" + cell.getCellType());
    }
    return cell.getCellFormula();
}

From source file:de.bund.bfr.knime.pmm.common.XLSReader.java

License:Open Source License

private String getData(Cell cell) {
    if (cell != null) {
        if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            CellValue value = evaluator.evaluate(cell);

            switch (value.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                return value.getBooleanValue() + "";
            case Cell.CELL_TYPE_NUMERIC:
                return value.getNumberValue() + "";
            case Cell.CELL_TYPE_STRING:
                return value.getStringValue();
            default:
                return "";
            }//  w ww .  j  av  a2 s  . c  om
        } else {
            return cell.toString().trim();
        }
    }

    return null;
}

From source file:de.bund.bfr.knime.pmmlite.io.XlsReader.java

License:Open Source License

private String getData(Cell cell) {
    if (cell == null) {
        return null;
    }/* w  ww  . j  a  v a 2  s  .c  om*/

    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
        CellValue value = wb.getCreationHelper().createFormulaEvaluator().evaluate(cell);

        switch (value.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            return String.valueOf(value.getBooleanValue());
        case Cell.CELL_TYPE_NUMERIC:
            return String.valueOf(value.getNumberValue());
        case Cell.CELL_TYPE_STRING:
            return Strings.emptyToNull(Strings.nullToEmpty(value.getStringValue()).trim());
        default:
            return null;
        }
    } else {
        return Strings.emptyToNull(cell.toString().trim());
    }
}

From source file:de.escnet.ExcelTable.java

License:Open Source License

private String getCellValue(XSSFCell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return "";

    case Cell.CELL_TYPE_BOOLEAN:
        return Boolean.toString(cell.getBooleanCellValue());

    case Cell.CELL_TYPE_ERROR:
        return cell.getErrorCellString();

    case Cell.CELL_TYPE_FORMULA:
        CellValue cellValue = evaluator.evaluate(cell);
        switch (cellValue.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return "";

        case Cell.CELL_TYPE_BOOLEAN:
            return Boolean.toString(cellValue.getBooleanValue());

        case Cell.CELL_TYPE_NUMERIC:
            return getNumericValue(cellValue.getNumberValue());

        case Cell.CELL_TYPE_STRING:
            return cellValue.getStringValue();

        case Cell.CELL_TYPE_ERROR:
        case Cell.CELL_TYPE_FORMULA:
        default:/*from   w  w  w  .j a  v  a2  s . c  o  m*/
            throw new IllegalStateException("Illegal cell type: " + cell.getCellType());
        }

    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue().toString();
        }

        return getNumericValue(cell.getNumericCellValue());

    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();

    default:
        throw new IllegalStateException("Illegal cell type: " + cell.getCellType());
    }
}

From source file:eu.esdihumboldt.hale.app.bgis.ade.common.AbstractAnalyseTable.java

License:Open Source License

/**
 * Extract the text from a given cell. Formulas are evaluated, for blank or
 * error cells <code>null</code> is returned
 * //from w w  w  .j  a  va  2s  .co m
 * @param cell the cell
 * @return the cell text
 */
protected String extractText(Cell cell) {
    if (cell == null)
        return null;

    if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        // do this check here as the evaluator seems to return null on a
        // blank
        return null;
    }

    CellValue value = evaluator.evaluate(cell);

    switch (value.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(value.getBooleanValue());
    case Cell.CELL_TYPE_NUMERIC:
        // number formatting
        double number = value.getNumberValue();
        if (number == Math.floor(number)) {
            // it's an integer
            return String.valueOf((int) number);
        }
        return String.valueOf(value.getNumberValue());
    case Cell.CELL_TYPE_STRING:
        return value.getStringValue();
    case Cell.CELL_TYPE_FORMULA:
        // will not happen as we used the evaluator
    case Cell.CELL_TYPE_ERROR:
        // fall through
    default:
        return null;
    }
}

From source file:eu.esdihumboldt.hale.io.xls.XLSUtil.java

License:Open Source License

/**
 * Extract the text from a given cell. Formulas are evaluated, for blank or
 * error cells <code>null</code> is returned
 * /*www  .  j  a va 2s .c o m*/
 * @param cell the cell
 * @param evaluator the formula evaluator
 * @return the cell text
 */
public static String extractText(Cell cell, FormulaEvaluator evaluator) {
    if (cell == null)
        return null;

    if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        // do this check here as the evaluator seems to return null on a
        // blank
        return null;
    }

    CellValue value = evaluator.evaluate(cell);

    switch (value.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(value.getBooleanValue());
    case Cell.CELL_TYPE_NUMERIC:
        // number formatting
        double number = value.getNumberValue();
        if (number == Math.floor(number)) {
            // it's an integer
            return String.valueOf((int) number);
        }
        return String.valueOf(value.getNumberValue());
    case Cell.CELL_TYPE_STRING:
        return value.getStringValue();
    case Cell.CELL_TYPE_FORMULA:
        // will not happen as we used the evaluator
    case Cell.CELL_TYPE_ERROR:
        // fall through
    default:
        return null;
    }
}

From source file:excel.Reader.java

public void print() {
    System.out.println("START PRINT");
    SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");
    int columnWidth = 15;
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    Sheet sheet = wb.getSheetAt(0);//from w  w w .  j  av  a 2  s.c o  m
    for (Row row : sheet) {
        //System.out.print("r");
        for (Cell cell : row) {
            //CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
            //System.out.print(cellRef.formatAsString());
            //System.out.print(" - ");
            // System.out.print("c");
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                //System.out.print("s");
                System.out.printf("%-" + columnWidth + "s", cell.getRichStringCellValue().getString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                //System.out.print("d");
                if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.printf("%-" + columnWidth + "s", df.format(cell.getDateCellValue()));
                } else {
                    if ((cell.getNumericCellValue() % 1.0) != 0.0)
                        System.out.printf("%-" + columnWidth + ".2f", cell.getNumericCellValue());
                    else
                        System.out.printf("%-" + columnWidth + ".0f", cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                //System.out.print("b");
                System.out.printf("%-" + columnWidth + "s", cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                CellValue val = evaluator.evaluate(cell);
                //System.out.print("f");
                switch (val.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.printf("%-" + columnWidth + "s", val.getStringValue());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.printf("%-" + columnWidth + ".2f", val.getNumberValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.printf("%-" + columnWidth + "s", val.getBooleanValue());
                    break;
                default:
                    System.out.printf("%-" + columnWidth + "s", "");
                }
                break;
            default:
                System.out.print("");
            }
        }
        System.out.println();
    }
}

From source file:fyp.POI.POI.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream(new File("FA_AAX.xlsx"));
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sheet = wb.getSheetAt(0);/*  w  w w  . j a v a 2 s  .c o  m*/
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

    CellReference cr = new CellReference("C4");
    Row row = sheet.getRow(cr.getRow());
    Cell cell = row.getCell(cr.getCol());
    CellValue cellValue = evaluator.evaluate(cell);

    switch (cellValue.getCellType()) {
    case Cell.CELL_TYPE_BOOLEAN:
        boolean truefalse = cellValue.getBooleanValue();
        System.out.println("Boolean" + truefalse);
        break;
    case Cell.CELL_TYPE_NUMERIC:
        double NumericCheck = cellValue.getNumberValue();
        if (NumericCheck % 1 == 0) {
            int Integer = (int) NumericCheck;
            System.out.println("Integer" + Integer);
        } else {
            double Dbl = NumericCheck;
            System.out.println("Double" + Dbl);
        }
        break;

    case Cell.CELL_TYPE_STRING:
        String str = cellValue.getStringValue();
        System.out.println("String" + str.substring(3));
        break;

    case Cell.CELL_TYPE_BLANK:
        System.out.println("Blank");
        break;

    case Cell.CELL_TYPE_ERROR:
        break;
    }
    fis.close();
}