I am using Apache POI library to use some formulas from the excel sheet but it seems that the formulas are not being updated at a runtime. If I save a workbook and reopen it, they are recalculated. Is there a way to calculate them at a runtime?
My code is:
FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(1);
datatypeSheet.getRow(19).getCell(2).setCellValue(0); // set C20=0
workbook.setForceFormulaRecalculation(true);
System.out.println(workbook.getSheetAt(1).getRow(19).getCell(8).getNumericCellValue()); // prints out the result
FileOutputStream out = new FileOutputStream(new File(FILE_NAME));
workbook.write(out);
out.close();
I also tried:
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.clearAllCachedResultValues();
evaluator.notifySetFormula(workbook.getSheetAt(1).getRow(19).getCell(8));
evaluator.evaluate(workbook.getSheetAt(1).getRow(19).getCell(8));
but they always return old value.