简体   繁体   English

SXSSF工作簿中的Apache POI评估公式

[英]Apache POI Evaluate Formula in SXSSF workbook

In my project I use the SXSSFWorkbook (Apache-POI 3.9) class to manage a large spreadsheet. 在我的项目中,我使用SXSSFWorkbook (Apache-POI 3.9)类来管理大型电子表格。 Now I need to evaluate formulas for some cells, so I tried with a FormulaEvaluator like this 现在我需要评估某些单元格的公式,所以我尝试了像这样的FormulaEvaluator

...
SXSSFWorkbook streamingWorkbook = new SXSSFWorkbook(100);
...
FormulaEvaluator fe = streamingWorkbook.getCreationHelper().createFormulaEvaluator();
...
fe.evaluateInCell(cell);

but doing so, an exception is thrown 但是这样做会引发异常

java.lang.ClassCastException: org.apache.poi.xssf.streaming.SXSSFCell cannot be cast to org.apache.poi.xssf.usermodel.XSSFCell
at     org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateInCell(XSSFFormulaEvaluator.java:177)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateInCell(XSSFFormulaEvaluator.java:44)

... ...

The direct cause of this error is clear: the method .evaluateInCell takes a Cell object, but internally casts the Cell into a XSSFCell . 此错误的直接原因很明显:方法.evaluateInCell接受一个Cell对象,但在内部将Cell强制转换为XSSFCell Since I'm passing instead an SXSSFCell the exception is thrown. 由于我改为传递SXSSFCell ,因此引发了异常。

So the question is: Is there a way to implement formula evaluation in streaming workbooks (SXSSF)? 因此问题是:是否有一种方法可以在流工作簿(SXSSF)中实现公式评估?

TL;DR - The support you need isn't in the older 3.9 version that you're using, so you'll need to upgrade to 3.13 beta 2 or later. TL; DR-您需要的支持不在您使用的旧版3.9中,因此您需要升级到3.13 beta 2或更高版本。

Your problem is that you're using too old a version of Apache POI. 您的问题是您使用的Apache POI版本太旧。 That's why you're getting exceptions when you try to evaluate SXSSF cells. 因此,当您尝试评估SXSSF单元时会出现异常。 As detailed towards the end of the Formula Evaluation documentation , for SXSSF formula evaluation, you need to be using 3.13 beta 2 or later. 公式评估文档末尾所述 ,对于SXSSF公式评估,您需要使用3.13 beta 2或更高版本。

One thing to be aware of though - Formula Evaluation needs not only the cell with the formula in to be in memory, but also any other cells it refers to, and any they refer to. 但是要注意的一件事-公式评估不仅需要将包含公式的单元存储在内存中,而且还需要该单元引用的任何其他单元以及它们引用的任何单元。 As such, a call to FormulaEvaluator.evaluateAll() is unlikely to work; 因此,对FormulaEvaluator.evaluateAll()的调用不太可能起作用。 you'll normally need to evaluate cells one-by-one just after writing. 您通常需要在编写后立即对单元格进行一次评估。 You'll also struggle with formulas which refer all over the place, as they'll only work if the cells they refer to are in the current window, and haven't been flushed to disk. 您还会遇到遍及整个地方的公式,因为它们仅在它们所引用的单元格在当前窗口中且尚未刷新到磁盘的情况下才起作用。

Just try to add the below dependency looks like you are missing other jars for XSSF 只需尝试添加以下依赖项,就像您缺少XSSF的其他jar一样

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>????</version>
</dependency>

the bellow image showing the problem(in SXSSF you can't evaluate the formula) 出现问题的波纹管图像(在SXSSF中,您无法评估公式) 在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM