[英]Giving out wrong value when evaluating COUNTIF function using apache poi formula evaluation
I have one excel workbook file in which there are 2 sheets.我有一个 excel 工作簿文件,其中有 2 张。 Sheet 1 has two columns which are just like values and criteria's like
工作表 1 有两列,就像值和标准一样
and the other sheet has formula defined as =COUNTIF(Sheet1:A2,A13:Sheet1!B2:B4)
另一张表的公式定义为
=COUNTIF(Sheet1:A2,A13:Sheet1!B2:B4)
so other sheet looks like所以其他表看起来像
Now when I am reading this second sheet, I want to evaluate the formula to extract the correct values from the workbook but it is giving all wrong values.现在,当我阅读第二张纸时,我想评估公式以从工作簿中提取正确的值,但它给出了所有错误的值。 Code is as follows:
代码如下:
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();
Could some one please point me what wrong I am doing here...有人可以指出我在这里做错了什么......
Used as normal worksheet function, COUNTIF does not use a cell range as second parameter but only one value or one cell reference.用作普通工作表 function, COUNTIF不使用单元格范围作为第二个参数,而是仅使用一个值或一个单元格引用。
If you have =COUNTIF(Sheet1:A2,A13:Sheet1!B2:B4)
as formula in a sheet, then you are using that function as dynamic array formula using spilled array behavior .如果您有
=COUNTIF(Sheet1:A2,A13:Sheet1!B2:B4)
作为工作表中的公式,那么您正在使用 function 作为使用溢出数组行为的动态数组公式。 Current Excel versions provide this.当前的 Excel 版本提供了这个。 But Apache POI (current versions of January 2023) cannot evaluate such formulas properly.
但是 Apache POI(2023 年 1 月的当前版本)无法正确评估此类公式。
If you would have normal worksheet functions in your Sheet2, such as:如果您的 Sheet2 中有正常的工作表函数,例如:
=COUNTIF(Sheet1:A2,A13,Sheet1!B2)
in Sheet2!A1
=COUNTIF(Sheet1:A2,A13,Sheet1!B2)
在Sheet2!A1
=COUNTIF(Sheet1:A2,A13,Sheet1!B3)
in Sheet2!A2
=COUNTIF(Sheet1:A2,A13,Sheet1!B3)
在Sheet2!A2
=COUNTIF(Sheet1:A2,A13,Sheet1!B4)
in Sheet2!A3
=COUNTIF(Sheet1:A2,A13,Sheet1!B4)
在Sheet2!A3
then formula evaluation should work.那么公式评估应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.