Apache的POI评估公式 [英] Apache POI evaluate formula
问题描述
我在我的表细胞的一些公式,我想后,我插入一些值来评价他们。例如:
我的公式是 = SUM(B1,B2)
值插入之前 B1
值 1
和 B2
值 3
,和公式的结果是 4
值插入后现在B1具有价值 5
,和B2具有价值 2
但配方仍然会产生 4
,我怎么能评估/触发这个计算?
7
计算,有没有触发方式本无需进行人工干预?自然
我使用的是2007年Excel,以便 XSSFWorkbook
编辑/ UPDATE:
我用Gabors的解决方案,他发布之前,但我使用它作为一个参考,这里是发生了什么:
异常线程mainjava.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
在org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator&LT;&初始化GT;(XSSFFormulaEvaluator.java:64)
在org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator&LT;&初始化GT;(XSSFFormulaEvaluator.java:51)
...............
...............
下面是相关code的一部分:
公共XSSFFormulaEvaluator getEvaluator(){
如果(评估== NULL){
评估=新XSSFFormulaEvaluator(WB);
}
返回评估;
}
实际调用计算器:
//其中指数为int和了myCell为int
行=(XSSFRow)sheet.getRow(指数);
电池= row.createCell(了myCell);
。getEvaluator()evaluateFormulaCell(手机);
我要找的人用这个,是成功的,而不是那些谁谷歌的解决方案并没有真正尝试它,我一直在google搜索了很多说,至少有。
每Gagravar建议,我也有我的类路径中的POI 2:
&LT;&依赖性GT;
&LT;&的groupId GT; org.apache.poi&LT; /的groupId&GT;
&LT;&的artifactId GT; POI&LT; / artifactId的&GT;
&LT;&版GT; 3.8-β1&LT; /版本&GT;
&LT; /依赖性&GT;
&LT;&依赖性GT;
&LT;&的groupId GT; org.apache.poi&LT; /的groupId&GT;
&LT;&的artifactId GT; POI-OOXML&LT; / artifactId的&GT;
&LT;&版GT; 3.7&LT; /版本&GT;
&LT; /依赖性&GT;
不过,我觉得我需要3.7版本XSSF工作簿等。
解决方案:
&LT;&依赖性GT;
&LT;&的groupId GT; org.apache.poi&LT; /的groupId&GT;
&LT;&的artifactId GT; POI&LT; / artifactId的&GT;
&LT;&版GT; 3.8-β2&LT; /版本&GT;
&LT; /依赖性&GT;
&LT;&依赖性GT;
&LT;&的groupId GT; org.apache.poi&LT; /的groupId&GT;
&LT;&的artifactId GT; POI-OOXML&LT; / artifactId的&GT;
&LT;&版GT; 3.8-β2&LT; /版本&GT;
&LT; /依赖性&GT;
要推动评论一个答案......
您首先需要确保你使用POI同一版本的主罐和OOXML的部分。 Maven的片段是显示3.7之一,而对于其他3.8 Beta 1中。你需要确保它们都是一样的。 (你甚至可能希望使用3.8-β2这是刚出来)。
然后,使用:
FormulaEvaluator评估= wb.getCreationHelper()createFormulaEvaluator()。
evaluator.evaluateFormulaCell(细胞);
或
XSSFFormulaEvaluator.evaluateAllFormulaCells(WB);
请参阅 http://poi.apache.org/s$p$padsheet/eval.html 更多详情
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>
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:
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);
or:
XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
See http://poi.apache.org/spreadsheet/eval.html for more details
这篇关于Apache的POI评估公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!