在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();