Excel Formula Not Updating Cell

孤者浪人 提交于 2019-12-05 00:57:57

If you're using XSSF workbooks, you can re-evaluate all formula cells as follows:

XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook)

A similar API exists if you're using an HSSF workbook:

HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook)

Or, depending on exactly how you're set up and what specifically you're trying to get accomplished, you should be able to find your solution here

In case your sheet has some formula for say A3=sum(A1:A2)is calculated and it is working fine with A1=5, A2 =4. Now if change the value of A2 with 2 like

sh.getRow(0).getCell(0).setCellValue(2); // set A1=2 and perform write operation, check the value of A3 it is still showing 9. It seems wrong but actully not. The point is that Excel caches previously calculated results and you need to trigger recalculation to updated them.

wb.getCreationHelper().createFormulaEvaluator().evaluateAll(); or with

wb.setForceFormulaRecalculation(true); and then perform Write operation. This will give you the correct result. For Detail check here

You have to explicitly trigger a recalculation. See the Recalculation of Formulas section here about why & how to do it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!