我是菜鸟,看到这些代码不怎么懂,麻烦高手解读一下好么,详细一点噢:        //导出到Excel
        public void ToExcel(System.Web.UI.Control ctl)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }        //导出到word文档
        public void ToWord(System.Web.UI.Control ctl)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Word.doc");
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/msword";
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }

解决方案 »

  1.   

     HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");  //输出的格式 filename=Excel.xls输出的名字
                HttpContext.Current.Response.Charset = "UTF-8";//输出的格式
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; ";//输出的编码格式
                HttpContext.Current.Response.ContentType = "application/ms-excel";//文档类型 ms-excel为excel
                ctl.Page.EnableViewState = false; //禁用EnableViewState 
                System.IO.StringWriter tw = new System.IO.StringWriter();//写入流
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                ctl.RenderControl(hw);//写入
                HttpContext.Current.Response.Write(tw.ToString());//输出
                HttpContext.Current.Response.End();//结束当前页的执行
      

  2.   

             就一个将控件中的数据导出到Word和Excel中。        //导出到Excel
            public void ToExcel(System.Web.UI.Control ctl)  //方法中的参数是要页面中的控件
            {
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls"); //为当前上下文中的响应添加头
                HttpContext.Current.Response.Charset = "UTF-8"; //设置响应的字符集
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; //响应的内容编码格式
                HttpContext.Current.Response.ContentType = "application/ms-excel"; //内容内省,是ms-excel,说明导出的是Excel格式
                ctl.Page.EnableViewState = false; 
                //以下是利用IO流输出内容
                System.IO.StringWriter tw = new System.IO.StringWriter();  
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                ctl.RenderControl(hw);
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();
            }
      

  3.   

              HttpContext.Current.Response.ContentType = "application/ms-excel";           HttpContext.Current.Response.ContentType = "application/msword";
    --------
    关键在这两句,意思是分别向浏览器输出Excel和Word格式的内容,这样浏览器就不会把内容作为HTML来解析了,而是会在浏览器打开Excel和Word来显示。