public static void DataGridToExcel(DataGrid dataGrid, string fileName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
// 1. Set ContentEncoding
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
// 2. Set harset
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
var sw = new StringWriter();
var htmlw = new HtmlTextWriter(sw);
dataGrid.RenderControl(htmlw);
// 3. Set met
DataGrid 导出 Excel 中文乱码
最新推荐文章于 2023-10-09 14:44:25 发布