当前位置:网站首页>Asp.net WebForm使用NPOI导出Excel

Asp.net WebForm使用NPOI导出Excel

2022-06-25 13:16:00 全栈程序员站长

大家好,又见面了,我是你们的朋友全栈君。

废话不多说,直接上代码

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();

        }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/151689.html原文链接:https://javaforall.cn

原网站

版权声明
本文为[全栈程序员站长]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/2030068