无法打开导出的xlsx文件

我试图出口到EXCEL与XLSX格式,但得到这个错误,当试图打开文件。 由于文件格式或文件扩展名无效,Excel无法打开“CertificationMetricsReport.xlsx”文件。 validation文件是否已损坏,文件扩展名是否与文件的格式相匹配。

如果我将提供文件名CertificationReport.xls然后我能够打开它。

这是代码片段

public void RenderedReportContent(byte[] fileContent) { try { Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("Content-Length", fileContent.Length.ToString()); Response.AddHeader("Content-Disposition", "attachment; filename="CertificationReport.xlsx"); Response.BinaryWrite(fileContent); Response.End(); } catch (Exception ex) { if (!(ex is System.Threading.ThreadAbortException)) { //Other error handling code here } } } 

当我试图下载CertificationMetricsReport.xls时,它工作正常。

请帮我下载xlsx文件。

也许你可以尝试改变内容types

to contenttype =“application / vnd.ms-excel”; 或应用程序/ octect-stream

  Response.Clear(); Response.Buffer = True; Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.xslx" Response.BinaryWrite(YourFile); Response.End(); 

你也可以尝试使用response.flush

在最后尝试在其他浏览器,如IE浏览器,Chrome浏览器,Firefox …

编辑: http ://social.msdn.microsoft.com/forums/en-US/3d539969-51c4-42bc-8289-6d70d8db7aff/prompt-while-downloading-a-file-for-open-or-save EDIT2: How在HttpResponse中发送文件?