Apache POI评估公式

发布于 2024-11-02 03:51:18 字数 2511 浏览 1 评论 0原文

我的工作表单元格中有一些公式,我想在插入一些值后评估它们。例如:

我的公式是 =SUM(B1,B2)

在值插入之前 B1 值为 1B2值为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

注定孤独终老 2024-11-09 03:51:18

为了将评论提升为答案...

您首先需要确保主 jar 和 OOXML 部分使用相同版本的 POI。您的 Maven 代码片段显示其中一个为 3.7,另一个为 3.8 beta 1。您需要确保它们都是相同的。 (您甚至可能想使用刚刚发布的 3.8-beta2)。

然后,使用:

FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);

或:

XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);

请参阅 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:

FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
evaluator.evaluateFormulaCell(cell);

or:

XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);

See http://poi.apache.org/spreadsheet/eval.html for more details

我不会写诗 2024-11-09 03:51:18

也许 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.

天生の放荡 2024-11-09 03:51:18

如果您使用 HSSF 尝试:

HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);

if you using HSSF try :

HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
念三年u 2024-11-09 03:51:18

你可以用这个。

 public static void triggerFormula(HSSFWorkbook workbook){      

                FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                HSSFSheet sheet = workbook.getSheetAt(0);
                int lastRowNo=sheet.getLastRowNum();        

                for(int rownum=0;rownum<=lastRowNo;rownum++){
                Row row;
                 if (sheet.getRow(rownum)!=null){
                         row= sheet.getRow(rownum);

                      int lastCellNo=row.getLastCellNum();

                          for(int cellnum=0;cellnum<lastCellNo;cellnum++){  
                                  Cell cell;
                                  if(row.getCell(cellnum)!=null){
                                     cell = row.getCell(cellnum);   
                                    if(Cell.CELL_TYPE_FORMULA==cell.getCellType()){
                                    evaluator.evaluateFormulaCell(cell);
                                }
                            }
                         }
                 }
                }


            }

You can use this.

 public static void triggerFormula(HSSFWorkbook workbook){      

                FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                HSSFSheet sheet = workbook.getSheetAt(0);
                int lastRowNo=sheet.getLastRowNum();        

                for(int rownum=0;rownum<=lastRowNo;rownum++){
                Row row;
                 if (sheet.getRow(rownum)!=null){
                         row= sheet.getRow(rownum);

                      int lastCellNo=row.getLastCellNum();

                          for(int cellnum=0;cellnum<lastCellNo;cellnum++){  
                                  Cell cell;
                                  if(row.getCell(cellnum)!=null){
                                     cell = row.getCell(cellnum);   
                                    if(Cell.CELL_TYPE_FORMULA==cell.getCellType()){
                                    evaluator.evaluateFormulaCell(cell);
                                }
                            }
                         }
                 }
                }


            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文