0

I am trying to export data from database to excel 2007 file.

I just want to change the header of html file to excel 2007 file.

I format the data into a table and change the header to this:

Response.AddHeader("Content-Disposition", "attachment;filename= filename.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

I keep getting the same error:

"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"

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

<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">
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
<!--[if gte mso 9]><xml>
 <x:ExcelWorkbook>
  <x:ExcelWorksheets>
   <x:ExcelWorksheet>
    <x:Name>Sheet1</x:Name>
    <x:WorksheetOptions>
     <x:Selected/>
     <x:ProtectContents>False</x:ProtectContents>
     <x:ProtectObjects>False</x:ProtectObjects>
     <x:ProtectScenarios>False</x:ProtectScenarios>
    </x:WorksheetOptions>
   </x:ExcelWorksheet>
  </x:ExcelWorksheets>
  <x:ProtectStructure>False</x:ProtectStructure>
  <x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><![endif]-->
<style>
<!--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;
    }
-->
</style>
</head>
<body>
<table>
<tr>
<td class=xl2>17.02.2010</td>
<td class=xl3>4</td>
<td class=xl3>0</td>
</tr>
<tr>
</tr>
</table>
</body>
</html>
1
  • I know this isn't the solution that you are looking for, but recently we supported export to excel from the database by simply creating a tab-delimited text file and saving it in the tmp directory on the server and then returning that text file in the HTTP response. Excel already supports the parsing of tab- and comma-delimited text files into excel worksheets Commented Sep 15, 2012 at 1:03

2 Answers 2

2

I use this form :

using(System.IO.MemoryStream ms = /*Include Excel File*/) {
  ControllerContext.HttpContext.Response.Clear();
  ControllerContext.HttpContext.Response.AddHeader("cache-control", "private");
  ControllerContext.HttpContext.Response.AddHeader("Content-disposition", "attachment; filename=" + filename + ";");
  ControllerContext.HttpContext.Response.AddHeader("Content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  ms.WriteTo(ControllerContext.HttpContext.Response.OutputStream);
}
return null;
Sign up to request clarification or add additional context in comments.

Comments

0

How about just generating a .csv? Why do you need a full-blown native Excel file? Another easy option is to just generate HTML content and then set the output content type to excel. Excel will open it and format it, its a quick shortcut, but it works.

1 Comment

customer require xlsx file and about html option that's what the example above does but it work for xls not xlsx file I get the above error when I try to open the excel file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.