title: OpenXML SDK - C# - Calculation Chain / Marking Cells as Edited to Force Full Recalculation on Open for Mobile/Web
body: <h3>Use Case</h3>
  <p>I have a windows service that makes use of the OpenXML sdk to write in tabular pricing data to an Excel sheet each day. This report is made available via a file share as well as sent out as an attachment. There are several other sheets in the workbook containing formulas that reference the data that is written in via OpenXML for visualizations, etc.</p>
  <p>If the ForceFullCalculation property is not set, the formulas do not update upon open until an edit is made to a cell farthest down in the formula dependency chain.</p>
  <p><code>document.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;</code></p>
  <p>This line solves all the issues with calculation because all formulas are re-evaluated upon open.. <em>IF opened in the Excel desktop client.</em></p>
  <h3>Problem</h3>
  <p>On the mobile Office app, mobile Excel app, Excel Online, and email preview applications, the formulas are not evaluated until an edit is made to a cell that triggers a recalculation. All show as N/A.</p>
  <h3>Efforts to Solve</h3>
  <p>Things that have not solved this issue for mobile/web/attachment viewers:</p>
  <ul>
  <li>ForceFullCalculation = true</li>
  <li>CalculationOnSave = true</li>
  <li>CalculationMode = CalculateModeValues.Auto (Excel Automatic Calculation Mode)</li>
  <li>Removing the <code>CalculaitonChainPart</code> element.</li>
  <li>Unzipping the xlsx and deleting/editing/removing cell data from the CalcChain file.</li>
  <li>Manually clicking the &quot;Calculate Workbook&quot; / &quot;Calculate Cell&quot; / &quot;Calculate Sheet&quot; buttons via the non-desktop Excel versions</li>
  <li>Publishing the file to sharepoint online / office 365 prior to opening</li>
  </ul>
  <p>What has &quot;solved&quot; the issue</p>
  <ul>
  <li>Opening the file on the desktop version of Excel prior to distributing the sheet. This would work, but requires manual intervention that isn't practical for a service based app.</li>
  <li>Opening the file on non-desktop versions of Excel and editing a cell value in the &quot;formula tree&quot;.</li>
  </ul>
  <h3>Goal</h3>
  <p>My hope is that someone can provide additional insight to this problem and address whether or not this is solvable using the OpenXML sdk. If the non-desktop versions of excel do not respect the <code>ForceFullCalculation</code> property, can anything be done via the XML to make excel treat cells as dirty and run a recalculation?</p>
  <h4>Ideal Scenario</h4>
  <p>My service creates a new xlsx file and writes data in via the OpenXML sdk. The file is then written out to a file share and sent as an email attachment. Recipients can open the workbook on desktop, mobile, web, and less-importantly attachment viewers and see the result of formulas rather than N/A until tracking down a specific cell to edit to trick excel into updating all the values.</p>
  <h3>Additional Notes</h3>
  <p>I am painfully aware that this is a misuse of Excel/OpenXML - a reporting solution like tableau/power BI are much better suited for my use case, but this is a sheet that was originally created by business users and has been a manual workload for one person to update each day for years. Automating the daily data entry and distribution of the sheet allows our users to continue using other sheets that reference this sheet and doesn't require a large effort to redefine the entire process for what essentially boils down to a report that people look at each morning.</p>
  <p>Eventually, we'll have a better solution, but that is a slow and ongoing effort. I'm hoping that there is a way forward that satisfies the need for now.</p>
  <p>Thanks for reading &amp; any assistance :)</p>
  <h3>Sample Code</h3>
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.IO;
  using System.Threading.Tasks;
  using DocumentFormat.OpenXml.Packaging;
  using DocumentFormat.OpenXml.Spreadsheet;
  
  namespace SampleProject.Services {
  
      public interface IExportPriceDataService
      {
          public Task CreateDailyWorkbookAsync();
      }
  
      public class ExportPriceDataService : IExportPriceDataService
      {
          private readonly IDistributionService _distributionService;
          
          public ExportPriceDataService(IDistributionService distributionService)
          {
              _ditributionService = distributionService; 
          }
  
          public async Task CreateDailyWorkbookAsync()
          {
              // Template file - contains constant data (headers, formulas, etc.) but no data.
              var templateFile = &quot;C:\\DailyExportTemplate.xlsx&quot;; 
              var filename = &quot;C:\\DailyExport.xlsx&quot;;
              
              // Load price data from the database - assume already shaped into the PriceData data structure (classes below).
              List&lt;PriceData&gt; priceData = await fetchTabularPriceDataAsync(DateTime.Now.Year, DateTime.Now.Month);
  
              // Delete previous day exports.
              if (File.Exists(filename))
              {
                  File.Delete(filename);
              }
  
              // Create today's copy.
              File.Copy(templateFile, filename);
  
              // Populate today's copy with data. 
              using (SpreadsheetDocument document = SpreadsheetDocument.Open(filename))
              {
                  if (document == null) return; 
                  var workbookPart = document.WorkbookPart; 
  
                  // Extension method to get the worksheet part by the sheet name. 
                  var worksheetPart = workbookPart.GetWorksheetPartByName(&quot;PriceDataSheet&quot;);
  
                  writePriceData(worksheetPart, priceData); 
  
                  document.WorkbookPart.Workbook.CalculationProperties.CalculationMode = CalculateModeValues.Auto;
                  document.WorkbookPart.Workbook.CalculationProperties.CalculationOnSave = true;
                  document.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
  
                  document.Save(); 
              }
  
              _distributionService.PublishToSharepointOnline(filename);
              _distributionService.SendAsEmailAttachment(filename); 
          }
  
          private void writePriceData(WorksheetPart worksheetPart, List&lt;PriceData&gt; priceData)
          {
              foreach (var item in priceData)
              {
                  // Match ItemName with constant column headers in the sheet.
                  var col = GetColumnForItem(); 
                  
                  // Header row is always row 1. 
                  var row = 1; 
                  
                  // Custom extension methods for creating and writing to cells. 
                  // Internal code from: https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-text-into-a-cell-in-a-spreadsheet
                  worksheetPart.InsertCellInWorksheet(col, row)
                               .SetCellType(CellValues.String)
                               .SetCellValue(new CellValue(item.ItemName));
                  
                  foreach (var price in Item.Prices)
                  {
                      // Row # matches up with the day plus offset of 1 for the header row. 
                      row = Date.day + 1; 
  
                      worksheetPart.InsertCellInWorksheet(col, row)
                                   .SetCellType(CellValues.Number)
                                   .SetCellValue(new CellValue(price.Price));
                  }
              }
          }
      }
  
      public class PriceData
      {
          public string ItemName {get; set;}
          public List&lt;PriceDate&gt; Prices { get; set; }
      }
  
      public class PriceDate
      {
          public DateTime Date {get; set;}
          public decimal Price {get; set;}
      }
  }
  </code></pre>