zoukankan      html  css  js  c++  java
  • 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 

  • 相关阅读:
    WCF与 Web Service的区别是什么?各自的优点在哪里呢?
    asp、asp.net、ado、ado.net各自区别和联系?
    SQL触发器 inset自学经验
    SQL触发器实例讲解
    特价汇9.9元商品
    sql中数据库连接与断开式连接有什么区别?
    终止线程的三种方法
    selenium设置代理,基于chrome浏览器
    Selenium Webdriver定位元素的几种方式
    spring常用接口 InitializingBean的作用
  • 原文地址:https://www.cnblogs.com/OwenWu/p/2637767.html
Copyright © 2011-2022 走看看