[英]Evaluate cell which uses named field in its formula - Apache POI
我在評估在其公式中使用命名單元格的excel表格單元格時遇到問題。 我正在使用apache-poi版本3.17 , java 1.8,並且正在使用的文件是.xlsm 。 這是示例代碼:
//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);
這給了我以下錯誤:
org.apache.poi.ss.formula.FormulaParseException:當前工作簿中不存在指定的命名范圍'Discount'。
excel公式看起來像這樣:
=Discount*A7
折扣為A5
,如果我將公式更改為以下形式:
=A5*A7
它工作正常。
如果我嘗試獲取命名字段Discount
可以找到它,這是一種奇怪的原因,但是如果我在使用該字段的單元格上使用evaluate
,則不會。
該文檔是預定義的,因此我無法對其進行任何更改。 如果我使用poi命名給定的折扣單元格,並且使用相同的名稱,則沒有問題,但是我不想這樣做,因為文檔中有更多的命名字段,如果有更改,我必須更改代碼。 任何幫助將不勝感激!
我發現了問題! Discount
字段是從文檔中的隱藏工作表中引用的,它是這樣引用的: Discount
而不是'Quote services'!Discount
。
您確定Discount
確實在您要評估的工作簿中嗎? 消息很清楚。
您能確切顯示公式的樣子嗎?是這樣的嗎:
=SUM('C:\Data\[Finances.xlsx]Discount'!D10:D25)
嘗試setIgnoreMissingWorkbooks(true)
,如果setIgnoreMissingWorkbooks(true)
,則在另一個工作簿中設置Discount
。 setIgnoreMissingWorkbooks 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.