最近在导出DataGrid/GridView数据行到Excel的时候,如果文件名为中文的话,就会出现乱码。晕了一个下午,终于找到解决方法,现记载如下:
1
Response.Clear();
2
Response.Buffer = true;
3
Response.Charset = "gb2312";
4
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
5
Response.AppendHeader("content-disposition","attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中文名称",System.Text.Encoding.UTF8) + ".xls\"");
6
Response.ContentType = "Application/ms-excel";
7
Response.Clear();2
Response.Buffer = true;3
Response.Charset = "gb2312";4
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");5
Response.AppendHeader("content-disposition","attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中文名称",System.Text.Encoding.UTF8) + ".xls\"");6
Response.ContentType = "Application/ms-excel";7

最关健的一句就是:System.Web.HttpUtility.UrlEncode("中文名称",System.Text.Encoding.UTF8) + ".xls\"");
浙公网安备 33010602011771号