下载文件,ClosedXML [英] Download file with ClosedXML
问题描述
所有
我如何可以下载一个文件,使用户可以看到它正在下载(如甲流?)
我目前使用的ClosedXML,但如果我使用SaveAs方法,我得给一个硬codeD URL,如果我只给文件名不会自动下载到下载文件夹中。
下面的方法的伟大工程,但我必须创建自己的Excel文件,该文件是基于HTML,并将该文件的方式增长比它应该过大,当我使用ClosedXML将文件从只有50%或更低低于code尺寸:
但是,下载的行为是怎么想的那样。
有没有一种方法,我可以转换code以下,所以我可以给我的工作簿作为一个对象,它只是下载此工作簿?
<$p$p><$c$c>HttpContext.Current.Response.AppendHeader(\"Content-Disposition\",\"attachment;filename=Excel.xls\");HttpContext.Current.Response.Charset =UTF-8;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType =应用程序/ MS-Excel的;
ctl.Page.EnableViewState = FALSE;
System.IO.StringWriter TW =新System.IO.StringWriter();
System.Web.UI.HtmlTextWriter HW =新System.Web.UI.HtmlTextWriter(TW);
ctl.RenderControl(HW);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
感谢
的另存为()
方法支持流,从而获得ClosedXml工作簿,因为我用一个流:
公共流GetStream(XLWorkbook excelWorkbook)
{
流FS =新的MemoryStream();
excelWorkbook.SaveAs(FS);
fs.Position = 0;
返回FS;
}
然后下载该文件:
字符串MYNAME = Server.UrlEn code(REPORTNAME +_+ DateTime.Now.ToShortDateString()+的.xlsx);
MemoryStream的流= GetStream(ExcelWorkbook);Response.Clear();
将Response.Buffer =真;
Response.AddHeader(内容处置,附件;文件名=+ MYNAME);
Response.ContentType =应用程序/ vnd.ms-EXCEL;
Response.BinaryWrite(stream.ToArray());
到Response.End();
All
How can I download a file so the user sees that it is downloading (like with a stream?)
I am currently using ClosedXML, but if I use the SaveAs method, I have to give a hard-coded URL, and if I just give the file name it does not automatically download to the download folder.
The method below works great, but I have to create my own excel file, which is based upon HTML, and the file grows way too large than it should, when I use ClosedXML the file is only 50% or less from the size of the code below: However, the download behaviour is how I would like it to be.
Is there a way I can convert the code below so I can give my 'workbook' as an object, and it just downloads this workbook?
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
Thanks
The SaveAs()
method supports stream, so to get the ClosedXml workbook as a stream I use:
public Stream GetStream(XLWorkbook excelWorkbook)
{
Stream fs = new MemoryStream();
excelWorkbook.SaveAs(fs);
fs.Position = 0;
return fs;
}
And then for downloading the file:
string myName = Server.UrlEncode(ReportName + "_" + DateTime.Now.ToShortDateString() + ".xlsx");
MemoryStream stream = GetStream(ExcelWorkbook);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + myName);
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(stream.ToArray());
Response.End();
这篇关于下载文件,ClosedXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!