Search in sources :

Example 56 with FormulaEvaluator

use of org.apache.poi.ss.usermodel.FormulaEvaluator in project drools by kiegroup.

the class ExcelParser method processSheet.

private void processSheet(Sheet sheet, List<? extends DataListener> listeners) {
    int maxRows = sheet.getLastRowNum();
    CellRangeAddress[] mergedRanges = getMergedCells(sheet);
    DataFormatter formatter = new DataFormatter(Locale.ENGLISH);
    FormulaEvaluator formulaEvaluator = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator();
    for (int i = 0; i <= maxRows; i++) {
        Row row = sheet.getRow(i);
        int lastCellNum = row != null ? row.getLastCellNum() : 0;
        newRow(listeners, i, lastCellNum);
        for (int cellNum = 0; cellNum < lastCellNum; cellNum++) {
            Cell cell = row.getCell(cellNum);
            if (cell == null) {
                continue;
            }
            double num = 0;
            CellRangeAddress merged = getRangeIfMerged(cell, mergedRanges);
            int mergedColStart = DataListener.NON_MERGED;
            if (merged != null) {
                cell = sheet.getRow(merged.getFirstRow()).getCell(merged.getFirstColumn());
                mergedColStart = cell.getColumnIndex();
            }
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    newCell(listeners, i, cellNum, cell.getBooleanCellValue() ? "true" : "false", mergedColStart);
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    String cellValue = null;
                    try {
                        newCell(listeners, i, cellNum, getFormulaValue(formatter, formulaEvaluator, cell), mergedColStart);
                    } catch (RuntimeException e) {
                        // This is thrown if an external link cannot be resolved, so try the cached value
                        log.warn("Cannot resolve externally linked value: " + formatter.formatCellValue(cell));
                        String cachedValue = tryToReadCachedValue(cell);
                        newCell(listeners, i, cellNum, cachedValue, mergedColStart);
                    }
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (isNumericDisabled(listeners)) {
                    // don't get a double value. rely on DataFormatter
                    } else {
                        num = cell.getNumericCellValue();
                    }
                default:
                    if (num - Math.round(num) != 0) {
                        newCell(listeners, i, cellNum, String.valueOf(num), mergedColStart);
                    } else {
                        newCell(listeners, i, cellNum, formatter.formatCellValue(cell), mergedColStart);
                    }
            }
        }
    }
    finishSheet(listeners);
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator)

Aggregations

FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)56 Cell (org.apache.poi.ss.usermodel.Cell)40 Test (org.junit.Test)34 Workbook (org.apache.poi.ss.usermodel.Workbook)30 Sheet (org.apache.poi.ss.usermodel.Sheet)28 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)20 Row (org.apache.poi.ss.usermodel.Row)14 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)12 CellValue (org.apache.poi.ss.usermodel.CellValue)11 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)8 CellReference (org.apache.poi.ss.util.CellReference)7 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)6 BaseTestFormulaEvaluator (org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)3 DecimalFormat (java.text.DecimalFormat)2 ArrayList (java.util.ArrayList)2 InternalSheet (org.apache.poi.hssf.model.InternalSheet)2 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)2