Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Apache POI HSSF, how can I refresh all formula cells at once?

I am filling cells of an Excel file using Apache POI, and there are a lot of formula cells in the document. However, their values are not refreshed when I open the document in Excel.

It's my understanding that I need to use a FormulaEvaluator to refresh formula cells. Is there a way, though, to update all formula cells at once? There are a lot of them, and while making an exhaustive list is not out of question, it's certainly not something I'm very willing to do.

like image 382
zneak Avatar asked May 09 '11 13:05

zneak


People also ask

What is the difference between HSSF & XSSF workbook?

HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (. xlsx) file format. HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets.


2 Answers

Sure. Refreshing all the formulas in a workbook is possibly the more typical use case anyway.

If you're using HSSF, call evaluatorAllFormulaCells:

 HSSFFormulaEvaluator.evaluateAllFormulaCells(hssfWorkbook) 

If you're using XSSF, call evaluatorAllFormulaCells:

 XSSFFormulaEvaluator.evaluateAllFormulaCells(xssfWorkbook) 

More details are available on the poi website

like image 115
Gagravarr Avatar answered Sep 21 '22 14:09

Gagravarr


wb.setForceFormulaRecalculation(true); // replace "wb" with your HSSFWorkbook/XSSFWorkbook object 

https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html#setForceFormulaRecalculation-boolean-

https://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFWorkbook.html#setForceFormulaRecalculation-boolean-

like image 32
Vebby Avatar answered Sep 23 '22 14:09

Vebby