当前位置:编程学习 > C#/ASP.NET >>

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

请各位高手指教,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();
        }
excel office mvc --------------------编程问答-------------------- 你不是你机器上没有安装office2007+ --------------------编程问答-------------------- 这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了 --------------------编程问答--------------------
引用 1 楼 bdmh 的回复:
你不是你机器上没有安装office2007+

你好,装了2010. --------------------编程问答--------------------
引用 2 楼 webdiyer 的回复:
这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了

你好,这个体验不太好吧? 请问还有没有其他方法实现?谢谢关注。 --------------------编程问答--------------------
引用 4 楼 WXW997665163 的回复:
引用 2 楼 webdiyer 的回复:这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了
你好,这个体验不太好吧? 请问还有没有其他方法实现?谢谢关注。

导出为xls文件用excel 2003打开就没有提示,2007以上就会出现提示。 --------------------编程问答--------------------
引用 5 楼 webdiyer 的回复:
引用 4 楼 WXW997665163 的回复:
引用 2 楼 webdiyer 的回复:这个提示有点烦,但我也没找到解决办法,让客户下载后打开另存为一下就好了
你好,这个体验不太好吧? 请问还有没有其他方法实现?谢谢关注。
导出为xls文件用excel 2003打开就没有提示,2007以上就会出现提示。


你好,导出Excel .xlsx提示文件格式或文件扩展名无效。打不开导出的Excel,不是提示格式与文件扩展名指定的格式不一致,所以不能呈现导出的数据。请问这种情况是我的代码哪里指定的参数不正确吗? --------------------编程问答-------------------- 导出为xls文件用excel 2003打开就没有提示,2007以上就会出现提示。 --------------------编程问答-------------------- 你就到处xls格式的啊  打不开的话到Excel设置里面Excel选项---加载项里面设置
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,