POI/Excel/HTML cell formula problem

1. Problem description

Use MyBatis to get data from the database, and then use POI to populate the data into the Excel template to generate the final xls file. Convert the final xls file to an html file and return it to the foreground to display in the Panel.

In the Excel template, in addition to the data point placeholders, there are also some calculation formulas. Since the data referenced by these calculation formulas are dots in the template, the calculation unit is displayed as "#VALUE!". See below:

clip_image001

After generating an Excel file, after adding the relevant code for recalculation (see below), the value of the calculated cell can be displayed normally. After converting to html, these calculated cells do not recalculate and still display "#VALUE!".

 

2. Problem Analysis

This problem has been around for about two weeks, with some time spent researching on and off, but the problem has not been resolved.

This kind of problem caused by the use of third-party components is more difficult to troubleshoot. Searched a lot on the Internet and found no description of similar problems. There is some reference to recalculating Excel cell formulas using POI.

Downloaded the source code related to POI 3.11, set up a test environment on this machine, and carried out trace debugging. It is found that in the ExcelToHtmlConverter class, when obtaining the CachedFormulaResultType of the cell containing the formula, Cell.CELL_TYPE_ERROR is always returned, so that the method can only obtain the value of the cell (that is, #VALUE!) through cell.getErrorCellValue() and return it.

clip_image002

Looking at the relevant source code, POI sets attributes such as _record to private, which cannot be modified when called. I searched on the Internet again, but there is no information of reference value.

 

3. Problem solving

When opening and closing the xls file generated after filling the data with Excel, Excel always prompts whether to save, but does not actually make any changes. If you "save" this template and generate html again, the cell formula will be calculated and displayed normally. I always feel that the xls file generated by POI still has compatibility problems, but MS did not develop the format and description of related files before Office 2007, so I can't blame POI.

After generating Excel, recalculate the formula cells:

clip_image003

[Only setForceFormulaRecalculation(Boolean.TRUE) is invalid when converting to html]

In the formal project and the initial test code, fetching data to generate Excel and converting to html is written in a function. It didn't feel too convenient in testing. So I split the generation of Excel and the conversion into html into two functions, so as to control whether to regenerate Excel. Unexpectedly, after splitting and running the test code, the calculation unit in html has value! Simply!

Then the code in the formal project was also split, and this maddening problem was solved in this nonsensical way.

 

Fourth, the problem summary

There is a problem with generating xls files and converting them to html in the same method, but there is no problem after splitting them into two methods. This phenomenon is rather strange, and there should be a problem at the code level. I won't delve into it for the time being.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326778945&siteId=291194637