{"id":2688,"date":"2016-03-18T11:49:37","date_gmt":"2016-03-18T11:49:37","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2688"},"modified":"2016-03-20T10:44:46","modified_gmt":"2016-03-20T10:44:46","slug":"force-recalculation-of-workbook-worksheet-using-spreadsheetml","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/force-recalculation-of-workbook-worksheet-using-spreadsheetml\/","title":{"rendered":"Force Recalculation of Workbook \/ Worksheet using SpreadsheetML"},"content":{"rendered":"<p>If you change values in cells in a worksheet, you may want to force recalculation upon open. &nbsp;The following code will do it.<\/p>\n<p>This operates on an XLSX (attached to this post) that contains a number in cell A1, and presupposes that there is a formula that relies on A1, so therefore Excel should run the calculation engine upon opening. &nbsp;The code then uses Excel automation to load and save the XLSX, causing the calculation engine to run.<\/p>\n<p><pre class=\"prettyprint\">using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing DocumentFormat.OpenXml.Packaging;\r\nusing DocumentFormat.OpenXml.Spreadsheet;\r\nusing Ex = Microsoft.Office.Interop.Excel;\r\nusing System.Reflection;\r\n\r\nclass Program\r\n{\r\n    static void Main(string[] args)\r\n    {\r\n        FileInfo src = new FileInfo(&quot;..\/..\/Test.xlsx&quot;);\r\n        FileInfo dest = new FileInfo(&quot;..\/..\/Test-Out.xlsx&quot;);\r\n        FileInfo dest2 = new FileInfo(&quot;..\/..\/Test-Out2.xlsx&quot;);\r\n        if (dest.Exists) dest.Delete();\r\n        File.Copy(src.FullName, dest.FullName);\r\n\r\n        using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(dest.FullName, true))\r\n        {\r\n            \/\/ This is arbitrary code that adds 10 to the upper-left cell of the first worksheet.\r\n            \/\/ It is used in a calculation, so must force recalculation upon open.\r\n\r\n            WorkbookPart partWorkbook = spreadSheet.WorkbookPart;\r\n            Sheet firstSheet = (Sheet)partWorkbook.Workbook.Sheets.First();\r\n            WorksheetPart worksheetPart = (WorksheetPart)partWorkbook.GetPartById(firstSheet.Id);\r\n            var sheetData = worksheetPart.Worksheet.Elements&lt;SheetData&gt;().FirstOrDefault();\r\n            if (sheetData != null)\r\n            {\r\n                var row = sheetData.Elements&lt;Row&gt;().FirstOrDefault();\r\n                if (row != null)\r\n                {\r\n                    var cell = row.Elements&lt;Cell&gt;().FirstOrDefault();\r\n                    if (cell != null)\r\n                    {\r\n                        var cellValue = cell.CellValue.Text;\r\n                        double d;\r\n                        if (double.TryParse(cellValue, out d))\r\n                        {\r\n                            cell.CellValue.Text = (d + 10).ToString();\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n\r\n            var calculationProperties = spreadSheet.WorkbookPart.Workbook.CalculationProperties;\r\n            calculationProperties.ForceFullCalculation = true;\r\n            calculationProperties.FullCalculationOnLoad = true;\r\n        }\r\n\r\n        \/\/ Use Excel automation to open and save the workbook, thereby running the calculation engine.\r\n        Ex.Application app = new Ex.Application();\r\n        string execPath =\r\n          Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);\r\n        Ex.Workbook book = app.Workbooks.Open(dest.FullName);\r\n        book.SaveAs(dest2.FullName);  \/\/ or book.Save();\r\n        book.Close();\r\n        app.Quit();\r\n    }\r\n}<\/pre>\n<\/p>\n<p>Download &#8211; <a href=\"https:\/\/www.ericwhite.com\/blog\/wp-content\/uploads\/2016\/03\/Test.xlsx\">Sample Excel<\/a><\/p>\n<p>Cheers, Eric<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you change values in cells in a worksheet, you may want to force recalculation upon open. &nbsp;The following code will do it. This operates on an XLSX (attached to this post) that contains a number in cell A1, and presupposes that there is a formula that relies on A1, so therefore Excel should run [&hellip;]<\/p>\n","protected":false},"author":10567,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-2688","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/10567"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=2688"}],"version-history":[{"count":2,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2688\/revisions"}],"predecessor-version":[{"id":3093,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2688\/revisions\/3093"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}