MVC3 C# Export to Excel

家住魔仙堡 提交于 2019-12-31 05:20:06

问题


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



回答2:


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.



来源:https://stackoverflow.com/questions/12433608/mvc3-c-sharp-export-to-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!