DataGrid导出到Excel代码 收藏

DataGrid导出到Excel代码 收藏
导出的函数:
public void ToExcel(System.Web.UI.WebControls dg)
{
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";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
dg.Page.EnableViewState =false;
System.IO.StringWriter tw
= new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw
= new System.Web.UI.HtmlTextWriter (tw);
dg.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
调用:
ToExcel(DataGrid1);

posted @ 2011-10-26 11:16  邓洁  阅读(198)  评论(0)    收藏  举报