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

  • 相关阅读:
    重新整理 .net core 实践篇————配置系统之盟约[五]
    重新整理 .net core 实践篇————依赖注入应用之援军[四]
    重新整理 .net core 实践篇————依赖注入应用之生命法则[三]
    重新整理 .net core 实践篇————依赖注入应用[二]
    重新整理 .net core 实践篇————配置应用[一]
    spring cloud 学习笔记 客户端(本地)均衡负载(三)
    Leetcode之插入区间
    Leetcode之两棵二叉搜索树中的所有元素
    Leetcode之二叉树的层序遍历
    LeetCode之验证二叉搜索树
  • 原文地址:https://www.cnblogs.com/dashi/p/4034716.html
Copyright © 2011-2022 走看看