title: Removing a formula from Excel using OpenXML
body: <p>I am trying to remove all formulas from a sheet using openxml. This what I am trying:</p>
  
  <pre><code>internal static void ReplaceFormulaWithValue()
  {
      var res = _worksheetPart.Worksheet.GetFirstChild&lt;SheetData&gt;().Elements&lt;Row&gt;();      
  
      foreach (Row row in _worksheetPart.Worksheet.GetFirstChild&lt;SheetData&gt;().Elements&lt;Row&gt;())
      {
          foreach (Cell cell in row.Elements&lt;Cell&gt;())
          {
              if (cell.CellFormula != null &amp;&amp;
                    cell.CellValue != null)
              {
                  string cellRef = cell.CellReference;
                  CalculationChainPart calculationChainPart = _spreadSheet.WorkbookPart.CalculationChainPart;
                  CalculationChain calculationChain = calculationChainPart.CalculationChain;
                  var calculationCells = calculationChain.Elements&lt;CalculationCell&gt;().ToList();
                  CalculationCell calculationCell = calculationCells.Where(c =&gt; c.CellReference == cellRef).FirstOrDefault();
                  //CalculationCell calculationCell = calculationChain.Elements&lt;CalculationCell&gt;().Where(c =&gt; c.CellReference == cell.CellReference).FirstOrDefault();
  
                  string value = cell.CellValue.InnerText;
                  UpdateCell(cell, DataTypes.String, value);
  
                  cell.CellFormula.Remove();
                  calculationCell.Remove();                   
              }
          }
      }
      SaveChanges();
  }
  </code></pre>
  
  <p>Upon opening the excel document, I am receiving the following error:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"&gt;
  &lt;logFileName&gt;error192600_01.xml&lt;/logFileName&gt;&lt;summary&gt;Errors were detected in file 'C:\DEV\ExcelEditor\ExcelEditor\bin\Debug\Chart.xlsx'&lt;/summary&gt;
  &lt;removedParts summary="Following is a list of removed parts:"&gt;
  &lt;removedPart&gt;Removed Part: /xl/calcChain.xml part with XML error.  (Calculation properties) Catastrophic failure Line 1, column 138.&lt;/removedPart&gt;
  &lt;/removedParts&gt;&lt;/recoveryLog&gt;
  </code></pre>
  
  <p>So I compare the old calcChain.xml file with the newly generated using OpenXML SDK Tool. The old file has the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes" ?&gt;
  &lt;calcChain xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"&gt;
   &lt;c i="1" l="1" r="D2"/&gt;
  &lt;/calcChain&gt;
  </code></pre>
  
  <p>and the new one after running my code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" standalone="yes" ?&gt;
  &lt;x:calcChain xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"&gt;  
  &lt;/x:calcChain&gt;
  </code></pre>
  
  <p>Could anyone help if I am missing something here.</p>