Gridview导出Excel时中文乱码问题

没落鬼族 2008-09-16 04:35:18
网上有挺多Gridview导出Excel的代码,大体内容都一样

版本一
...
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8)+".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
...

版本二
...
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
...

版本三
...
Response.ContentEncoding = System.Text.Encoding.UTF7;
...


前提:web.config中添加了<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
现状:版本一和版本二我都试成功过,版本三从来没成功过

问题:为什么有的时候版本一会乱码,有的时候版本二会乱码,原因是什么?请高手帮忙解释一下,o(∩_∩)o...谢谢!
...全文
1075 32 打赏 收藏 转发到动态 举报
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhzhiy_cate 2012-01-12
  • 打赏
  • 举报
回复
加上这一句就没问题了,
我已经试过了
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
li_dongfangguo 2010-04-11
  • 打赏
  • 举报
回复
其实GridView中导出Excel的方法,大部分都是导出一个文本的html文件,利用了office能够编辑网页的功能,来实现。只是有时候导出的中文显示是乱码。经过我不断的baidu最后发现最后一句话很重要。没有他基本上行不通。不过我也只是用vs2005和office2003测试。

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application nd.ms-excel";
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Disposition", "attachment;filename=a.xls");
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
thousandlin 2009-03-04
  • 打赏
  • 举报
回复
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
linxisheng 2008-09-18
  • 打赏
  • 举报
回复
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8)+".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
web.config 也要为 utf-8
记得全部要统一
lietou1986 2008-09-17
  • 打赏
  • 举报
回复
utf8通用,vs的默认编码
txg3104257 2008-09-17
  • 打赏
  • 举报
回复
Response.AddHeader("Content-Disposition", "attachment; filename=" & Session("eid").ToString.Trim + Session("uid").ToString.Trim & ".txt")
Response.ContentType = "application/ms-excel"
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
Response.Write(sw)
Response.End()
没落鬼族 2008-09-17
  • 打赏
  • 举报
回复
现在的情况是:
代码1:

Response.Charset = "GB2312";
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ".xls");

上面代码大多数GridView导出EXCEL时能正常显示中文,少部分GridView要用下面代码才能正常显示。
代码2:

Response.Charset = "GB2312";
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ".xls");


更为奇怪的是,有时本来用代码2能正常显示的gridview会莫名其妙的又出现乱码现象。改成代码1后显示正常。
到目前为止代码1显示正常的一直多显示正常,没有出现突然不正常的情况。

到底怎么回事啊?谁能告诉我!
wwd252 2008-09-17
  • 打赏
  • 举报
回复
JF
超人Q 2008-09-17
  • 打赏
  • 举报
回复
这个是我用的,试试:

。。。
Response.Charset = "GB2312";
Response.ContentEncoding = Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
this.GridView1.AllowPaging = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
。。。
zpcoder 2008-09-17
  • 打赏
  • 举报
回复
参考:


        Response.Clear();
Response.Buffer = true;

Response.Charset = "GB2312";
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.GetEncoding("gb2312")).ToString());
this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView gv = new GridView();
gv.DataSource = dt;
gv.DataBind();
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
没落鬼族 2008-09-17
  • 打赏
  • 举报
回复
顶起,没人知道为什么吗?
没落鬼族 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 panw520 的回复:]

Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

或者用utf-8
[/Quote]

有的页面gb2312乱码,utf-8正常
有的页面utf-8乱码,gb2312正常

这是怎么回事?
没落鬼族 2008-09-16
  • 打赏
  • 举报
回复
我相信大家给我的建议都是自己曾经测试过的,那为什么答案会有这么多版本?
panw520 2008-09-16
  • 打赏
  • 举报
回复

Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

或者用utf-8
没落鬼族 2008-09-16
  • 打赏
  • 举报
回复
答案是不行
帝国飘客 2008-09-16
  • 打赏
  • 举报
回复
你会成功的。我相信你的实力。嘎嘎。。。祝君好运。
solomonliang 2008-09-16
  • 打赏
  • 举报
回复
使用encoding.default 比较好,使用默认的编码
没落鬼族 2008-09-16
  • 打赏
  • 举报
回复
哦,我试试o(∩_∩)o...
Smile_Wong1 2008-09-16
  • 打赏
  • 举报
回复
不一样,没有
Response.Charset = "GB2312";
没落鬼族 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 Smile_Wong 的回复:]
版本四:
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); // 中文
Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls\"");
[/Quote]

这个应该跟版本一,一样的吧
加载更多回复(12)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧