I have two woorkbooks:
- map.xlsx
- req.xlsx
map.xlsx has a sheet Mapping, req.xlsx has a sheet ABC Request.
I want to insert and evaluate a formula in map.xlsx which references req.xlsx. According to POI Documentation I use the following code.
FormulaEvaluator mapWbEvaluator = mapWb.getCreationHelper().createFormulaEvaluator();
FormulaEvaluator reqWbEvaluator = reqWb.getCreationHelper().createFormulaEvaluator();
Map<String, FormulaEvaluator> evalMap = new HashMap<>();
evalMap.put("map.xlsx", mapWbEvaluator);
evalMap.put("req.xlsx", reqWbEvaluator);
mapWbEvaluator.setupReferencedWorkbooks(evalMap);
Sheet mapSheet = mapWb.getSheet("Mapping");
CellReference tgtRef = new CellReference("A25");
Cell tgtCell = mapSheet.getRow(tgtRef.getRow()).getCell(tgtRef.getCol());
tgtCell.setCellFormula("IF('[req.xlsx]ABC Request'!R28,1,2)");
CellValue cellVal = mapWbEvaluator.evaluate(tgtCell);
When the last line is executed I receive the following exception:
Exception in thread "main" java.lang.RuntimeException: Book not linked for filename req.xlsx at org.apache.poi.xssf.usermodel.BaseXSSFEvaluationWorkbook.resolveBookIndex(BaseXSSFEvaluationWorkbook.java:135)
Any idea what's going wrong? Do I miss anything?