无法使用apache poi java在excel中评估嵌套公式

时间:2019-06-13 10:45:53

标签: java excel apache-poi

嗨,我正在尝试使用嵌套公式来评估单元格值 apache POI 4.1.0。当我尝试使用普通公式时,它可以正常工作。 考虑以下代码:

FileInputStream inputExcelFile = new FileInputStream(new File(INPUT_FILE_NAME)); 
XSSFWorkbook workbook = new XSSFWorkbook(inputExcelFile);
XSSFSheet sheet = workbook.createSheet("Total");             
Row row = sheet.createRow(0); Cell cell = row.createCell(0);     
//setting formula in the cell    
cell.setCellFormula("IF('Data Set'!B2=0,CONCATENATE(IF('Data Set'!J2="","UMS",'Data Set'!J2),"_",IF('Data Set'!K2<>"",'Data Set'!K2&"_",""),'Data Set'!H2),"");

Cell cellValue = formulaEvaluator.evaluateInCell(cell);

现在,如果执行cell.setCellFormula("CONCATENATE(F4,"/",G4)")或任何直接形式的代码,此代码可以正常工作,但是当使用嵌套公式(例如“ IF(....,CONCATENATE(IF ...))”)时,它将引发以下错误

Exception in thread "main" org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell Total!A1
    at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:344)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:285)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:216)
    at org.apache.poi.xssf.usermodel.BaseXSSFFormulaEvaluator.evaluateFormulaCellValue(BaseXSSFFormulaEvaluator.java:56)
    at org.apache.poi.ss.formula.BaseFormulaEvaluator.evaluateInCell(BaseFormulaEvaluator.java:145)
    at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateInCell(XSSFFormulaEvaluator.java:85)
    at org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator.evaluateInCell(XSSFFormulaEvaluator.java:34)
    at com.example.com.CollibraAutomationScript.evaluateFormula(CollibraAutomationScript.java:255)
    at com.example.com.CollibraAutomationScript.main(CollibraAutomationScript.java:164)
Caused by: org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell 'Data Set'!B2
    at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:344)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:285)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateReference(WorkbookEvaluator.java:792)
    at org.apache.poi.ss.formula.SheetRefEvaluator.getEvalForCell(SheetRefEvaluator.java:48)
    at org.apache.poi.ss.formula.SheetRangeEvaluator.getEvalForCell(SheetRangeEvaluator.java:74)
    at org.apache.poi.ss.formula.LazyRefEval.getInnerValueEval(LazyRefEval.java:39)
    at org.apache.poi.ss.formula.eval.OperandResolver.chooseSingleElementFromRef(OperandResolver.java:217)
    at org.apache.poi.ss.formula.eval.OperandResolver.getSingleValue(OperandResolver.java:67)
    at org.apache.poi.ss.formula.eval.RelationalOperationEval.evaluate(RelationalOperationEval.java:66)
    at org.apache.poi.ss.formula.functions.Fixed2ArgFunction.evaluate(Fixed2ArgFunction.java:33)
    at org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:153)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:541)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:275)
    ... 7 more
Caused by: org.apache.poi.ss.formula.eval.NotImplementedException: Error evaluating cell 'Data Set'!A2
    at org.apache.poi.ss.formula.WorkbookEvaluator.addExceptionInfo(WorkbookEvaluator.java:344)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:285)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateReference(WorkbookEvaluator.java:792)
    at org.apache.poi.ss.formula.SheetRefEvaluator.getEvalForCell(SheetRefEvaluator.java:48)
    at org.apache.poi.ss.formula.SheetRangeEvaluator.getEvalForCell(SheetRangeEvaluator.java:74)
    at org.apache.poi.ss.formula.LazyRefEval.getInnerValueEval(LazyRefEval.java:39)
    at org.apache.poi.ss.formula.eval.OperandResolver.chooseSingleElementFromRef(OperandResolver.java:217)
    at org.apache.poi.ss.formula.eval.OperandResolver.getSingleValue(OperandResolver.java:67)
    at org.apache.poi.ss.formula.functions.Countif.evaluateCriteriaArg(Countif.java:498)
    at org.apache.poi.ss.formula.functions.Countif.createCriteriaPredicate(Countif.java:470)
    at org.apache.poi.ss.formula.functions.Countif.evaluate(Countif.java:442)
    at org.apache.poi.ss.formula.functions.Fixed2ArgFunction.evaluate(Fixed2ArgFunction.java:33)
    at org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:153)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:541)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:275)
    ... 18 more
Caused by: org.apache.poi.ss.formula.eval.NotImplementedFunctionException: _xlfn.CONCAT
    at org.apache.poi.ss.formula.UserDefinedFunction.evaluate(UserDefinedFunction.java:56)
    at org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:155)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateFormula(WorkbookEvaluator.java:541)
    at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:275)
    ... 31 more

所以apache poi不允许执行嵌套公式吗?因为当我将相同的公式放在excel工作表中时,它的评估正确。

0 个答案:

没有答案