简体   繁体   English

下载的Excel文件无法打开

[英]Downloaded excel file not opening

I am trying to download excel file to the location in local system but on opening the file, i am getting error: 我正在尝试将excel文件下载到本地系统中的位置,但是在打开文件时出现错误:

The file format or extension does not match. 文件格式或扩展名不匹配。

Although the file at backend is of same extension but i am getting the error PFB the code: 虽然后端的文件具有相同的扩展名,但我收到错误PFB代码:

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


downloadedFileName= "Myfile.xlsx"

It will save as details.xls 它将另存为details.xls

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

You have this error because you're not using Response.TransmitFile() . 因为没有使用Response.TransmitFile()所以出现了此错误。 Your code should be: 您的代码应为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM