漂泊雪狼的博客

思考,讨论,分享C#,JavaScript,.NET,Oracle,SQL Server……技术

导航

ASP.NET中解决导出到Excel乱码的方法

Posted on 2006-06-20 15:20  漂泊雪狼  阅读(9584)  评论(5)    收藏  举报



        最近在导出DataGrid/GridView数据行到Excel的时候,如果文件名为中文的话,就会出现乱码。晕了一个下午,终于找到解决方法,现记载如下:

1Response.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\"");