解决C#导出execl中文乱码问题!

解决办法:

     Response.Clear();
            Response.Buffer = true;
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=file.xls");
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\">");
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();

ps:关键就在于红色的这句话!

posted on 2013-11-14 15:49  井绳竖不起来  阅读(817)  评论(0编辑  收藏  举报