WebControl(DataGrid/GridView)导出Excel、Word

原创 2012年03月26日 10:37:49

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

 

相关文章推荐

asp.net中将DataGrid数据导出到excel或word文件中

private void Page_Load(object sender, System.EventArgs e) { SqlConnection con=...

GridView导出到Excel或Word文件

在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户)网上查话费的页面中就有一个导出到Excel的功能,光大网上银行查看历史明细也有这些功能......

GridView导出到Excel或Word文件

在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户)网上查话费的页面中就有一个导出到Excel的功能,光大网上银行查看历史明细也有这些功能......
  • cz_2012
  • cz_2012
  • 2012年11月07日 18:21
  • 423

带分页功能的GridView导出Excel或Word的方法

http://www.coridc.com/archives/889.html 首先需要在后台代码页加上命名空间: using System.IO; using System.Text ...
  • lJean
  • lJean
  • 2011年08月09日 11:54
  • 727

DataTable、GridView、DataList导出至Word或Excel(已验证)

http://www.cnblogs.com/insus/archive/2009/02/28/1400266.html http://www.cnblogs.com/insus/archive/2...

ASP.NET 导出Panel、GridView控件到Excel或Word

导出效果图: //调用方法,导出Excel protected void btnExcel_Click(object sender, System.EventArgs e) { ...

将GridView中的数据导出到Excel / Word中

在开发ASP.NET web应用程序时,我们很常用到GridView控件来显示数据,那我们怎样才能将GridView中的数据导出到Excel或者Word中呢? 前提条件: 1、你的GridVi...

c# gridview操作:绑定特定列、导出Excel、导出WORD

开始时间 结束时间 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:WebControl(DataGrid/GridView)导出Excel、Word
举报原因:
原因补充:

(最多只允许输入30个字)