ASP.NET AJAX MSSQL MYSQL 技术交流区

Bullion技术交流BLOG

博客园 首页 新随笔 联系 订阅 管理
  17 Posts :: 4 Stories :: 51 Comments :: 1 Trackbacks

公告

2009年4月16日 #

private void DataTable2Excel(System.Data.DataTable dtData)
{
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();
            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
 }
posted @ 2009-04-16 12:01 bullion 阅读(752) 评论(0) 编辑