public static void gridToExcel (System.Web.UI.WebControls.DataGrid grid, string excelName) {HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext. Current.Response.ContentType = "application / ms-excel"; HttpContext.Current.Response.AppendHeader ( "Content-Disposition", "attachment; filename =" excelName ".xls"); //HttpContext.Current.Response .Contencoding = system.text.encoding.getencoding ("GB2312");
grid.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter (); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw); grid.RenderControl (hw );
HttpContext.current.response.write (Tw.toString ()); httpContext.current.Response.end ();