zoukankan      html  css  js  c++  java
  • asp.net 导出excel 中文乱码解决方法 (转)

    用我转载的上一篇文章 Asp.net中把DataTable或DataGrid导出为Excel 导出的文档,中文有乱码现象,
    其实要解决中文乱码很简单,设置一下字符集。如下:

     
    1. // 设置编码和附件格式   
    2.    curContext.Response.ContentType = "application/vnd.ms-excel";  
    3. curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
    4. curContext.Response.Charset = "gb2312";  
       // 设置编码和附件格式 
                       curContext.Response.ContentType = "application/vnd.ms-excel";
                    curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                    curContext.Response.Charset = "gb2312";
    

     另外,在输出的时候,最好加上以下语句:

    <meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>

    1. // 返回客户端   
    2. dgExport.RenderControl(htmlWriter);  
    3. curContext.Response.Clear();  
    4. curContext.Response.Write("<meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>" + strWriter.ToString());  
    5. curContext.Response.End();  
                    // 返回客户端 
                    dgExport.RenderControl(htmlWriter);
                    curContext.Response.Clear();
                    curContext.Response.Write("<meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>" + strWriter.ToString());
                    curContext.Response.End();

    完整方法如下:

    /// <summary>   
    2./// 把DataTable内容导出excel并返回客户端   
    3./// </summary>   
    4./// <param name="dgData">待导出的DataTable</param>   
    5./// 创 建 人:陈文凯   
    6./// 创建日期:2005年10月08日   
    7./// 修 改 人: ranbolwb  修改导出中文乱码的问题  
    8./// 修改日期: 2012-05-29  
    9.public static void DataTable2Excel(System.Data.DataTable dtData)  
    10.{  
    11.    System.Web.UI.WebControls.DataGrid dgExport = null;  
    12.    // 当前对话   
    13.    System.Web.HttpContext curContext = System.Web.HttpContext.Current;  
    14.    // IO用于导出并返回excel文件   
    15.    System.IO.StringWriter strWriter = null;  
    16.    System.Web.UI.HtmlTextWriter htmlWriter = null;  
    17.  
    18.    if (dtData != null)  
    19.    {  
    20.        // 设置编码和附件格式   
    21.        curContext.Response.ContentType = "application/vnd.ms-excel";  
    22.        curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
    23.        curContext.Response.Charset = "gb2312";  
    24.  
    25.        // 导出excel文件   
    26.        strWriter = new System.IO.StringWriter();  
    27.        htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);  
    28.  
    29.        // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid   
    30.        dgExport = new System.Web.UI.WebControls.DataGrid();  
    31.        dgExport.DataSource = dtData.DefaultView;  
    32.        dgExport.AllowPaging = false;  
    33.        dgExport.DataBind();  
    34.  
    35.        // 返回客户端   
    36.        dgExport.RenderControl(htmlWriter);  
    37.        curContext.Response.Clear();  
    38.        curContext.Response.Write("<meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>" + strWriter.ToString());  
    39.        curContext.Response.End();  
    40.    }  
    41.}  
    
  • 相关阅读:
    设计模式小结
    Asp.net 中HttpHandler,HttpModule,IHttpHandlerFactory的原理与应用(一)
    全新对待.net一次全面的旅程
    页面生命周期小结
    面向对象点滴
    Chapter 2.1:WCF服务契约的重载与继承详解
    一封给“X教授”的回信(讨论Socket通信)
    Chapter 1.4:WCF实践 元数据详解
    有了WCF,Socket是否已人老珠黄?
    Chapter 1.3:WCF实践 HelloWorld
  • 原文地址:https://www.cnblogs.com/mishy/p/3865775.html
Copyright © 2011-2022 走看看