MVC3 C#导出到Excel [英] MVC3 C# Export to Excel

查看:134
本文介绍了MVC3 C#导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据从数据库导出到Excel 2007文件。

我只是想改变HTML文件的文件头,以Excel 2007文件。

我的数据格式化为一个表,并改变头这样的:

  Response.AddHeader(内容处置,附件;文件名= filename.xlsx);
Response.ContentType =应用/ vnd.openxmlformats-officedocument.s preadsheetml.sheet;
 

我不断收到同样的错误:

  

Excel无法打开文件filename.xlsx由于文件格式   文件扩展名是无效的。验证该文件一直没有   损坏,该文件扩展名的文件

的格式相匹配

我也试过这个例子,我在网上找到的,我可以在2003年Excel,将出现警告信息打开,但在2007年,我得到上面的错误消息。它需要使它与Excel2007的工作

 < HTML
的xmlns:O =瓮:架构 - 微软COM:办公室:办公室
的xmlns:X =瓮:架构 - 微软COM:办公室:EXCEL
的xmlns =htt​​p://www.w3.org/TR/REC-html40>
< HEAD>
< META HTTP-当量=内容类型内容=text / html的;字符集=窗口1252>
< META NAME =的ProgId含量= Excel.Sheet>
< META NAME =生成内容=Microsoft Excel中11>
&所述;! -  [如果GTE MSO 9]≥&其中;的xml>
 &所述; X:ExcelWorkbook>
  &所述; X:ExcelWorksheets>
   &所述; X:ExcelWorksheet>
    < X:名称>&工作表Sheet1 LT; / X:名称>
    &所述; X:WorksheetOptions>
     < X:选择/>
     < X:ProtectContents>假< / X:ProtectContents>
     < X:ProtectObjects>假< / X:ProtectObjects>
     < X:ProtectScenarios>假< / X:ProtectScenarios>
    < / X:WorksheetOptions>
   < / X:ExcelWorksheet>
  < / X:ExcelWorksheets>
  < X:ProtectStructure>假< / X:ProtectStructure>
  < X:ProtectWindows>假< / X:ProtectWindows>
< / X:ExcelWorkbook>
< / XML><![ENDIF]  - >
<风格>
&所述;! - 表
    {MSO的显示十进制分隔:;\。
    MSO的显示千位分隔符:;}
.xl2
    {
    MSO的数字格式:M / D / YY;
    左边框:.5pt固体;
    边框顶部:.5pt固体;
    右边框:.5pt固体;
    下边框:.5pt固体;
    }
.xl3
    {
    左边框:.5pt固体;
    边框顶部:.5pt固体;
    右边框:.5pt固体;
    下边框:.5pt固体;
    }
 - >
< /风格>
< /头>
<身体GT;
<表>
&其中; TR>
< TD类= XL2> 2010年2月17日< / TD>
&所述; TD类= XL3→4&所述; / TD>
&所述; TD类= XL3大于0&所述; / TD>
< / TR>
&其中; TR>
< / TR>
< /表>
< /身体GT;
< / HTML>
 

解决方案

我用这种形式:

 使用(System.IO.MemoryStream毫秒​​= / *包括Excel文件* /){
  ControllerContext.HttpContext.Response.Clear();
  ControllerContext.HttpContext.Response.AddHeader(缓存控制,私人);
  ControllerContext.HttpContext.Response.AddHeader(内容处置,附件;文件名=+文件名+;);
  ControllerContext.HttpContext.Response.AddHeader(内容型,应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet);
  ms.WriteTo(ControllerContext.HttpContext.Response.OutputStream);
}
返回null;
 

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>

解决方案

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;

这篇关于MVC3 C#导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆