【问题标题】:open xml sdk excel formula recalculate cache issue打开 xml sdk excel 公式重新计算缓存问题
【发布时间】:2014-01-24 16:49:38
【问题描述】:

我已经查看了 SO 中类似问题的所有先前答案,但这些答案不起作用。我在 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<Sheet>().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<SheetData>().
                        Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);

                Cell cell3 = row.Elements<Cell>().FirstOrDefault(c => "C3".Equals(c.CellReference.Value));
                if (cell3 != null) 
                {
                    cell3.CellValue = new CellValue("13");
                    cell3.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.Number); 
                }

                document.WorkbookPart.WorksheetParts
   .SelectMany(part => part.Worksheet.Elements<SheetData>())
   .SelectMany(data => data.Elements<Row>())
   .SelectMany(row1 => row1.Elements<Cell>())
   .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<Sheet>().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<SheetData>().
                        Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);

                Cell cell = row.Elements<Cell>().FirstOrDefault(c => "C4".Equals(c.CellReference.Value));

                calculatedvalue = Convert.ToDouble(cell.CellValue.InnerText);
            }

【问题讨论】:

  • 您是否在第一次打开电子表格后的第一个代码块中尝试了ForceFullCalculation=true;
  • 是的.. 我刚试了一下,还是不行。过去3天我一直在发布同样的问题,但仍然没有人解决它..谈论挫败感!!!!!

标签: c# asp.net asp.net-mvc-4 openxml openxml-sdk


【解决方案1】:

从你提供的代码 sn-p 我错过了你提到的博客的基本部分。

如该博客所述,OpenXML 只能用于创建/读取/更新和删除 OpenXML 兼容应用程序的存储格式。您没有得到实际的应用程序引擎。为了实现您想要的,您需要在您的服务器上安装 Excel,以便您可以利用 Microsoft.Office.Interop.Excel namespace 和它的 Application COM 接口,或者在打开更新的 Excel 表时依靠最终用户的 Excel 应用程序进行重新计算。

code copied and text adapted from this blog

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# 中实现计算规则/公式。

【讨论】:

  • 感谢 rene,我确实遇到了您提到的博客,但没有意识到将 excel 和工作簿引用为 Microsoft.Interop... 对象的重要性。我试过了,效果很好。非常感谢您的帮助。我非常感谢。它***有效 ****
  • 嗨 Rene,在没有将 excel 安装到网络服务器中的情况下,在 Windows azure Web 云应用程序上工作的最佳方法是什么?提前谢谢你。
  • 我看不到也不知道有什么方法可以做到这一点,请参阅also this KB article。正如我所说,如果您需要计算服务器端,请在 C# 中实现。
  • 谢谢 Rene.. 我会看看那个链接。我发布了一个关于如何解析公式的单独问题.. stackoverflow.com/questions/21336769/…
猜你喜欢
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多