OpenXML SDK: Make Excel recalculate formula

前端 未结 5 433
旧巷少年郎
旧巷少年郎 2020-11-30 06:15

I update some cells of an Excel spreadsheet through the Microsoft Office OpenXML SDK 2.0. Changing the values makes all cells containing formula that depend on the changed c

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 06:37

    You need to save the worksheet at the end, This worked for me.

    foreach (WorksheetPart worksheetPart in spreadSheet.WorkbookPart.WorksheetParts) {
        foreach (Row row in
                worksheetPart.Worksheet.GetFirstChild().Elements()) {
            foreach (Cell cell in row.Elements()) {
                if (cell.CellFormula != null && cell.CellValue != null)
                    cell.CellValue.Remove();
            }
        }
        worksheetPart.Worksheet.Save();
    }
    

提交回复
热议问题