首页 > 其他 > 详细

Response.Write中文乱码问题

时间:2021-06-02 21:18:23      阅读:33      评论:0      收藏:0      [点我收藏+]

接手别人的一个ASP项目,功能是页面按钮下载Excel导出数据。

每次导出某一天的数据会出现excel中文乱码,其他天又没问题,因为数据量比较大,所以没有逐条去检查。

 

找了一些资料

https://www.cnblogs.com/si812cn/archive/2009/08/30/1556697.html

这个里面方法是:

往html的header里追加<meta http-equiv="Content-Type" content="text/html;charset=SHIFT_JIS">

试了没用!!

 

https://bbs.csdn.net/topics/40010306

按照这个老哥的方法试了下,问题解决了,啊哈哈哈。

技术分享图片

 

 

解决了就好,不想去深挖了。

 

 

附上代码

 

        public void ExportRuntimeByRobot(DataTable dt, string FileName)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
            DataTable dt2 = new DataTable();
            string strContent = "";
            try
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment; filename=" + FileName + ".xls");
                Response.ContentType = "application/vnd.ms-excel";
                Response.Charset = "";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;


                dg.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                dg.DataSource = dt;
                dg.DataBind();
                dg.RenderControl(htw);
                Response.Write(sw.ToString());
                Response.End();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                strContent = null;
            }
        }

 

Response.Write中文乱码问题

原文:https://www.cnblogs.com/bert-hubo/p/14842565.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!