title: Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error
body: <p>I am in the process of writing a module to export a DataTable to Excel using <code>Microsoft.Office.Interop.Excel</code> but before starting in earnest I want to get the very basics working: open file, save as, and close.</p>
  
  <p>I have succeeded in opening and saving a file with the .xls extension, but saving with the .xlsx extension does not work. It writes the .xlsx file, but when I try to open it I get the following error:</p>
  
  <blockquote>
    <p>Excel cannot open the file 'SomeFile.xlsx' because the file format is not valid. Verify that file has not been corrupted and that the file extension matched the format of the file.</p>
  </blockquote>
  
  <p>The code I use to open, save and close the files is:</p>
  
  <pre><code>Excel.Application excelApplication = new Excel.Application();
  //excelApplication.Visible = true;
  //dynamic excelWorkBook = excelApplication.Workbooks.Add();
  Excel.Workbook excelWorkBook = excelApplication.Workbooks.Add();
  //Excel.Worksheet wkSheetData = excelWorkBook.ActiveSheet;
  int rowIndex = 1; int colIndex = 1;
  excelApplication.Cells[rowIndex, colIndex] = "TextField";
  
  // This works.
  excelWorkBook.SaveAs("C:\\MyExcelTestTest.xls", Excel.XlFileFormat.xlWorkbookNormal,
      System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false,
      Excel.XlSaveAsAccessMode.xlShared, false, false, System.Reflection.Missing.Value,
      System.Reflection.Missing.Value, System.Reflection.Missing.Value);
  
  // This does not!?
  excelWorkBook.SaveAs("C:\\MyExcelTestTest.xlsx", Excel.XlFileFormat.xlWorkbookNormal, 
      System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, 
      Excel.XlSaveAsAccessMode.xlShared, false, false, System.Reflection.Missing.Value, 
      System.Reflection.Missing.Value, System.Reflection.Missing.Value);
  
  excelWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
  </code></pre>
  
  <p>I have also tried the file format <code>Excel.XlFileFormat.xlExcel12</code> in place of <code>Excel.XlFileFormat.xlWorkbookNormal</code> but this does not even write instead throwing the COMException:</p>
  
  <blockquote>
    <p>Exception from HRESULT: 0x800A03EC</p>
  </blockquote>
  
  <p>Any help resolving this would be most appreciated.</p>
  
  <p>Edit: I have now also tried:</p>
  
  <pre><code>excelWorkBook.SaveAs("C:\\MyExcelTestTest", Excel.XlFileFormat.xlExcel12, 
      System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, 
      Excel.XlSaveAsAccessMode.xlShared, false, false, System.Reflection.Missing.Value, 
      System.Reflection.Missing.Value, System.Reflection.Missing.Value);
  </code></pre>