I'm having a problem evaluating excel sheet cell which uses named cell in its formula. I'm using apache-poi version 3.17, java 1.8 and the file I'm working with is .xlsm. Here is an example code:
//Getting the excel file from the database
Document doc = documentRepository.findByDocumentType(DocumentType.EXAMPLE);
InputStream fis = new ByteArrayInputStream(doc.getFileData());
Workbook wb = new XSSFWorkbook(fis);
Sheet mappingSheet = workbook.getSheet("Quote services");
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// This cell contains a formula which uses named cell
CellReference cellReference = new CellReference("A109");
Row row = mappingSheet.getRow(cellReference.getRow());
Cell cell = row.getCell(cellReference.getCol());
CellValue cellValue = evaluator.evaluate(cell);
This gives me the following error:
org.apache.poi.ss.formula.FormulaParseException: Specified named range 'Discount' does not exist in the current workbook.
The excel formula looks something like that:
=Discount*A7
Where Discount is A5
and if I change the formula to look like this:
=A5*A7
it works fine.
This is kind of strange cause if I try to get the named field Discount
it finds it, but if I use evaluate
on a cell which uses the field it doesn't.
The document is predefined so I can't change anything in it. If I name the given discount cell with poi and with the same name there is no problem, but I don't want to do this cause there are more named fields in the document and if something change I have to change the code. Any help will be appreciated!
I found the problem! It was that the Discount
field was referenced from a hidden sheet in the document and it was referenced like this: Discount
instead of 'Quote services'!Discount
.