繁体   English   中英

使用 apache poi 公式评估评估 COUNTIF function 时给出错误值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM