zoukankan      html  css  js  c++  java
  • WebControl(DataGrid/GridView)导出Excel、Word

           /// <summary>
            ///
            /// </summary>
            /// <param name="dg"></param>
            /// <param name="name"></param>
            /// <param name="bm">编码</param>
            public static void OutExcel(System.Web.UI.Control dg, string name, string bm)
            {
                HttpResponse rsp = HttpContext.Current.Response;
                dg.Visible = true;
                rsp.Clear();
                rsp.Buffer = true;
                rsp.Charset = bm;
                name = "attachment;filename=" +HttpUtility.UrlPathEncode(name)+".xls";
                rsp.AppendHeader("Content-Disposition", name);
                rsp.ContentEncoding = System.Text.Encoding.GetEncoding(bm);
                rsp.ContentType = "application/ms-excel";
                dg.EnableViewState = false;
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

                Page page = new Page();
                page.EnableEventValidation = false;
                page.DesignerInitialize();
                HtmlForm form = new HtmlForm();
                page.Controls.Add(form);
                form.Controls.Add(dg);

                page.RenderControl(oHtmlTextWriter);
                rsp.Write(oStringWriter.ToString());
                rsp.End();
            }

            public static void OutWord(System.Web.UI.WebControls.WebControl dg, string name)
            {
                HttpResponse rsp = HttpContext.Current.Response;
                dg.Visible = true;
                rsp.Clear();
                rsp.Buffer = true;
                rsp.Charset = "utf-8";
                name = "attachment;filename=" + name;
                rsp.AppendHeader("Content-Disposition", name);
                rsp.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                rsp.ContentType = "application/ms-word";
                dg.EnableViewState = false;
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

                Page page = new Page();
                page.EnableEventValidation = false;
                page.DesignerInitialize();
                HtmlForm form = new HtmlForm();
                page.Controls.Add(form);
                form.Controls.Add(dg);

                form.RenderControl(oHtmlTextWriter);
                rsp.Write(oStringWriter.ToString());
                rsp.End();
            }

  • 相关阅读:
    [BZOJ3694]最短路
    [Usaco2011 Jan]道路和航线
    洛谷P1443马的遍历
    洛谷P1636学画画
    洛谷P1605走迷宫
    队列&广搜
    数论卷积公式and莫比乌斯反演
    数学之判断素数
    纯数学篇之欧拉定理证明
    筛素数
  • 原文地址:https://www.cnblogs.com/dashi/p/4034716.html
Copyright © 2011-2022 走看看