MVC4:使用chrome将.xlsx文件导出为mvc。 它适用于.xls但不适用于.xlsx

如何使用chrome通过mvc将.xlsx文件导出为ex​​cel。 它适用于.xls但不适用于.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(); 

当我打开excel文件。 它表现得像这样。 “Excel无法打开文件’FileName.xlsx’,因为文件格式或文件扩展名无效。”

非常感谢你的帮助! 🙂

尝试更改.xlsx内容类型

 Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 

您应该使用正确的MIME类型。 对于DOCX,是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

有关更多详细信息,请参阅此博客文章和此答案