[英]Giving out wrong value when evaluating COUNTIF function using apache poi formula evaluation
我有一個 excel 工作簿文件,其中有 2 張。 工作表 1 有兩列,就像值和標准一樣
另一張表的公式定義為=COUNTIF(Sheet1:A2,A13:Sheet1!B2:B4)
現在,當我閱讀第二張紙時,我想評估公式以從工作簿中提取正確的值,但它給出了所有錯誤的值。 代碼如下:
try {
fis = new FileInputStream(fileName);
workbook = new XSSFWorkbook(fis);
} catch (IOException e) {
e.printStackTrace();
}
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Iterator<Row> rowIterator = workbook.getSheetAt(1).iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getCellType() == CellType.FORMULA) {
String cellFormula = cell.getCellFormula();
System.out.println("Cell Formula=" + cellFormula);
CellType cellType = evaluator.evaluateFormulaCell(cell);
if (cellType == CellType.NUMERIC) {
System.out.println(cell.getNumericCellValue());
}
} else if (cell.getCellType() == CellType.NUMERIC)
System.out.println(cell.getNumericCellValue());
}
}
closeFIS();
有人可以指出我在這里做錯了什么......
用作普通工作表 function, COUNTIF不使用單元格范圍作為第二個參數,而是僅使用一個值或一個單元格引用。
如果您有=COUNTIF(Sheet1:A2,A13:Sheet1!B2:B4)
作為工作表中的公式,那么您正在使用 function 作為使用溢出數組行為的動態數組公式。 當前的 Excel 版本提供了這個。 但是 Apache POI(2023 年 1 月的當前版本)無法正確評估此類公式。
如果您的 Sheet2 中有正常的工作表函數,例如:
=COUNTIF(Sheet1:A2,A13,Sheet1!B2)
在Sheet2!A1
=COUNTIF(Sheet1:A2,A13,Sheet1!B3)
在Sheet2!A2
=COUNTIF(Sheet1:A2,A13,Sheet1!B4)
在Sheet2!A3
那么公式評估應該起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.