Export Excel to the client

zhaozj2021-02-16  54

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 ();

转载请注明原文地址:https://www.9cbs.com/read-16317.html

New Post(0)