C# 下载 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9603151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:05:35  来源:igfitidea点击:

Download an Excel file

c#asp.net

提问by Mike

I have read some past posts here on how to download a Excel file from a website. So, I have setup the below code:

我在这里阅读了一些关于如何从网站下载 Excel 文件的过去帖子。所以,我已经设置了以下代码:

string path = MapPath(fname);
string name = Path.GetFileName(path);
string ext = Path.GetExtension(path);
string type = "application/vnd.ms-excel";


if (forceDownload)
{
    Response.AppendHeader("content-disposition",
        "attachment; filename=" + name);
}

if (type != "")
{
    Response.ContentType = type;
    Response.WriteFile(path);
    Response.End();
}

However, I get no download dialog box.

但是,我没有下载对话框。

I try this both in IE 8 and FireFox 10.0.2.
The file is there, it's not locked, and it's not set to read only.
I'm not sure were I went wrong.

我在 IE 8 和 FireFox 10.0.2 中都尝试过。
文件就在那里,没有被锁定,也没有设置为只读。
我不确定是不是我做错了。

采纳答案by Luiggi Mendoza

According to this link, you need to add this line:

根据此链接,您需要添加以下行:

strFileName = Path.GetFileName(path);
Response.TransmitFile( Server.MapPath(strFileName) );

This will cause a Open / Save As dialog box to pop up with the filename of SailBig.jpg as the default filename preset.

This of course assumes you're feeding a file that already exists. If you need to feed dynamically generated - say an image [or any file] that was generated in memory - you can use Response.BinaryWrite() to stream a byte array or write the output directly in Response.OutputStream.

这将导致弹出一个打开/另存为对话框,文件名 SailBig.jpg 作为默认文件名预设。

这当然假设您正在提供一个已经存在的文件。如果您需要动态生成 - 比如说在内存中生成的图像[或任何文件] - 您可以使用 Response.BinaryWrite() 来流式传输字节数组或直接在 Response.OutputStream 中写入输出。

EDIT:

编辑:

Microsoft's MSDN site has a detailed explanation about File Downloading. It includes both samples for Java and .Net applications, the concept is the same:

Microsoft 的 MSDN 站点有关于File Downloading的详细说明。它包括 Java 和 .Net 应用程序的示例,概念是相同的:

  1. Get the response.
  2. With the response:
    1. Set the content type to "APPLICATION/OCTET-STREAM" (it means there's no application to open the file).
    2. Set the header to "Content-Disposition", "attachment; filename=\"" + + "\"".
    3. Write the file content into the response.
  3. Close the response.
  1. 得到回应。
  2. 随着回应:
    1. 将内容类型设置为“APPLICATION/OCTET-STREAM”(这意味着没有应用程序可以打开文件)。
    2. 将标题设置为“Content-Disposition”、“attachment; filename=\”” + + “\””。
    3. 将文件内容写入响应。
  3. 关闭响应。

So, looking at the MSDN ASP.Net file download, you're lacking the 2.3 step. You're just writing the file name to the response.

因此,查看 MSDN ASP.Net 文件下载,您缺少 2.3 步骤。您只是将文件名写入响应。

// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new
    System.IO.FileInfo("C:\downloadJSP\DownloadConv\myFile.txt");
Response.Flush();
Response.WriteFile(fileToDownload.FullName);

With this example you will download your file successfully, of course if you can get the file with no problems :).

通过此示例,您将成功下载文件,当然,如果您可以毫无问题地获取文件:)。

EDIT 2:

编辑2:

The HTML component used to download any file must be a regular HTML Request. Any ajax request to download a file won't work. Microsoft explains that here. And the main quote:

用于下载任何文件的 HTML 组件必须是常规 HTML 请求。任何下载文件的 ajax 请求都不起作用。微软在这里解释了这一点。和主要报价:

Its impossible to attach an event before and after a download through javascript. Browser doesn't allow this type of events for security reasons.

在通过 javascript 下载之前和之后附加事件是不可能的。出于安全原因,浏览器不允许此类事件。

回答by Diodeus - James MacFarlane

You need to send this before the file attachment header:

您需要在文件附件标题之前发送:

Response.ContentType = "application/vnd.ms-excel"

See: Export data to excel file from Classic ASP failing

请参阅:将数据从经典 ASP 导出到 excel 文件失败

回答by Aleksej Vasinov

Try adding such HTTP headers

尝试添加此类 HTTP 标头

Content-Type: application/force-download
Content-Type: application/vnd.ms-excel
Content-Type: application/download