Deepak Kumar
Deepak Kumar

Reputation: 863

Formula evaluation in excel using Apache poi

I am trying to read from the Excel 2007 worksheet. The problem in reading the excel sheet is that all its cell has values in the type of formula.

The values of the cell are like:

='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.

I tried doing all possible ways given in the below link. Formula Evaluation Apache but it does not work.

Please help.

Upvotes: 4

Views: 4296

Answers (2)

OldDriver
OldDriver

Reputation: 11

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

Upvotes: 1

Vusala Hasanli
Vusala Hasanli

Reputation: 408

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. When I evaluate formulas in new excel file,it has given this error with its old name.Following line solved problem like a charm:

wb.setForceFormulaRecalculation(true)

Hope it will help someone.

Upvotes: 2

Related Questions