title: Microsoft.Office.Interop.Excel.Range assemble object array with formulas and values from worksheet?
body: <p>Let's say I have a formula in cell A1, for example: A1 contains =B1+C1</p>
  
  <p>And B1 contains the number 5</p>
  
  <p>and C1 contains the number 5</p>
  
  <p>And then I use the following C# to get an object array of the UsedRange in the worksheet:</p>
  
  <pre><code>Microsoft.Office.Interop.Excel.Range excelRange = excelWorksheet.UsedRange;
  
  object[,] valueArray =  (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
  </code></pre>
  
  <p>ValueArray will contain 3 entries:</p>
  
  <p>ValueArray[0,0]=10</p>
  
  <p>ValueArray[0,1]=5</p>
  
  <p>ValueArray[0,2]=5</p>
  
  <p>How do I instead write the C# so that I am returned this:</p>
  
  <p>ValueArray[0,0]="=B1+C1"</p>
  
  <p>ValueArray[0,1]=5</p>
  
  <p>ValueArray[0,2]=5</p>
  
  <p>In summary, if it is a formula, I want the object array element to contain the formula, but if it is <em>not</em> a formula, I want the object array element to contain the value.</p>
  
  <h1>THE SOLUTION</h1>
  
  <pre><code>    object[,] formulaArray = (object[,]) excelRange.Formula;
      object[,] valueArray = (object[,])excelRange.Value2;
  </code></pre>
  
  <p>And all formula start with "="; therefore mix the object arrays as your logic dictates.</p>