gpt4 book ai didi

java - Apache POI 不更新公式

转载 作者:行者123 更新时间:2023-12-04 22:17:59 25 4
gpt4 key购买 nike

我正在使用 Apache POI 库来使用 excel 表中的一些公式,但似乎这些公式没有在运行时更新。如果我保存工作簿并重新打开它,则会重新计算它们。有没有办法在运行时计算它们?
我的代码是:

FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(1);

datatypeSheet.getRow(19).getCell(2).setCellValue(0); // set C20=0
workbook.setForceFormulaRecalculation(true);

System.out.println(workbook.getSheetAt(1).getRow(19).getCell(8).getNumericCellValue()); // prints out the result

FileOutputStream out = new FileOutputStream(new File(FILE_NAME));
workbook.write(out);
out.close();

我也试过:
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.clearAllCachedResultValues();
evaluator.notifySetFormula(workbook.getSheetAt(1).getRow(19).getCell(8));
evaluator.evaluate(workbook.getSheetAt(1).getRow(19).getCell(8));
但它们总是返回旧值。

最佳答案

Workbook.setForceFormulaRecalculation明确地只强制重新计算过程到Excel下次打开工作簿时。所以按设计工作。
但是使用 FormulaEvaluator是正确的方法。但是你应该使用 FormulaEvaluator.evaluateFormulaCell而不是 FormulaEvaluator.evaluate .
来自 FormulaEvaluator.evaluate :

If cell contains a formula, the formula is evaluated and returned,else the CellValue simply copies the appropriate cell value from thecell and also its cell type.


单元格中没有更新公式的结果。
在对面 FormulaEvaluator.evaluateFormulaCell :

If cell contains formula, it evaluates the formula, and saves theresult of the formula. The cell remains as a formula cell.


单元格中公式的结果也被保存。
ExcelExample.xlsx 的第二张表中,以下对我有用细胞 C20包含一个数字和单元格 I20包含具有 C20 的公式作为参数。
import org.apache.poi.ss.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;

class ExcelEvaluateCell {

public static void main(String[] args) throws Exception {

Workbook workbook = WorkbookFactory.create(new FileInputStream("./ExcelExample.xlsx"));
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();

System.out.println(workbook.getSheetAt(1).getRow(19).getCell(2).getNumericCellValue()); // prints value of C20

System.out.println(workbook.getSheetAt(1).getRow(19).getCell(8).getCellFormula()); // prints formula in I20
System.out.println(workbook.getSheetAt(1).getRow(19).getCell(8).getNumericCellValue()); // prints result of formula in in I20

workbook.getSheetAt(1).getRow(19).getCell(2).setCellValue(0); // set C20 = 0

evaluator.evaluateFormulaCell(workbook.getSheetAt(1).getRow(19).getCell(8)); // evaluates formula in cell I20 and updates the result
System.out.println(workbook.getSheetAt(1).getRow(19).getCell(8).getNumericCellValue()); // prints new result of formula in in I20

FileOutputStream out = new FileOutputStream("./ExcelExampleNew.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}

关于java - Apache POI 不更新公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67023497/

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