title: OpenXML SDK: Make Excel recalculate formula
body: <p>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 cells invalid. However, due to the cached values Excel does not recalculate the formular, even if the user clicks on "Calculate now".</p>
  
  <p>What is the best way to invalidate all dependent cells of the whole workbook through the SDK? So far, I've found the following code snippet at <a href="http://cdonner.com/introduction-to-microsofts-open-xml-format-sdk-20-with-a-focus-on-excel-documents.htm" rel="noreferrer">http://cdonner.com/introduction-to-microsofts-open-xml-format-sdk-20-with-a-focus-on-excel-documents.htm</a>:</p>
  
  <pre><code>public static void ClearAllValuesInSheet
        (SpreadsheetDocument spreadSheet, string sheetName)
  {
      WorksheetPart worksheetPart =
          GetWorksheetPartByName(spreadSheet, sheetName);
  
      foreach (Row row in
         worksheetPart.Worksheet.
            GetFirstChild().Elements())
      {
          foreach (Cell cell in row.Elements())
          {
              if (cell.CellFormula != null &amp;&amp;
                    cell.CellValue != null)
              {
                  cell.CellValue.Remove();
              }
          }
  
      }
  
      worksheetPart.Worksheet.Save();
  }
  </code></pre>
  
  <p>Besides the fact that this snippet does not compile for me, it has two limitations:</p>
  
  <ul>
  <li>It only invalidates a single sheet, although other sheets might contain dependent formula</li>
  <li>It does not take into account any dependencies.</li>
  </ul>
  
  <p>I am looking for a way that is efficient (in particular, only invalidates cells that depend on a certain cell's value), and takes all sheets into account.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>In the meantime I have managed to make the code compile &amp; run, and to remove the cached values on all sheets of the workbook. (See answers.) Still I am interested in better/alternative solutions, in particular how to only delete cached values of the cells that actually depend on the updated cell. </p>