使用公式读取单元格会从excel文件中获得不同的结果 - Java Apache POI

时间:2018-01-24 01:59:42

标签: java excel excel-formula apache-poi

我正在编写一个程序:

  • 打开Excel文件
  • 将公式写入单元格 - SUM(item:item);
  • 读取公式输出的值

鉴于我的下面的代码片段,我可以成功写下公式并检查excel文件将产生正确的SUM答案。

            Row row = sheet.getRow(2); //which is row 3 at excel file
            Cell cell = row.createCell(0); //so that's cell A3
            cell.setCellFormula("SUM(A1:A2)");

可悲的是,当我读到公式的值时,应该是" 101.0"它给了我一个" 0.0"。

            row = sheet.getRow(2);
            Cell currentCell = row.getCell(0); //getting value of formula which we wrote above
            if (currentCell != null) {
                if (currentCell.getCellTypeEnum() == CellType.FORMULA) {
                    if (currentCell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) {
                        System.out.println("VALUE FROM PROGRAM: "+currentCell.getNumericCellValue());
                    }
                }
            }

我尝试过通过MS Excel应用程序编写公式来比较程序读取时返回的值。 事实证明,对于我的程序编写的公式,它是相同的不正确的值,但对于使用MS Excel编写的公式,该值是完全读取的。

我在这里缺少什么?为什么不能正确阅读第一个公式的答案?希望有人可以提供帮助。提前致谢。

这是我文件中的内容:

        |    A      |     B     |
    1   |    1      |     2     |
    2   |   100     |    100    |
    3   |=SUM(A1:A2)|=SUM(B1:B2)| //where A3 was written by java and B3 was written using MS Excel

这里是main中的完整代码:

    String surveyDirectory = "DATABASE.xlsx";
    XSSFWorkbook workbookx;
    FileOutputStream outputStream;
    FileInputStream fis;

    File file = new File(surveyDirectory);

    if (file.exists()) {
        try {
            fis = new FileInputStream(surveyDirectory);
            workbookx = new XSSFWorkbook(fis);
            Sheet sheet = workbookx.getSheetAt(0);

            //writing a formula to file

            Row row = sheet.createRow(2); //which is row 3 at excel file
            Cell cell = row.createCell(0); //so that's A3 in excel
            cell.setCellFormula("SUM(A1:A2)");

            outputStream = new FileOutputStream(surveyDirectory); //write it down the file
            workbookx.write(outputStream);
            outputStream.close();

            System.out.println("DONE WRITING");

            //reading the fomula file
            //we are accessing same sheet
            row = sheet.getRow(2);
            Cell currentCell = row.getCell(0); //getting value of formula which we wrote above
            System.out.println("FORMULA at A3: " + currentCell);
            if (currentCell != null) {
                if (currentCell.getCellTypeEnum() == CellType.FORMULA) {
                    if (currentCell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) {
                        System.out.println("VALUE FROM PROGRAM: "+currentCell.getNumericCellValue());
                    }
                }
            }

            row = sheet.getRow(2); //formula is =SUM(B1:B2)
            currentCell = row.getCell(1); //getting value of formula which was written vie MS Excel
            System.out.println("FORMULA at B3: " + currentCell);
            if (currentCell != null) {
                if (currentCell.getCellTypeEnum() == CellType.FORMULA) {
                    if (currentCell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) {
                        System.out.println("VALUE FROM MS EXCEL: "+currentCell.getNumericCellValue());
                    }
                }
            }

            System.out.println("DONE READING");
            fis.close();
            workbookx.close();
        } catch (IOException ex) {
            Logger.getLogger(TextHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

这是我得到的输出:

    DONE WRITING
    FORMULA at A3: SUM(A1:A2)
    VALUE FROM PROGRAM: 0.0
    FORMULA at B3: SUM(B1:B2)
    VALUE FROM MS EXCEL: 102.0
    DONE READING
    BUILD SUCCESSFUL (total time: 1 second)

编辑:我使用的是Apache POI 3.17

编辑#2 :这是工作代码段。

    Row row = sheet.getRow(2); 
    Cell cell = row.createCell(0); 
    cell.setCellFormula("SUM(A1:A2)");
    FormulaEvaluator evaluator = workbookx.getCreationHelper().createFormulaEvaluator();
    evaluator.evaluateFormulaCell(cell);

1 个答案:

答案 0 :(得分:3)

设置公式不会更新单元格中的关联值。您还需要评估单元格as described here。 您可以使用string [] args来查看"偷看"在结果中,或FormulaEvaluator.evaluate()将结果保存到单元格中,就像Excel一样。