技术,思考的艺术

ASP.net ,C#,VB.NET,SQL + B2B,SCM,CRM,ERP
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ExportToExcel函数

Posted on 2004-09-07 08:59  1werwerfer  阅读(974)  评论(0)    收藏  举报
public void ExportToExcel(System.Web.UI.Control ctl)
        {
            bool CurrCtlVisible=ctl.Visible;
            ctl.Visible=true;        Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls"); 
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
           
            ctl.Page.EnableViewState = true;
            ctl.Visible=CurrCtlVisible;
        }