解决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("");
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();

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

转载于:https://www.cnblogs.com/Yet-Yet/p/3423470.html

你可能感兴趣的:(解决C#导出execl中文乱码问题!)