如果您使用的版本不是POI 3.5或更高版本,则可能会遇到此问题。 在这种情况下,您需要使用POI 3.5或更高版本才能更新公式。
另外,请确保已使用 FormulaEvaluator 对象对更新的公式进行了重新计算。 以下是一个示例:
// Obtain the workbook object Workbook workbook = WorkbookFactory.create(new FileInputStream("workbook.xls"));
// Get the sheet you want to update the formula in Sheet sheet = workbook.getSheetAt(0);
// Get the cell that contains the formula you want to update Cell cellToUpdate = sheet.getRow(0).getCell(0);
// Update the formula with the new formula cellToUpdate.setCellFormula("SUM(A1:A10)");
// Obtain a FormulaEvaluator object FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
// Evaluate the cell to update the formula evaluator.evaluateFormulaCell(cellToUpdate);
// Save the changes to the workbook FileOutputStream fileOut = new FileOutputStream("workbook.xls"); workbook.write(fileOut); fileOut.close();