X龙@China .Net 'blog

需要的不仅仅是工作,而是通过努力得来的美好将来。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c# 导出excel(将数据直接输出到响应)

Posted on 2009-06-30 11:55  X龙  阅读(628)  评论(0编辑  收藏  举报

 

        public void ExportExcel()
        {
            Response.Clear();
            Response.Buffer 
= true;
            
//Response.Charset="utf-8";
            
            Response.AppendHeader(
"Content-Disposition""attachment;filename=myExcel.xls");           
            Response.ContentEncoding 
= System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
            Response.Write("<meta http-equiv=Content-Type content=application/vnd.ms-excel;charset=gb2312>");//设置输出文件类型为excel文件。 

            
this.EnableViewState = false;

            System.Globalization.CultureInfo myCItrad 
= new System.Globalization.CultureInfo("ZH-CN"true);
            System.IO.StringWriter oStringWriter 
= new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter 
= new System.Web.UI.HtmlTextWriter(oStringWriter);
            
this.gridXls.RenderControl(oHtmlTextWriter);  
            Response.Write(oStringWriter.ToString());
            Response.End();
        }

       //重写Page的下面方法.

        public override void VerifyRenderingInServerForm(Control control)
        {
            if (!control.GetType().Equals(gridXls.GetType()))
            {
                base.VerifyRenderingInServerForm(control);
            }
        }


 

点击这里给我发消息http://wp.qq.com/index.html