【问题标题】:Reading cell with formula gives different result from excel file - Java Apache POI用公式读取单元格会给出与 excel 文件不同的结果 - Java Apache POI
【发布时间】:2018-01-24 01:59:42
【问题描述】:

我正在编写一个程序:

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

鉴于我下面的 sn-p,我可以成功写下公式并检查 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

EDIT #2:这是工作的 sn-p。

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

【问题讨论】:

    标签: java excel excel-formula apache-poi


    【解决方案1】:

    设置公式不会更新单元格中的关联值。您还需要评估单元格,as described here。 您可以使用FormulaEvaluator.evaluate()“查看”结果,或使用FormulaEvaluator.evaluateFormulaCell() 将结果保存到 Excel 中的单元格中。

    【讨论】:

    • 哇。我得到了我想要的输出。不过只是跟进。在设置公式后仅放置 evaulateFormulaCell(currentCell) 是否正确?只要确保它不会导致进一步的错误。我在上面添加了工作 sn-p(参见 EDIT 2)
    猜你喜欢
    • 2019-06-06
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    相关资源
    最近更新 更多