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

  • 相关阅读:
    STM32Cube Uart_DMA测试工程
    STM32CubeMX安装指南
    基于STM32Cube的ADC模数采样设计
    C++ this指针的用法
    用七段数码管显示26个字母的方案
    FPGA的引脚VCCINT 、VCCIO VCCA
    Keil环境中建立带FreeRTOS的STM32L项目
    STM32L时钟
    Mysql explain
    nginx屏蔽IP
  • 原文地址:https://www.cnblogs.com/dashi/p/4034716.html
Copyright © 2011-2022 走看看