zoukankan      html  css  js  c++  java
  • 将DataTable导出到Excel

       /// <summary>        

    /// 导出Excel        

    /// </summary>        

    /// <param name="dtData"></param>        

    public void DataTable2Excel(System.Data.DataTable dtData)        

    {            

             System.Web.UI.WebControls.DataGrid dgExport = null;            

             // 当前对话            

             System.Web.HttpContext curContext = System.Web.HttpContext.Current;            

             // IO用于导出并返回excel文件            

               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 = "";

                    // 导出excel文件

                    strWriter = new System.IO.StringWriter();

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

                    // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid

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

                }

            }

  • 相关阅读:
    南阳33(蛇形填数)规律题;
    南阳241(字母统计)
    南阳57(6174问题)
    android图形基础知识
    Linux中yum手动安装、手动建立仓库文件夹关联实现关联包自动安装、yum相关命令使用
    debug连线指令
    Qt之信号连接,你Out了吗?
    hdu-4607-Park Visit
    MySQL 分区表 partition线上修改分区字段,后续进一步学习partition (1)
    如何用正则将多个空格看成一个空格结合spllit()方法将文本数据入库
  • 原文地址:https://www.cnblogs.com/conghua/p/3446504.html
Copyright © 2011-2022 走看看