打开xml sdk excel公式重新计算缓存问题

我已经查看了之前所有类似问题的答案,但这些答案都不起作用。 我传入excel中的值,并希望公式单元格重新计算,但它仍然显示旧的值。

我使用了2种技术1)从单元格中删除计算出的值2)以编程方式打开和关闭文件。 它们都不适合我。 这是我的代码。 请帮忙。

string filename = Server.MapPath("/") + "ExcelData.xlsx"; // getting 2 numbers in excel sheet, saving, and closing it. using (SpreadsheetDocument document = SpreadsheetDocument.Open(filename, true)) { Sheet sheet = document.WorkbookPart.Workbook.Descendants().SingleOrDefault(s => s.Name == "myRange1"); if (sheet == null) { throw new ArgumentException( String.Format("No sheet named {0} found in spreadsheet {1}", "myRange1", filename), "sheetName"); } WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id); int rowIndex = int.Parse("C3".Substring(1)); Row row = worksheetPart.Worksheet.GetFirstChild(). Elements().FirstOrDefault(r => r.RowIndex == rowIndex); Cell cell3 = row.Elements().FirstOrDefault(c => "C3".Equals(c.CellReference.Value)); if (cell3 != null) { cell3.CellValue = new CellValue("13"); cell3.DataType = new DocumentFormat.OpenXml.EnumValue(CellValues.Number); } document.WorkbookPart.WorksheetParts .SelectMany(part => part.Worksheet.Elements()) .SelectMany(data => data.Elements()) .SelectMany(row1 => row1.Elements()) .Where(cell => cell.CellFormula != null && cell.CellValue != null) .ToList() .ForEach(cell => cell.CellValue.Remove()); worksheetPart.Worksheet.Save(); } // getting the result out of excel. using (SpreadsheetDocument document = SpreadsheetDocument.Open(filename, false)) { document.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true; document.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true; Sheet sheet = document.WorkbookPart.Workbook.Descendants().SingleOrDefault(s => s.Name == "myRange1"); if (sheet == null) { throw new ArgumentException( String.Format("No sheet named {0} found in spreadsheet {1}", "myRange1", filename), "sheetName"); } WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id); int rowIndex = int.Parse("C4".Substring(1)); Row row = worksheetPart.Worksheet.GetFirstChild(). Elements().FirstOrDefault(r => r.RowIndex == rowIndex); Cell cell = row.Elements().FirstOrDefault(c => "C4".Equals(c.CellReference.Value)); calculatedvalue = Convert.ToDouble(cell.CellValue.InnerText); } 

从你提供的代码片段我想念你所提到的博客的重要部分。

如该博客中所述,OpenXML只能用于创建/读取/更新和删除OpenXML兼容应用程序的存储格式。 您没有获得实际的应用程序引擎。 要实现您的目标,您需要在服务器上安装Excel,以便可以利用Microsoft.Office.Interop.Excel命名空间及其Application COM接口,或依赖最终用户Excel应用程序在打开更新的Excel工作表时进行重新计算。

复制的代码和从此博客改编的文本

 var excelApp = new Microsoft.Office.Interop.Excel.Application(); Workbook workbook = excelApp.Workbooks.Open(Path.GetFullPath(_filePath)); workbook.Close(true); excelApp.Quit(); 

请注意,如果您需要在服务器上运行(例如,因为您需要在网页中计算结果),您可能会遇到严重的性能问题,内存,I / O和CPU。 在那种情况下,我宁愿在C#中实现计算规则/公式。