java - Apache POI 不更新公式

标签 java excel excel-formula apache-poi

我正在使用 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 the cell and also its cell type.

没有任何内容更新单元格中的公式结果。

对面FormulaEvaluator.evaluateFormulaCell :

If cell contains formula, it evaluates the formula, and saves the result 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/

相关文章:

python - 在 python 中打开和读取 excel .xlsx 文件

android - 在我的 android 应用程序中查看 excel 文件

excel - 查找单元格中一起出现的 n 个数字的公式

excel - 不能从另一个单元格中减去一个单元格的文本

excel - 多列与多列查找

java - Runtime.getRuntime().exec(),执行Java类

java - 无法将 Firebase 数据库中存储的图像(静态)提取到我的应用程序中

java - 如何在Android中记录和解析InputStream

java - ANDROID 由 CursorWindow 的 : java. lang.IllegalStateException : Couldn't read row 0, col 0 引起

java - Sqlsheet公式评估