//将HTTP头添加到输出流
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=word.doc"); //指定生成文件的类型
Response.ContentType = "application/vnd.ms-text";  
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
this.Datagrid2.Page.EnableViewState = true; System.IO.StringWriter tw= new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw= new System.Web.UI.HtmlTextWriter(tw);
this.Panel1.Visible =true;

//将服务器控件中的内容输出到ystem.Web.UI.HtmlTextWriter对象中
this.Panel1.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();