Apache POI评估公式
我的工作表单元格中有一些公式,我想在插入一些值后评估它们。例如:
我的公式是 =SUM(B1,B2)
在值插入之前 B1
值为 1
和 B2
值为 3
,公式结果为 4
插入值后,现在 B1 的值为 5
,B2 的值为 2< /code> 但公式仍然产生
4
,怎么办我可以评估/触发这个计算吗?
当然,在我点击公式单元格上的返回按钮后,会计算新值 7
,有没有办法在无需手动交互的情况下触发此操作?
我使用的是 excel 2007,所以 XSSFWorkbook
编辑/更新:
在他发布之前我已经使用了 Gabors 解决方案,但我将其用作参考,这就是发生的情况:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.ss.formula.WorkbookEvaluator.<init>(Lorg/apache/poi/ss/formula/EvaluationWorkbook;Lorg/apache/poi/ss/formula/IStabilityClassifier;Lorg/apache/poi/hssf/record/formula/udf/UDFFinder;)V
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.<init>(XSSFFormulaEvaluator.java:64)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.<init>(XSSFFormulaEvaluator.java:51)
...............
...............
这是相关代码的一部分:
public XSSFFormulaEvaluator getEvaluator(){
if(evaluator == null){
evaluator = new XSSFFormulaEvaluator(wb);
}
return evaluator;
}
实际调用评估器:
//where index is int, and mycell is int
row = (XSSFRow) sheet.getRow(index);
cell = row.createCell(mycell);
getEvaluator().evaluateFormulaCell(cell);
我正在寻找使用此方法并取得成功的人,而不是那些没有真正尝试过的谷歌解决方案的人,我一直在谷歌上搜索很多至少说。
根据 Gagravar 的建议,我的类路径上确实有 2 个 POI:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.7</version>
</dependency>
虽然我需要 3.7 版本的 XSSF 工作簿等。
解决方案:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8-beta2</version>
</dependency>
I have some formulas in cells of my sheet, and I want to evaluate them after I insert some values. Ex :
My formula is =SUM(B1,B2)
Before values insertion B1
value was 1
, and B2
value was 3
, and the formula result is 4
After insertion of values now B1 has value 5
, and B2 has value 2
but the formula still produces 4
, how can I evaluate/trigger this to be calculated?
Naturally after I hit the return button on the formula cell the new value 7
is calculated, is there a way to trigger this without manual interaction?
I'm using excel 2007 so XSSFWorkbook
EDIT/UPDATE :
I've used Gabors solution before he posted it but I'm using it as a reference, here is what happens :
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.ss.formula.WorkbookEvaluator.<init>(Lorg/apache/poi/ss/formula/EvaluationWorkbook;Lorg/apache/poi/ss/formula/IStabilityClassifier;Lorg/apache/poi/hssf/record/formula/udf/UDFFinder;)V
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.<init>(XSSFFormulaEvaluator.java:64)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.<init>(XSSFFormulaEvaluator.java:51)
...............
...............
Here is a part of the relevant code :
public XSSFFormulaEvaluator getEvaluator(){
if(evaluator == null){
evaluator = new XSSFFormulaEvaluator(wb);
}
return evaluator;
}
actually invoking evaluator :
//where index is int, and mycell is int
row = (XSSFRow) sheet.getRow(index);
cell = row.createCell(mycell);
getEvaluator().evaluateFormulaCell(cell);
I'm looking for someone used this and was successful, not those who google solution without really trying it, I've been googling a lot to say at least.
Per Gagravar suggestion I do have 2 POIs on my classpath :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8-beta1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.7</version>
</dependency>
I though I need 3.7 version for XSSF workbooks etc.
SOLUTION :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.8-beta2</version>
</dependency>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了将评论提升为答案...
您首先需要确保主 jar 和 OOXML 部分使用相同版本的 POI。您的 Maven 代码片段显示其中一个为 3.7,另一个为 3.8 beta 1。您需要确保它们都是相同的。 (您甚至可能想使用刚刚发布的 3.8-beta2)。
然后,使用:
或:
请参阅 http://poi.apache.org/spreadsheet/eval.html更多细节
To promote a comment to an answer...
You firstly need to ensure you're using the same version of POI for the main jar, and the OOXML part. Your maven snippet was showing 3.7 for one, and 3.8 beta 1 for the other. You need to make sure they're both the same. (You might even want to use 3.8-beta2 which is just out).
Then, use either:
or:
See http://poi.apache.org/spreadsheet/eval.html for more details
也许 XSSFFormulaEvaluator
.evaluateAllFormulaCells(XSSFWorkbook wb)
?(或更具体地说:
evaluateFormulaCell(Cell cell)
。)如果不起作用:您使用的是哪个版本的 POI?从 v3.5 开始支持 XSSF 中的公式。
另外,尝试使用 XSSFCreationHelper 实例化您的POI 文档建议的公式评估器。
Perhaps XSSFFormulaEvaluator
.evaluateAllFormulaCells(XSSFWorkbook wb)
?(or more specifically:
evaluateFormulaCell(Cell cell)
.)If it doesn't work: what version of POI are you using? Formulas in XSSF are supported from v3.5.
Also, try to use XSSFCreationHelper to instantiate your Formula Evaluator as suggested by the POI docs.
如果您使用 HSSF 尝试:
if you using HSSF try :
你可以用这个。
You can use this.