漫漫技术人生路

C#

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

ASP.NET中解决导出到Excel乱码的方法
  最近在导出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\"");
找到方法啦!

Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
换成:
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
就可以解决Excel内部中文出现乱码的问题了

posted on 2009-12-16 18:23  javaca88  阅读(370)  评论(0编辑  收藏  举报