java excel apache-poi notimplementedexception

PDI de Java: ¿cómo leer el valor de celda de Excel y no la fórmula que lo computa?



apache-poi notimplementedexception (3)

Hay un comando alternativo donde puede obtener el valor bruto de una celda donde se coloca la fórmula. Su tipo de devolución es String. Utilizar:

cell.getRawValue();

Estoy usando API de POI de Apache para obtener valores de un archivo de Excel. Todo funciona bien excepto con celdas que contienen fórmulas. De hecho, cell.getStringCellValue() devuelve la fórmula utilizada en la celda y no el valor de la celda.

Intenté utilizar el método evaluateFormulaCell() pero no funciona porque estoy usando la fórmula Excel de GETPIVOTDATA y esta fórmula no está implementada en la API:

Exception in thread "main" org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell Landscape!K11 at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:321) at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:288) at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:221) at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCellValue(HSSFFormulaEvaluator.java:320) at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCell(HSSFFormulaEvaluator.java:213) at fromExcelToJava.ExcelSheetReader.unAutreTest(ExcelSheetReader.java:193) at fromExcelToJava.ExcelSheetReader.main(ExcelSheetReader.java:224) Caused by: org.apache.poi.ss.formula.eval.NotImplementedException: GETPIVOTDATA at org.apache.poi.hssf.record.formula.functions.NotImplementedFunction.evaluate(NotImplementedFunction.java:42)


Las soluciones publicadas anteriormente no me funcionaron. cell.getRawValue() devolvió la misma fórmula que se indica en la celda. La siguiente función funcionó para mí:

public void readFormula() throws IOException { FileInputStream fis = new FileInputStream("Path of your file"); Workbook wb = new XSSFWorkbook(fis); Sheet sheet = wb.getSheetAt(0); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); CellReference cellReference = new CellReference("C2"); // pass the cell which contains the formula Row row = sheet.getRow(cellReference.getRow()); Cell cell = row.getCell(cellReference.getCol()); CellValue cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: System.out.println(cellValue.getBooleanValue()); break; case Cell.CELL_TYPE_NUMERIC: System.out.println(cellValue.getNumberValue()); break; case Cell.CELL_TYPE_STRING: System.out.println(cellValue.getStringValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_ERROR: break; // CELL_TYPE_FORMULA will never happen case Cell.CELL_TYPE_FORMULA: break; } }


Para las celdas de fórmula, Excel almacena dos cosas. Uno es la Fórmula misma, el otro es el valor "en caché" (el último valor que se evaluó como forumla)

Si desea obtener el último valor en caché (que puede que ya no sea correcto, pero siempre y cuando Excel haya guardado el archivo y no lo haya modificado), querrá algo como:

for(Cell cell : row) { if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { System.out.println("Formula is " + cell.getCellFormula()); switch(cell.getCachedFormulaResultType()) { case Cell.CELL_TYPE_NUMERIC: System.out.println("Last evaluated as: " + cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: System.out.println("Last evaluated as /"" + cell.getRichStringCellValue() + "/""); break; } } }