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



     

  • 相关阅读:
    实现JavaScript自定义函数的整合、链式调用及类的封装
    jQuery事件
    jQuery特效
    jQuery基础细节
    CSS3盒模型
    主流清浮动方法
    JavaScript 执行环境及作用域
    JavaScript 参数传递与变量复制
    PHP雪花背景验证码
    Linkis 0.9.2 版本发布
  • 原文地址:https://www.cnblogs.com/wisdom/p/617687.html
Copyright © 2011-2022 走看看