zoukankan      html  css  js  c++  java
  • asp.net中将DataGrid数据导出到excel或word文件中

    private void Page_Load(object sender, System.EventArgs e)
            {
                SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
                con.Open();
                SqlDataAdapter sda=new SqlDataAdapter();
                sda.SelectCommand=new SqlCommand("select * from txtInsert",con);
                DataSet ds=new DataSet();
                sda.Fill(ds,"emp");
                this.DgSource.DataSource=ds.Tables["emp"];
                this.DgSource.DataBind();
                con.Close();
            }

    public void DataGridToExcel(DataGrid grdTemp,DataSet dsTemp)
            {
                grdTemp.AllowPaging=false;   //设置不能分页

                grdTemp.DataSource=dsTemp;  //重新绑定数据源
                grdTemp.DataBind();
      
                //常规导出方法

                System.IO.StringWriter SW = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW);
                grdTemp.RenderControl(HTW);

                //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
                Response.Buffer=true;
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/vnd.ms-excel";
                //Response.ContentType是输出流的 HTTP MIME 类型
                //Response.ContentType     --- word文件
                //application/vnd.ms-excel --- excel文件
                //
                Response.Charset="utf-8";
                Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
                Response.AddHeader("Content-Disposition", "attachment;filename=aaa.xls");
                //attachment --- 作为附件下载
                //inline --- 在线打开
                //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
                //进行进行编码,以解决文件名乱码的问题
                Response.Write(SW.ToString());
                Response.Flush();
                Response.Close();
            }

    private void Button1_Click(object sender, System.EventArgs e)
            {
                SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
                con.Open();
                SqlDataAdapter sda=new SqlDataAdapter();
                sda.SelectCommand=new SqlCommand("select * from txtInsert",con);           
                DataSet ds=new DataSet();
                sda.Fill(ds,"emp");
                this.DgSource.DataSource=ds.Tables["emp"];           
                this.DataGridToExcel(this.DgSource,ds);
                con.Close();
            }

  • 相关阅读:
    生成类似于MongoDB产生的ObjectId
    链式编程:遇到多个构造器参数(Constructor Parameters)时要考虑用构建器(Builder)
    mysql时间字符串按年/月/天/时分组查询 -- date_format
    根据模板导出excel
    九度 1188 约瑟夫环问题
    快速排序
    Linux 命令
    volatile小记
    线程池ThreadPoolExecutor
    CyclicBarrier、CountDownLatch与Semaphore的小记
  • 原文地址:https://www.cnblogs.com/dekevin/p/2548896.html
Copyright © 2011-2022 走看看