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 meta data
HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>");
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}DataGrid 导出 Excel 中文乱码
最新推荐文章于 2021-09-15 15:36:29 发布
本文介绍如何处理DataGrid导出到Excel时出现的中文乱码问题,通过设置ContentEncoding、Charset及在响应中添加meta标签来确保正确编码。

714

被折叠的 条评论
为什么被折叠?



