简体   繁体   English

Apache POI org.apache.poi.ss.formula.FormulaParseException

[英]Apache POI org.apache.poi.ss.formula.FormulaParseException

I'm using Apache POI and I'm facing an issue when evaluating a formula cell. 我正在使用Apache POI,在评估公式单元格时我遇到了一个问题。

This is my code: 这是我的代码:

XSSFCell cellvalue = sheet.getRow(6).getCell(2);
if (cellvalue.getCellType() == cellvalue.CELL_TYPE_FORMULA) {
   evaluator.evaluateFormulaCell(cellvalue);
}

The formula: 公式:

=C6/Num_Input and Num_Input = F36

The issue: 问题:

org.apache.poi.ss.formula.FormulaParseException:
Cell reference expected after sheet name at index 18.

First, leave POI and check in excel itself if the formula is working fine. 首先,如果公式工作正常,请保留POI并检查excel本身。 There is no problem at POI end. POI结束没有问题。 I wrote this sample code and it's working fine. 我写了这个示例代码,它运行正常。

        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());

It evaluates to 2 + 5 = 7 as it should. 它应该评估为2 + 5 = 7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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