MVC导出Excel .xlsx提示文件格式或文件扩展名无效

WXW997665163 2013-02-01 11:09:24
请各位高手指教,MVC导出Excel时导出.xlsx正常,但导出.xlsx就提示文件格式或文件扩展名无效。
不知道要导出.xlsx需要什么格式?请各位指正...谢谢大家!
 public void ExportToExcel()
{
System.IO.StringWriter sw = new System.IO.StringWriter();

string str="<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\"><DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\"><Author>User</Author><LastAuthor>User</LastAuthor><Created>2012-10-10T11:18:43Z</Created> <Company>微软中国</Company><Version>11.5606</Version></DocumentProperties><ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\"><WindowHeight>9225</WindowHeight><WindowWidth>18180</WindowWidth><WindowTopX>240</WindowTopX> <WindowTopY>120</WindowTopY><ProtectStructure>False</ProtectStructure><ProtectWindows>False</ProtectWindows></ExcelWorkbook><Styles><Style ss:ID=\"Default\" ss:Name=\"Normal\"><Alignment ss:Vertical=\"Center\"/> <Borders/><Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/> <Interior/> <NumberFormat/> <Protection/> </Style></Styles><Worksheet ss:Name=\"Sheet1\"><Table ss:ExpandedColumnCount=\"1\" ss:ExpandedRowCount=\"1\" x:FullColumns=\"1\" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"> <Row> <Cell><Data ss:Type=\"String\">fdfd</Data></Cell> </Row> </Table><WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\"><Selected/><ProtectObjects>False</ProtectObjects><ProtectScenarios>False</ProtectScenarios></WorksheetOptions></Worksheet></Workbook>";
//string str = ConvertEntity().ToString();
//string str="<table><tr><td>品名</td><td>最高价格</td><td>最低价格</td><td>平均价格</td><td>计量单位</td><td>备注</td></tr><tr><td>青菜</td><td> </td><td> </td><td> </td><td>元/公斤</td><td> </td></tr></table>";
sw.Write(str);
sw.Close();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
//HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xlsx");
HttpContext.Current.Response.Write(sw);
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
}
...全文
842 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
WXW997665163 2013-02-04
  • 打赏
  • 举报
回复
引用 5 楼 webdiyer 的回复:
引用 4 楼 WXW997665163 的回复: 引用 2 楼 webdiyer 的回复:这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了 你好,这个体验不太好吧? 请问还有没有其他方法实现?谢谢关注。 导出为xls文件用excel 2003打开就没有提示,2007以上就会出现提示。
你好,导出Excel .xlsx提示文件格式或文件扩展名无效。打不开导出的Excel,不是提示格式与文件扩展名指定的格式不一致,所以不能呈现导出的数据。请问这种情况是我的代码哪里指定的参数不正确吗?
xiongxyt2 2013-02-04
  • 打赏
  • 举报
回复
你就到处xls格式的啊 打不开的话到Excel设置里面Excel选项---加载项里面设置
jhdxhj 2013-02-04
  • 打赏
  • 举报
回复
导出为xls文件用excel 2003打开就没有提示,2007以上就会出现提示。
webdiyer 2013-02-01
  • 打赏
  • 举报
回复
这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了
bdmh 2013-02-01
  • 打赏
  • 举报
回复
你不是你机器上没有安装office2007+
webdiyer 2013-02-01
  • 打赏
  • 举报
回复
引用 4 楼 WXW997665163 的回复:
引用 2 楼 webdiyer 的回复:这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了 你好,这个体验不太好吧? 请问还有没有其他方法实现?谢谢关注。
导出为xls文件用excel 2003打开就没有提示,2007以上就会出现提示。
WXW997665163 2013-02-01
  • 打赏
  • 举报
回复
引用 2 楼 webdiyer 的回复:
这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了
你好,这个体验不太好吧? 请问还有没有其他方法实现?谢谢关注。
WXW997665163 2013-02-01
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
你不是你机器上没有安装office2007+
你好,装了2010.

62,052

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧