gpt4 book ai didi

java - 使用公式读取单元格会给出与 excel 文件不同的结果 - Java Apache POI

转载 作者:行者123 更新时间:2023-11-30 07:49:33 25 4
gpt4 key购买 nike

我正在编写一个程序:

  • 打开 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);

最佳答案

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

关于java - 使用公式读取单元格会给出与 excel 文件不同的结果 - Java Apache POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48413722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com