Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下载的excel文件未打开_C#_Asp.net_.net - Fatal编程技术网

C# 下载的excel文件未打开

C# 下载的excel文件未打开,c#,asp.net,.net,C#,Asp.net,.net,我正在尝试将excel文件下载到本地系统中的位置,但打开该文件时,出现错误: 文件格式或扩展名不匹配 虽然在后端的文件是相同的扩展名,但我得到的错误 PFB代码: string ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.ContentType = ContentType; Response.AppendHeader("Content-Disposition

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

文件格式或扩展名不匹配

虽然在后端的文件是相同的扩展名,但我得到的错误 PFB代码:

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


downloadedFileName= "Myfile.xlsx"

它将另存为details.xls

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

出现此错误是因为您没有使用
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

请确定:文件大小是否匹配?手动从服务器复制时是否可以打开文件?我没有看到
响应。写…
,你能粘贴代码吗?我想你没有提到文件扩展名。i、 e.xlsx或。xls@PatrickHofman:文件已保存到本地系统。当我双击它打开时,我得到的错误不在代码中。后端的文件很好。您必须为
.xlsx.
设置
ContentType
application/vnd.openxmlformats oficedocument.spreadsheetml.sheet
,为
.xls
@Taosique设置
application/vnd.ms excel
:我只为.xlsx设置了tanks Taosique,但是你能修改代码吗?这样我就可以问用户文件保存的位置。@Aquarius24:我想你是在问客户端的问题。这正是您的浏览器决定使用的方式。例如,Chrome和IE会将其保存在下载文件夹中。这是无法从服务器控制的。对<代码>响应。TransmitFile()获取服务器上文件的路径。