将数据控件(如GridView)的内容转化成Excel格式文件

翻译|其它|编辑:郝浩|2007-10-16 16:48:57.000|阅读 1223 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

        Response.Clear();  //清空输出流
        Response.Buffer = true; //开启缓冲输出 
        Response.Charset = "GB2312";  //输出字符集中文
        Response.AppendHeader("Content-Disposition", "attachment;filename=你要存成的文件名字.xls");
        // 如果设置为 GetEncoding("GB2312");否则导出的文件将会出现乱码!!!
      
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";//设置输出文件类型为  excel  文件。
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GridView1.RenderControl(oHtmlTextWriter);    //GridView  内容输出到流,其他数据组件类似
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();

 

      注意:另外一定要加上下面的方法  这个是重写的注册方法,否则会报错
      public override void VerifyRenderingInServerForm(Control control)
     {
     }


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP