我正在尝试将数据从数据库导出到 Excel 2007 文件。
我只想将 html 文件的标题更改为 excel 2007 文件。
我将数据格式化为表格并将标题更改为:
Response.AddHeader("Content-Disposition", "attachment;filename= filename.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
我不断收到同样的错误:
“Excel 无法打开文件“filename.xlsx”,因为文件格式为
文件扩展名无效。验证该文件未被
已损坏且文件扩展名与文件格式匹配”
我也尝试了我在网上找到的这个例子,我可以在 excel 2003 中打开并显示一条警告消息,但在 2007 年我收到上述错误消息。它需要使它与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>
我使用这种形式:
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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)