首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2013-07-01 
从GricView中从出数据到Excel中汉字全是乱码,怎么回事啊? protected void btnExport_Click(object sender,

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


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

        }

[解决办法]
编码格式必须是GB2312

热点排行