杏鲍菇是鸡腿菇吗:将DataGrid数据导出到Excel文档的问题

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 04:55:57
现在有如下代码:
namespace Cost.Common
{
public class CreateExcel
{
public void ToExcel(System.Web.UI.Control ctl,string FileNmae)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+FileNmae+".xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/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();
}
}
}

出来就是DataGrid的显示效果,但是DataGrid中要添加了服务器端控件改如何解决????