zoukankan      html  css  js  c++  java
  • 将gridView中的数据导出 并保存到excel中

     

    protected void Button1_Click( object sender, System.EventArgs e )
    {

          Response.Clear();     //清楚缓冲区流中内容
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.Buffer = true;        //设为缓冲输出
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.Charset = "GB2312";
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
                                       //导出Excel格式,其中filename为Excel文件的名字,可根据要求定义
    ASP.NET中将GridView 导出到Excel 文件中(图四)    // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.ContentEncoding = System.Text.Encoding.UTF_8;  //设置输出流为简体中文
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
    ASP.NET中将GridView 导出到Excel 文件中(图四)    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    ASP.NET中将GridView 导出到Excel 文件中(图四)    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    ASP.NET中将GridView 导出到Excel 文件中(图四)    this.GridView1.RenderControl(oHtmlTextWriter);
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.Output.Write(oStringWriter.ToString());
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.Flush();   //刷新设计器加载程序的挂起更改
    ASP.NET中将GridView 导出到Excel 文件中(图四)    Response.End();
            }


    如果是将内容导出到word 
    则将上面的两行改为:
       Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.doc");
       Response.ContentType = "application/ms-word"; 
    就ok 了。
                                                                                             miss wang



     

  • 相关阅读:
    P3391 【模板】文艺平衡树(Splay)
    P4198 楼房重建
    P1491 集合位置
    P3957 跳房子
    P4016 负载平衡问题
    bzoj1077: [SCOI2008]天平 差分约束
    bzoj1151: [CTSC2007]动物园zoo 状压dp
    bzoj1076: [SCOI2008]奖励关 状压dp
    bzoj1226: [SDOI2009]学校食堂Dining 状压dp
    bzoj1879: [Sdoi2009]Bill的挑战 状压dp
  • 原文地址:https://www.cnblogs.com/wisdom/p/617687.html
Copyright © 2011-2022 走看看