您的位置:首页 > 其它

POI:Excel计算公式的再计算

2011-08-30 22:02 447 查看
前面在用POI操作Excel的时候,对于Excel的公式再计算使用的是
setForceFormulaRecalculation(boolean value)


Control if Excel should be asked to recalculate all formulas on this sheet when the workbook is opened.

正常情况下都能够正常计算。

但是如果Excel模板的计算方式是手动的时候,貌似就不灵性了。

无奈之下查询POI的API,实际上有两个方式实现公式再计算。

1. Re-evaluate formuals with POI's FormulaEvaluator:

Workbook wb = WorkbookFactory.create(new FileInputStream("workbook.xls"));

Sheet sh = wb.getSheetAt(0);
sh.getRow(0).getCell(0).setCellValue(2);  // set A1=2

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

2. Delegate re-calculation to Excel. The application will perform a full recalculation when the workbook is opened:

Workbook wb = WorkbookFactory.create(new FileInputStream("workbook.xls"));

Sheet sh = wb.getSheetAt(0);
sh.getRow(0).getCell(0).setCellValue(2);  // set A1=2

wb.setForceFormulaRecalculation(true);

即使Excel模板的计算方式是手动的时候,也能够实现公式再计算。这是因为FormulaEvaluator是事先计算,而setForceFormulaRecalculation是打开Excel的时候计算。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: