简体   繁体   English

Excel公式未更新单元格

[英]Excel Formula Not Updating Cell

I am using apache POI to change cells in an excel sheet. 我正在使用apache POI来更改Excel工作表中的单元格。 After I change the values, the cells with formulas corresponding to cells that have been changed are not updating. 更改值后,具有与已更改的单元格对应的公式的单元格不会更新。

When I go into excel and click on the cell with the formula, and then click in the function bar, the formula updates. 当我进入excel并单击带有公式的单元格,然后单击功能栏时,公式将更新。

I have calculation options set to automatically update. 我有计算选项设置为自动更新。

Quick example. 快速举例。

Row : 行:

[A2 + A3] [1] [2] [A2 + A3] [1] [2]

A1 here would equal 3 A1在这里等于3

When I change it using POI: 当我使用POI更改它时:

[A2+A3] [2] [5] [A2 + A3] [2] [5]

A1 still equals 3 until I click on that cell. 在我点击那个单元格之前,A1仍然等于3。

Refreshing the workbook or sheet also does not work. 刷新工作簿或工作表也不起作用。 Is this a problem with excel or POI? 这是excel还是POI的问题? Can anyone think of a workaround? 谁能想到一个解决方法?

If you're using XSSF workbooks, you can re-evaluate all formula cells as follows: 如果您正在使用XSSF工作簿,则可以按如下方式重新评估所有公式单元格:

XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook)

A similar API exists if you're using an HSSF workbook: 如果您使用HSSF工作簿,则存在类似的API:

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. 如果你的工作表有一些公式,比如说A3=sum(A1:A2)被计算出来并且工作正常, A1=5, A2 =4. Now if change the value of A2 with 2 like 现在如果用2改变A2的值就好了

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. //设置A1 = 2并执行写入操作,检查A3的值是否仍显示9.看似错误但实际上没有。 The point is that Excel caches previously calculated results and you need to trigger recalculation to updated them. 关键是Excel缓存以前计算的结果,您需要触发重新计算以更新它们。

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. 请参阅此处重新计算公式部分了解为何以及如何执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当前值/单元格POI的Excel公式 - Excel formula on the current value/cell POI Apache POI读取Excel单元格的公式值 - Apache POI reading formula value of excel cell 使用Apache POI在Excel中将公式单元格转换为错误单元格 - Formula Cell converting to Error Cell in excel using Apache POI 如何检查Excel单元格文本是否包含Excel公式 - How to check excel cell text contain excel formula or not 如何从java中的excel单元格中提取公式来识别cmd函数? - How to extract formula from excel cell in java to identify cmd functions? 如何使用Apache POI在Excel中添加公式(单元格的动态值) - How Add a formula(Dynamic value of cell) in Excel Using Apache POI Java POI:如何读取Excel单元格的值而不是公式计算呢? - Java POI : How to read Excel cell value and not the formula computing it? JExcel:在MS Excel中具有公式ROUNDUP的单元格上失去单元精度 - JExcel : Losing Cell Precison On Cells Having Formula ROUNDUP in MS Excel 使用Fillo在Excel中更新单元格时,getiing错误为“ NullPointerException” - When updating Cell in Excel using Fillo , getiing error as “NullPointerException” 将Google Spreadsheet API与JAVA结合使用时,引用中的公式单元格值未更新 - Formula Cell value is not updating in the reference while Using Google Spreadsheet API with JAVA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM