将数据库中的表以excel或doc形式保存到客户端

DataTable dt = SQL.OperateSQL.GetDatatable();
        if ( dt != null)
        {
            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;

            //curContext.Response.ContentType="application/vnd.ms-excel";
            curContext.Response.ContentType = "application/msword";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

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

            dgExport = new DataGrid();
            dgExport.DataSource = dt.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }

首先从数据库中映射至内存中的一张数据表,然后通过浏览器以不同形式的流输出。

posted on   画一个圆圈  阅读(135)  评论(0编辑  收藏  举报

< 2009年10月 >
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7

导航

统计

点击右上角即可分享
微信分享提示