C# 我可以从 ASP.NET 页面下载由内存流制作的 Excel 文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9777185/
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:49:42  来源:igfitidea点击:

Can I download an Excel file made from a memory stream off an ASP.NET page?

c#asp.netexcelepplus

提问by Andrew Dunaway

I have an ASP.NET page where a user provides an ID, and then we pull some data from the DB and put it into an Excel spreadsheet. I would like to create the Excel file in memory and then allow the user to download the file. I could create a file on the server, and then delete it afterwards, but it seems unnecessary. Depending on error handling I could potentially orphan a file with that approach, etc.

我有一个用户提供 ID 的 ASP.NET 页面,然后我们从数据库中提取一些数据并将其放入 Excel 电子表格中。我想在内存中创建 Excel 文件,然后允许用户下载该文件。我可以在服务器上创建一个文件,然后删除它,但似乎没有必要。根据错误处理,我可能会使用这种方法孤立一个文件,等等。

Is something like this possible? Or do I need to use a file stream?

这样的事情可能吗?还是我需要使用文件流?

On a side note, I'm using EPPlusas an API (quick plug for it).

附带说明一下,我使用EPPlus作为 API(快速插件)。

采纳答案by dana

You want to specify the content-typeand content-dispisitionheaders like so - Response.ContentType = "application/vnd.ms-excel" works in IE and firefox but not in Safari, then stream your file. Once complete, call Response.End()to stop the application execution

您想像这样指定content-typecontent-dispisition标题 - Response.ContentType = "application/vnd.ms-excel" 在 IE 和 firefox 中有效,但在 Safari 中无效,然后流式传输您的文件。完成后,调用Response.End()停止应用程序执行

Code Sample:

代码示例:

void StreamExcelFile(byte[] bytes)
{
    Response.Clear();
    Response.ContentType = "application/force-download";
    Response.AddHeader("content-disposition", "attachment; filename=name_you_file.xls");
    Response.BinaryWrite(bytes);
    Response.End();
}

回答by Khan

What you're looking for is called a generic handler:

您正在寻找的称为通用处理程序:

http://www.dotnetperls.com/ashx

http://www.dotnetperls.com/ashx

In a nutshell, what you need to do is define the context type. Which in your case will be a xls or xlsx. Set the response, and direct the user to the handler.

简而言之,您需要做的是定义上下文类型。在您的情况下将是 xls 或 xlsx。设置响应,并将用户定向到处理程序。

They will be prompted to download the file.

系统将提示他们下载文件。

回答by javram

Yes, look into using an HTTP Handler to stream the file to the browser from memory.

是的,考虑使用 HTTP 处理程序将文件从内存流式传输到浏览器。

http://msdn.microsoft.com/en-us/library/ms972953.aspx

http://msdn.microsoft.com/en-us/library/ms972953.aspx

回答by nawigate

ExcelPackage pck = new ExcelPackage();
.....
.....
.....

byte[] bfr = pck.GetAsByteArray();
Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-disposition", "attachment; filename=ExcelFileName.xlsx");

Response.OutputStream.Write(bfr, 0, bfr.Length);
Response.Flush();
Response.Close();