从GricView中从出数据到Excel中汉字全是乱码,怎么回事啊?

yelang 2013-06-26 05:30:45

protected void btnExport_Click(object sender, EventArgs e)
{
System.Web.HttpContext HC = System.Web.HttpContext.Current;
HC.Response.Clear();
HC.Response.Charset = "UTF-8";//这里原来是GB2312,导出是乱码,我改成UTF-8后还是乱码,为什么啊?
HC.Response.Buffer = true;
HC.Response.ContentEncoding = System.Text.Encoding.UTF8;
HC.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("查询结果打印", System.Text.Encoding.UTF8) + ".xls");
HC.Response.ContentType = "application/excel";//如果要打印为excel格式,则换为"application/excel"
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
gvDataDisplay.RenderControl(htw);
HC.Response.Write(sw.ToString());
HC.Response.End();

}
...全文
70 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
abutwang 2013-06-26
  • 打赏
  • 举报
回复
编码格式必须是GB2312

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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