使用Apache poi

时间:2015-07-14 07:39:41

标签: java apache-poi

我正在尝试阅读Excel 2007工作表。读取Excel工作表的问题在于其所有单元格都具有公式类型的值。

单元格的值如下:

=' C:!\ \ ** \ [*** XLSX]的 *' G23

当我尝试使用公式评估阅读它时,它给出了以下错误:

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

工作表具有正确的值,但因为它们是公式类型,当我们尝试评估它时,它会给出公式中引用的excel的错误。

我尝试了以下链接中给出的所有可能方式。 Formula Evaluation Apache但它不起作用。

请帮忙。

2 个答案:

答案 0 :(得分:1)

我今天有同样的错误。因为由于某些数据丢失问题,我从旧文件创建了新的excel文件并更改了名称。当我在新的excel文件中评估公式时,它使用旧名称给出了此错误。下一行解决了如下问题:

wb.setForceFormulaRecalculation(true)

希望它将对某人有所帮助。

答案 1 :(得分:1)

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