我可以从一个内存流做了一个ASP.NET页面的Excel文件? [英] Can I download an Excel file made from a memory stream off an ASP.NET page?
问题描述
我有一个ASP.NET页面,用户提供了一个ID,然后我们从数据库拉一些数据,并把它变成一个Excel US preadsheet。我想在内存中创建的Excel文件,然后让用户下载文件。我可以在服务器上创建一个文件,然后将其删除之后,但它似乎没有必要。根据差错处理我可能孤儿与方法等文件。
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?
在一个侧面说明,我使用 EPPlus 作为一个API(快速插上它)。
On a side note, I'm using EPPlus as an API (quick plug for it).
推荐答案
您想指定内容类型
和内容dispisition
头像这样 - <一个href=\"http://stackoverflow.com/questions/4959732/response-contenttype-application-vnd-ms-excel-works-in-ie-and-firefox-but-no\">Response.ContentType = QUOT;应用程序/ vnd.ms - Excel中&QUOT;在IE和Firefox,但不是在Safari ,然后流文件。一旦完成后,调用到Response.End()
停止应用程序执行
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样品:</ P>
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屋!