日期:2014-05-17  浏览次数:20940 次

从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