可以从ASP.NET页面下载从内存流中创建的Excel文件吗? [英] Can I download an Excel file made from a memory stream off an ASP.NET page?
问题描述
有可能吗?还是需要使用文件流?
在旁注中,我使用 EPPlus 作为API(快速插件)。
内容类型
和内容分配
标题如下 - Response.ContentType =application / vnd.ms-excel在IE和Firefox中工作,但不在Safari中,然后流式传输您的文件。一旦完成,请调用 Response.End()
以停止应用程序执行
代码示例:
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();
}
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.
Is something like this possible? Or do I need to use a file stream?
On a side note, I'm using EPPlus as an API (quick plug for it).
You want to specify the content-type
and content-dispisition
headers 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
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();
}
这篇关于可以从ASP.NET页面下载从内存流中创建的Excel文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!