2012-05-03 61 views
1

如何使用chrome通过mvc将.xlsx文件导出为ex​​cel。它适用于.xls的但不是原来的.xlsx导出到Excel .xlsx文件

 Response.ClearContent(); 
     Response.AddHeader("content-disposition", "attachment; filename= Estimate1.xlsx"); 
     Response.ContentType = "application/excel"; 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter htw = new HtmlTextWriter(sw); 

     Response.Write(sw.ToString()); 
     Response.End(); 
+0

看看http://epplus.codeplex.com/ – user287107

+0

我们在我们的一个项目中使用EPPlus,它的效果很好。 –

+0

我还使用它,它的伟大工程有时也功能也一点点有限的,例如所有图表选项都不可用,或者如果写入超过1000000行,它不会返回错误。另外有时数字写错了方式,Excel然后抱怨破损的xls文件 – user287107

回答

2

检查您的MIME类型在IIS - 网络服务器是不知道的Office 2007(或更高版本)的文件扩展名,并拒绝为他们服务。

请参阅TechNet上的Register the 2007 Office system file format MIME types on servers关于此主题。

即使你不使用“真正的” IIS,你应该尝试添加XSLX MIME类型,你的web.config:

<configuration> 
    <system.webServer> 
     <staticContent> 
      <mimeMap fileExtension=".xslx" mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> 
     </staticContent> 
    </system.webServer> 
</configuration> 
+0

即时通讯使用本地主机asp.net不是IIS – TMan

+0

@TMan你的问题听起来很像你的网络服务器将知道.xls MIME类型,但错过了一个为.xslx。 – Filburt

+2

@Tman - Filburt指出,“application/excel”的“内容类型”对于xlsx文件不正确:您应该使用此处列出的那个:http://blogs.msdn.com/b/vsofficedeveloper/archive /2008/05/08/office-2007-open-xml-mime-types.aspx –