title: Adding formula to Excel with C# - making the formula shown
body: <p>I wanted to add formulas to an Excel workSheet.<br>
  I managed to do so with the <strong>Formula</strong> property.</p>
  
  <p>The problem is that when I open the worksheet in Excel, I can see that the formula works - but I can only see the result in the cell. I can't see the formula that was calculated in the Formula Bar at the top of Excel.</p>
  
  <p>Obviously if I enter a formula in Excel itself I can see the result in the cell and the formula in the Formula Bar.</p>
  
  <p>Some of my code: </p>
  
  <pre><code>for (int i = 0; i &lt; nOfColumns / 3; i++)
  {
      Range cells = workSheet.Range[workSheet.Cells[2, i * 3 + 3], workSheet.Cells[lastRowNumber, i * 3 + 3]];
      cells.FormulaR1C1 = "=IF(EXACT(RC[-2],RC[-1]),TRUE,ABS(RC[-2]/RC[-1]-1))";
  }
  </code></pre>
  
  <p>below is a test code. even after I save the workbook - the FormulaHidden is false and I can successfully retrieve the formula insterted. really frustrated</p>
  
  <pre><code>        Microsoft.Office.Interop.Excel.Application excelApp = null;
          Workbooks workBooks = null;
          Workbook workBook = null;
          Worksheet workSheet;
  
          try
          {
  
              excelApp = new Microsoft.Office.Interop.Excel.Application();
              excelApp.DisplayAlerts = false;
  
              workBooks = excelApp.Workbooks;
              workBook = workBooks.Open(filePath, AddToMru: false);
              workSheet = workBook.Worksheets.get_Item(1);
  
              int nOfColumns = workSheet.UsedRange.Columns.Count;
              int lastRowNumber = workSheet.UsedRange.Rows.Count;
  
              Range rng = workSheet.Range["C1"];
              rng.Formula = "=SUM(B2:B4)";
              String formula = rng.Formula; //retrieve the formula successfully
  
              rng.FormulaHidden = false;
              workSheet.Unprotect();
  
              workBook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlExclusive);
  
              formula = rng.Formula;  //retrieve the formula successfully
              bool hidden = rng.FormulaHidden;
  
          }
          catch (Exception e)
          {
              throw;
          }
          finally
          {
              if (workBook != null)
              {
                  workBook.Close();
                  workBook = null;
              }
              if (workBooks != null)
              {
                  workBooks.Close();
                  workBooks = null;
              }
              if (excelApp != null)
              {
                  excelApp.Quit();
                  excelApp = null;
              }
          }
      }
  </code></pre>
  
  <p>Anyone know how to make the formula shown, <strong>when adding the formulas programatically</strong> ?</p>