简体   繁体   English

使用 Apache poi 在 excel 中进行公式评估

[英]Formula evaluation in excel using Apache poi

I am trying to read from the Excel 2007 worksheet.我正在尝试从 Excel 2007 工作表中读取。 The problem in reading the excel sheet is that all its cell has values in the type of formula.读取 Excel 工作表的问题在于,它的所有单元格都具有公式类型的值。

The values of the cell are like:单元格的值是这样的:

='C:\\ \\**\\[***.xlsx] *'!G23 ='C:\\ \\**\\[***.xlsx] *'!G23

When I try to read them using formula evaluation it gives me the below error:当我尝试使用公式评估读取它们时,它给了我以下错误:

Exception in thread "main" java.lang.RuntimeException: Could not resolve external workbook name '/**/**.xlsx'. Workbook environment has not been set up.
at org.apache.poi.ss.formula.OperationEvaluationContext.createExternSheetRefEvaluator(OperationEvaluationContext.java:109)
at org.apache.poi.ss.formula.OperationEvaluationContext.createExternSheetRefEvaluator(OperationEvaluationContext.java:84)
at org.apache.poi.ss.formula.OperationEvaluationContext.getRef3DEval(OperationEvaluationContext.java:309)
at org.apache.poi.ss.formula.WorkbookEvaluator.getEvalForPtg(WorkbookEvaluator.java:634)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:505)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:263)
at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:205)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateFormulaCellValue(XSSFFormulaEvaluator.java:267)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateFormulaCell(XSSFFormulaEvaluator.java:154)
at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateAllFormulaCells(HSSFFormulaEvaluator.java:346)
at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateAllFormulaCells(HSSFFormulaEvaluator.java:337)
at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateAllFormulaCells(XSSFFormulaEvaluator.java:241)
at com.kiodex.ExcelWorkbook2007.parse(ExcelWorkbook2007.java:26)
at ExcelTest.main(ExcelTest.java:24)
    Caused by: org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment$WorkbookNotFoundException: Could not resolve external workbook name '/**/**.xlsx'. Workbook environment has not been set up.
at org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.getWorkbookEvaluator(CollaboratingWorkbooksEnvironment.java:194)
at org.apache.poi.ss.formula.WorkbookEvaluator.getOtherWorkbookEvaluator(WorkbookEvaluator.java:156)
at org.apache.poi.ss.formula.OperationEvaluationContext.createExternSheetRefEvaluator(OperationEvaluationContext.java:107)
... 13 more

The sheet has proper values but as they are of formula type, when we try to evaluate it, it gives the error for the excel which is referenced in the formula.该工作表具有正确的值,但由于它们属于公式类型,当我们尝试对其进行评估时,它会给出公式中引用的 excel 的错误。

I tried doing all possible ways given in the below link.我尝试了以下链接中给出的所有可能的方法。 Formula Evaluation Apache but it does not work.公式评估 Apache但它不起作用。

Please help.请帮忙。

I have had same error today.我今天有同样的错误。 Because due to some data loss issues I created new excel file from old one and changed its name.因为由于一些数据丢失问题,我从旧文件创建了新的 excel 文件并更改了名称。 When I evaluate formulas in new excel file,it has given this error with its old name.Following line solved problem like a charm:当我在新的 excel 文件中计算公式时,它用旧名称给出了这个错误。以下行解决了问题,就像一个魅力:

wb.setForceFormulaRecalculation(true)

Hope it will help someone.希望它会帮助某人。

var bcd = sheet1.Workbook.GetCreationHelper().CreateFormulaEvaluator();
bcd.IgnoreMissingWorkbooks = true;
bcd.EvaluateAll();

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

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