下载的excel文件无法打开

时间:2014-01-29 10:18:15

标签: c# asp.net .net

我正在尝试将excel文件下载到本地系统中的位置,但在打开文件时,我收到错误:

  

文件格式或扩展名不匹配。

虽然后端的文件具有相同的扩展名,但我收到错误 PFB代码:

string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename="+downloadedFileName);


downloadedFileName= "Myfile.xlsx"

2 个答案:

答案 0 :(得分:0)

它将保存为details.xls

Response.ClearContent();
 Response.Buffer = true;
   Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Details.xls"));
    Response.ContentType = "application/ms-excel";

答案 1 :(得分:0)

您遇到此错误,因为您没有使用Response.TransmitFile()。你的代码应该是:

string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename="+downloadedFileName);
Response.TransmitFile(FilePath); // full path here