zoukankan      html  css  js  c++  java
  • DATATABLE、GRIDVIEW內容导出到EXCEL表中

    几个实例,其中DATATABLE导出时也是先绑定GRIDVIEW再从其中导出。
    例一:
       DataTable dt = db.GetData(strTMP);

           HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
           HttpContext.Current.Response.Charset ="UTF-8";   
           HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
           HttpContext.Current.Response.ContentType ="application/ms-excel";
           System.IO.StringWriter  tw = new System.IO.StringWriter() ;
           System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);

           GridView GV = new GridView();//一个无分页的GridView
           GV.DataSource = dt;
           GV.AllowPaging = false;
           GV.DataBind();
           GV.RenderControl(hw);
           HttpContext.Current.Response.Write(tw.ToString());
           HttpContext.Current.Response.End();

    例二:
       

       HttpContext curContext = System.Web.HttpContext.Current;
            System.IO.StringWriter strWriter = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            curContext.Response.Charset = "GB2312";

            GridView GV = new GridView();//一个无分页的GridView
            GV.DataSource = DT;//绑定DATATABLE
            GV.AllowPaging = false;
            GV.DataBind();
            GV.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();

  • 相关阅读:
    使用T4模板生成POCO类
    MiniProfiler工具介绍
    程序集和反射(C#)
    按自己的想法去理解事件和泛型(C#)
    WebAPI性能优化之压缩解压
    那些年困扰我们的委托(C#)
    HTML5笔记2——HTML5音/视频标签详解
    HTML5笔记1——HTML5的发展史及标签的改变
    工作中常用的js、jquery自定义扩展函数代码片段
    记一次.NET代码重构
  • 原文地址:https://www.cnblogs.com/vic_lu/p/1820304.html
Copyright © 2011-2022 走看看