Workaround for using Apache POI to handle Excel formulas that are not updated

Source: Internet
Author: User

When you use a POI to update Excel, if cell a sets a formula, and when the other cells it depends on are populated with values, a in the exported Excel is still a formula instead of an auto-calculated value.


Paste_image.png

The score subtotal is not updated to the calculated results.

Workaround

This interface is provided in the POI to org.apache.poi.ss.usermodel.FormulaEvaluator enable updates to the formula. Where HSSFFormulaEvaluator XSSFFormulaEvaluator this interface is implemented.

Implementation ideas

We can iterate through all the cells in a row of cells, and if they are of type Cell.CELL_TYPE_FORMULA , prove that the cell is a formula that you can use to evaluator.evaluateFormulaCell(cell); update it. The sample code is as follows:

PrivateStaticvoid updateformula (Workbook WB, Sheet s,int row) {row r=s.getrow (row); Cell c=null; Formulaecaluator eval=null; if (WB instanceof Hssfworkbook) eval= new hssfformulaevaluator ((hssfworkbook) WB); else if (WB instanceof Xssfworkbook) Eval=new xssfformulaevaluator ((xssfworkbook) WB); for (int i=r.getfirstcellnum (); I<r.getlastcellnum (); i++ ) {C=r.getcell (i); if (C.getcelltype () ==cell.cell_type_formula) Eval.evaluateformulacell (c);}}   

If you think this article to you to help me, please me to eat a candy bar ~??



Devid
Links: https://www.jianshu.com/p/5d5265cafa06
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.

Workaround for using Apache POI to handle Excel formulas that are not updated

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.