我正在使用 Apache POI,在评估公式单元格时遇到问题。
这是我的代码:
XSSFCell cellvalue = sheet.getRow(6).getCell(2);
if (cellvalue.getCellType() == cellvalue.CELL_TYPE_FORMULA) {
evaluator.evaluateFormulaCell(cellvalue);
}
公式:
=C6/Num_Input and Num_Input = F36
问题:
org.apache.poi.ss.formula.FormulaParseException:
Cell reference expected after sheet name at index 18.
首先,离开 POI 并检查 Excel 本身是否公式运行正常。 POI端没有问题。我编写了这个示例代码并且运行良好。
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(workbook);
XSSFCell cellvalue = sheet.createRow(0).createCell(0);
sheet.getRow(0).createCell(1).setCellValue(2);
sheet.getRow(0).createCell(2).setCellValue(5);
cellvalue.setCellFormula("B1+C1");
if (cellvalue.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
evaluator.evaluateFormulaCell(cellvalue);
}
System.out.println(cellvalue.getNumericCellValue());
它的计算结果应该是 2 + 5 = 7。
QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ