Example usage for org.apache.poi.ss.usermodel FormulaError getString

List of usage examples for org.apache.poi.ss.usermodel FormulaError getString

Introduction

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

Prototype

public String getString() 

Source Link

Usage

From source file:blanco.commons.calc.parser.AbstractBlancoCalcParser.java

License:Open Source License

public static String getCellValue(Cell cell) {
    // 2016.01.20 j.amano
    // ?jxl to poi ?????
    //------------------------
    //??:\-1,000//  w ww .j a  va2 s  . c o  m
    //jxl:($1,000)?$?????????
    //poi:-1000
    //------------------------
    //??:2016/1/20
    //jxl:0020, 1 20, 2016
    //poi:2016/01/20 00:00:00
    //------------------------
    //??:#REF!???
    //jxl:#REF!
    //poi:#REF!
    //------------------------
    //??:1,000
    //jxl:" "1,000
    //poi:-1000
    //------------------------

    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BLANK:
            return "";
        case Cell.CELL_TYPE_STRING:
            return cell.getRichStringCellValue().getString();
        case Cell.CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue());
        case Cell.CELL_TYPE_NUMERIC:
            // ??
            if (DateUtil.isCellDateFormatted(cell)) {
                // ????
                Date dt = cell.getDateCellValue();
                // ????
                DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                String sDate = df.format(dt);
                return sDate;
            }
            // ???.0
            DecimalFormat format = new DecimalFormat("0.#");
            return format.format(cell.getNumericCellValue());
        case Cell.CELL_TYPE_FORMULA:
            Workbook wb = cell.getSheet().getWorkbook();
            CreationHelper crateHelper = wb.getCreationHelper();
            FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
            return getCellValue(evaluator.evaluateInCell(cell));
        case Cell.CELL_TYPE_ERROR:
            byte errorCode = cell.getErrorCellValue();
            FormulaError error = FormulaError.forInt(errorCode);
            String errorText = error.getString();
            return errorText;
        default:
            return "";
        }
    }
    return "";
}

From source file:edu.si.sidora.excel2tabular.TabularCellTest.java

License:Apache License

@Test
public void testErrorValuedCell() {
    for (final FormulaError error : FormulaError.values()) {
        when(errorCell.getCellType()).thenReturn(CELL_TYPE_ERROR);
        when(errorCell.getErrorCellValue()).thenReturn(error.getCode());
        assertEquals(error.getString(), new TabularCell(errorCell).toString());
    }/*www .ja  va2 s .c  om*/
}

From source file:org.apache.metamodel.excel.ExcelUtils.java

License:Apache License

public static String getCellValue(Workbook wb, Cell cell) {
    if (cell == null) {
        return null;
    }//w  w w  .  j a  v a 2  s  .c  om

    final String cellCoordinate = "(" + cell.getRowIndex() + "," + cell.getColumnIndex() + ")";

    final String result;

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        result = null;
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        result = Boolean.toString(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        String errorResult;
        try {
            byte errorCode = cell.getErrorCellValue();
            FormulaError formulaError = FormulaError.forInt(errorCode);
            errorResult = formulaError.getString();
        } catch (RuntimeException e) {
            logger.debug("Getting error code for {} failed!: {}", cellCoordinate, e.getMessage());
            if (cell instanceof XSSFCell) {
                // hack to get error string, which is available
                String value = ((XSSFCell) cell).getErrorCellString();
                errorResult = value;
            } else {
                logger.error("Couldn't handle unexpected error scenario in cell: " + cellCoordinate, e);
                throw e;
            }
        }
        result = errorResult;
        break;
    case Cell.CELL_TYPE_FORMULA:
        // result = cell.getCellFormula();
        result = getFormulaCellValue(wb, cell);
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            Date date = cell.getDateCellValue();
            if (date == null) {
                result = null;
            } else {
                result = DateUtils.createDateFormat().format(date);
            }
        } else {
            // TODO: Consider not formatting it, but simple using
            // Double.toString(...)
            result = _numberFormat.format(cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_STRING:
        result = cell.getRichStringCellValue().getString();
        break;
    default:
        throw new IllegalStateException("Unknown cell type: " + cell.getCellType());
    }

    logger.debug("cell {} resolved to value: {}", cellCoordinate, result);

    return result;
}