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



     

  • 相关阅读:
    JavaScripts广告轮播图以及定时弹出和定时隐藏广告
    JavaScript正则表达
    表单常用标签 和 属性
    html框架集
    Hbuilder 快捷键
    css 图片
    html input accept类型
    db2 sql
    js 数组排序
    html input size maxlength
  • 原文地址:https://www.cnblogs.com/wisdom/p/617687.html
Copyright © 2011-2022 走看看