繁体   English   中英

用公式读取单元格会得出与Excel文件不同的结果-Java Apache POI

[英]Reading cell with formula gives different result from excel file - Java 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);

设置公式不会更新单元格中的关联值。 您还需要评估单元格, 如此处所述 您可以使用FormulaEvaluator.evaluate()的结果“窥视”,或FormulaEvaluator.evaluateFormulaCell()将结果保存到细胞如Excel一样。

暂无
暂无

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

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