在使用中文名导出数据到excel文件时文件名出现乱码的解决办法

来源:互联网 发布:淘宝在线客服人工咨询 编辑:程序博客网 时间:2024/04/29 11:55

可以用下面2种简单的方法:

导出的类部分代码:
HttpResponse resp;
resp=Page.Response;
resp.ContentEncoding=System.Text.Encoding.Default;
resp.AppendHeader("Content-Disposition", "attachment;filename=报表.xls");   //这样写就出现了乱码

 

1.用System.Web.HttpUtility.UrlEncode或者Server.UrlEncode方法,不过要2个参数都写上:
System.Web.HttpUtility.UrlEncode("报表",System.Text.Encoding.UTF8)+".xls");

2.用HttpUtility.UrlPathEncode方法,只要写一个参数就可以了:
HttpUtility.UrlPathEncode("报表.xls")

最好就是这种了:
resp.AppendHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlPathEncode(FileName));