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);
}
Aggregations