title: MVC3 C# Export to Excel
body: <p>I am trying to export data from database to excel 2007 file. </p>
  
  <p>I just want to change the header of html file to excel 2007 file. </p>
  
  <p>I format the data into a table and change the header to this:</p>
  
  <pre><code>Response.AddHeader("Content-Disposition", "attachment;filename= filename.xlsx");
  Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  </code></pre>
  
  <p>I keep getting the same error: </p>
  
  <blockquote>
    <p>"Excel cannot open the file "filename.xlsx' because the file format of
    file extension is not valid. Verify that the file has not been
    corrupted and that the file extension matches the format of the file"</p>
  </blockquote>
  
  <p>I also tried this example i found online and i can open in excel 2003 with a warning message, but on 2007 i get the above error message. It need to make it work with excel2007</p>
  
  <pre><code>&lt;html 
  xmlns:o="urn:schemas-microsoft-com:office:office" 
  xmlns:x="urn:schemas-microsoft-com:office:excel" 
  xmlns="http://www.w3.org/TR/REC-html40"&gt;
  &lt;head&gt;
  &lt;meta http-equiv=Content-Type content="text/html; charset=windows-1252"&gt;
  &lt;meta name=ProgId content=Excel.Sheet&gt;
  &lt;meta name=Generator content="Microsoft Excel 11"&gt;
  &lt;!--[if gte mso 9]&gt;&lt;xml&gt;
   &lt;x:ExcelWorkbook&gt;
    &lt;x:ExcelWorksheets&gt;
     &lt;x:ExcelWorksheet&gt;
      &lt;x:Name&gt;Sheet1&lt;/x:Name&gt;
      &lt;x:WorksheetOptions&gt;
       &lt;x:Selected/&gt;
       &lt;x:ProtectContents&gt;False&lt;/x:ProtectContents&gt;
       &lt;x:ProtectObjects&gt;False&lt;/x:ProtectObjects&gt;
       &lt;x:ProtectScenarios&gt;False&lt;/x:ProtectScenarios&gt;
      &lt;/x:WorksheetOptions&gt;
     &lt;/x:ExcelWorksheet&gt;
    &lt;/x:ExcelWorksheets&gt;
    &lt;x:ProtectStructure&gt;False&lt;/x:ProtectStructure&gt;
    &lt;x:ProtectWindows&gt;False&lt;/x:ProtectWindows&gt;
  &lt;/x:ExcelWorkbook&gt;
  &lt;/xml&gt;&lt;![endif]--&gt;
  &lt;style&gt;
  &lt;!--table
      {mso-displayed-decimal-separator:"\.";
      mso-displayed-thousand-separator:" ";}
  .xl2
      {
      mso-number-format:M/D/YY;
      border-left:.5pt solid;
      border-top:.5pt solid;
      border-right:.5pt solid;
      border-bottom:.5pt solid;
      }
  .xl3
      {
      border-left:.5pt solid;
      border-top:.5pt solid;
      border-right:.5pt solid;
      border-bottom:.5pt solid;
      }
  --&gt;
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td class=xl2&gt;17.02.2010&lt;/td&gt;
  &lt;td class=xl3&gt;4&lt;/td&gt;
  &lt;td class=xl3&gt;0&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>