Asp.net WebForm使用NPOI导出Excel

废话不多说,直接上代码

public void ExportExcel()
        {
                string datefrom = Request.QueryString["datefrom"];
                string dateto = Request.QueryString["dateto"];
                string ID = Request.QueryString["ID"];

                Report_AgentModel model = new Report_AgentModel();

                string strSql = model.getExcelSql(datefrom, dateto, ID);
                string sort = "createtime desc";
                string sheetName = "Report_Agent";

                ExcelHelper help = new ExcelHelper();
                ExcelHelper.NpoiMemoryStream ms = help.ExportExcel(sheetName, strSql, sort);

                //输出Excel
                string filename = "Report_Agent_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";

                Response.Clear();
                Response.ContentType = "application/vnd.ms-excel";
                Response.AddHeader("Content-Length", ms.Length.ToString());
                Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}",       Server.UrlEncode(filename)));

                Response.BinaryWrite(ms.GetBuffer());
                Response.Flush();
                Response.End();

        }

在这里插入图片描述

发布了11 篇原创文章 · 获赞 6 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览