Gridview往Excel中导入数据出现中文乱码的解决办法?

在asp.net中导出excel通常做法是:

Response.Charset="GB2312";

Response.AddHeader("content-disposition","attachment;filename="+HttpUtility.UrlEncode(用户单据报表.xls""));

Response.ContentType="application/ms-excel";

但是有中文的时候,老出现乱码,有很多解决方案,但是不能通盘的解决。

但是对代码做如下修改后:

Response.Write("<html><head><meta http-equiv=content-type content=\"text/html;charset=utf-8\">");

     System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.GridView1.RenderControl(hw);
        Response.Write(sw.ToString());
        Response.Write("</body></html>");

就这样就解决这个问题。

我推测,大概excel读到utf-8自己会改变字符集读取方式吧,

但是把他改变为Unicode又出现原来的乱码

仅供参考,如果问题,大家互相切磋!!!

Response.Clear();
Response.Charset
= "gb2312";
Response.Buffer
= true;
Response.AddHeader(
"content-disposition", "attachment;filename=" + HttpUtility.UrlEncode("用户信息表.xls").ToString());
Response.ContentType
= "application/ms-excel";
Response.Write(
"<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
System.IO.StringWriter sw
= new System.IO.StringWriter();
HtmlTextWriter hw
= new HtmlTextWriter(sw);
this.GridView1.RenderControl(hw);
Response.Write(sw.ToString());
Response.Write(
"</body></html>");
Response.Flush();
Response.End();
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
原文地址:https://www.cnblogs.com/hfliyi/p/2039634.html