• Asp.NET导出Excel文件乱码 终极解决方法


    System.Web.UI.WebControls.DataGrid datagrid=new System.Web.UI.WebControls.DataGrid();
                datagrid.ID="excelGrid1";
                
                datagrid.DataSource=data;
                datagrid.DataBind();

                string filename = "table.xls";
                
                filename = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename));

                //导出
                System.Web.HttpContext.Current.Response.Clear(); 
                System.Web.HttpContext.Current.Response.Buffer= true
                System.Web.HttpContext.Current.Response.Charset="UTF-8";   //GB2312 
                System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+filename); 
                System.Web.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文
                System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                datagrid.EnableViewState = false;    
                System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);        
                datagrid.RenderControl(oHtmlTextWriter); 
                System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
                System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
                System.Web.HttpContext.Current.Response.End();

    重点在

    Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>"); 

    其他 Asp.NET导出Excel文件乱码解决若干方法 请参考

    http://www.cnblogs.com/cbcye/archive/2009/05/26/1490451.html 

  • 相关阅读:
    mysql配置参数
    nginx配置https,重定向后https变成了http
    网速测试工具
    批量清理mysql进程
    新版本django中的path不能使用正则表达式
    巨好看的xshell配色
    zabbix 基于sendmail发送邮件相关问题
    curl分析请求的各个部分耗时情况
    wqs二分的边界
    oauth2
  • 原文地址:https://www.cnblogs.com/OwenWu/p/2637767.html
Copyright © 2020-2023  润新知