I am doing project which reads a .xlsx file. In that .xlsx file there are some formula cells which I am trying to read but it throws Error 15. Below is the print.. The actual value of the cell is $92,345.16. but that is not getting printed. But if i read non formula cells it works for me. The issue is only with the formula cells. Below is the code snippet. Please some one help me to solve the issue. thanks
Just for info. This .xlsx file is available in one of our server which I am reading. But if I create my own manual .xlsx file by putting some formula and then reading it, it works for me. I am not sure why this happens only for production .xlsx file.
Formula Cell: SUM(G21:G2629) Evaluated Cell Type: ERROR Formula Evaluation Error: 15
XSSFSheet sheet = workbook.getSheetAt(1);
int desiredRowNum = 5; // Replace with the desired row number (0-based index)
int desiredCellNum = 10; // Replace with the desired cell number (0-based index)
Cell cell = sheet.getRow(desiredRowNum).getCell(desiredCellNum);
if (cell != null && cell.getCellType() == CellType.FORMULA) {
System.out.println("Formula Cell: " + cell.getCellFormula());
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
CellValue cellValue = evaluator.evaluate(cell);
System.out.println("Evaluated Cell Type: " + cellValue.getCellType());
if (cellValue.getCellType() == CellType.NUMERIC) {
double numericValue = cellValue.getNumberValue();
System.out.println("Evaluated Numeric Value: " + numericValue);
} else if (cellValue.getCellType() == CellType.STRING) {
String stringValue = cellValue.getStringValue();
System.out.println("Evaluated String Value: " + stringValue);
} else if (cellValue.getCellType() == CellType.ERROR) {
System.out.println("Formula Evaluation Error: " + cellValue.getErrorValue());
if (cellValue.getErrorValue() == FormulaError.DIV0.getCode()) {
System.out.println("Division by Zero Error Detected");
}
}
}