title: How to verify if a file is a valid excel spreadsheet?
body: <p>I already did some search here on SO, on <a href="https://stackoverflow.com/questions/236926/how-do-i-programatically-interface-an-excel-spreadsheet">this</a>, <a href="https://stackoverflow.com/questions/7613898/how-to-read-an-excel-spreadsheet-in-c-sharp-quickly">this</a>, <a href="https://stackoverflow.com/questions/5409351/how-to-read-data-from-excel-spreadsheet">this</a> and more, but no clue, so I'm going to have to ask you guys.</p>
  
  <p>I'm trying to open an Excel file via Interop, and I have Office 2007 installed, so I'm using <code>Microsoft.Office.Interop.Excel</code> version <code>14.0</code>.</p>
  
  <p>When I try to open a valid <code>xls</code> file, everything runs fine.</p>
  
  <p>But if I try to open an invalid file (like a bitmap, or an executable) via Interop, Excel opens it without complain or warning.</p>
  
  <p>How can I detect Excel has an invalid workbook file loaded, without blocking with an alert ?</p>
  
  <p>I'm trying to write a unit test of my Excel object reader. One of the cases is to try to open an invalid file.</p>
  
  <p>Some code to help: 
  Application Creation:</p>
  
  <pre><code>    _app = new Application()
          {
              Visible = false,
              DisplayAlerts = false,     // &lt;-- I don't want to remove this.
              AskToUpdateLinks = false,
          };
  </code></pre>
  
  <p>Workbook opening:</p>
  
  <pre><code>   _workbooks.Open(filename);
     _workbook = _workbooks.get_Item(1); // 1-based.
     _worksheets = _workbook.Sheets;
  </code></pre>
  
  <p>EDIT:
  Just to add 1 more information: Excel loads the invalid file. If <code>DisplayAlerts</code> is set to true, it complains (open a dialog) informing the workbook seems to be corrupted, but if <code>DisplayAlerts</code> is set to false, it loads the file just as below:</p>
  
  <p><img src="https://i.stack.imgur.com/199bu.png" alt="Excel Loads Invalid File"></p>