工作中点滴记录

永远保持学徒心态

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

在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();
posted on 2011-05-07 10:33  梦里故乡  阅读(1301)  评论(0编辑  收藏  举报