码迷,mamicode.com
首页 > Web开发 > 详细

asp.net文件流下载的代码摘要

时间:2017-12-12 22:21:45      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:ase   tty   clear   编码   string   tab   ice   table   his   

try
            {

                var workbook = new XLWorkbook();
                if (Workbook != null)
                {
                    workbook = Workbook;
                }
                if (this.ExportData != null || this.Workbook != null)
                {
                    context.HttpContext.Response.Clear();

                    // 编码
                    context.HttpContext.Response.ContentEncoding = Encoding.UTF8;

                    // 设置网页ContentType
                    context.HttpContext.Response.ContentType =
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

                    // 导出名字
                    var browser = context.HttpContext.Request.Browser.Browser;
                    var exportFileName = browser.Equals("Firefox", StringComparison.OrdinalIgnoreCase)
                        ? this.FileName
                        : HttpUtility.UrlEncode(this.FileName, Encoding.UTF8);

                    context.HttpContext.Response.AddHeader(
                        "Content-Disposition",
                        string.Format("attachment;filename={0}", exportFileName));

                    // Add all DataTables in the DataSet as a worksheets
                    if (ExportData != null)
                        workbook.Worksheets.Add(this.ExportData, this.SheetName);

                    using (var memoryStream = new MemoryStream())
                    {
                        workbook.SaveAs(memoryStream);
                        memoryStream.WriteTo(context.HttpContext.Response.OutputStream);
                        memoryStream.Close();
                    }
                }
                workbook.Dispose();
            }
            catch
            {
                throw;
            }

  

asp.net文件流下载的代码摘要

标签:ase   tty   clear   编码   string   tab   ice   table   his   

原文地址:http://www.cnblogs.com/coolyylu/p/8028800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!