您的位置:首页 > 编程学习 > ASP.NET > 正文

Asp.net导出Excel乱码

更多 时间:2014-5-29 类别:编程学习 浏览量:248

Asp.net导出Excel乱码

Asp.net导出Excel乱码

一、设置编码格式为"UTF-8"

解决方法关键代码

  •  
  • C# 代码   复制
  • 
    Response.Charset = "UTF-8";//添加编码格式 
    Response.ClearContent(); 
    Response.Clear(); 
    Response.ContentEncoding = System.Text.Encoding.UTF8;//表格内容添加编码格式 
    Response.HeaderEncoding = System.Text.Encoding.UTF8;//表头添加编码格式 
    
    		
  •  

    例如

  •  
  • C# 代码   复制
  • 
    protected void Excel_Click(object sender, EventArgs e) 
    { 
      Response.Charset = "UTF-8"; 
      Response.ClearContent(); 
      Response.Clear(); 
      Response.ContentEncoding = System.Text.Encoding.UTF8; 
      Response.HeaderEncoding = System.Text.Encoding.UTF8; 
      Response.AddHeader("content-disposition", "attachment; filename=MyExpress。xls"); 
      Response.ContentType = "application/excel"; 
      System.IO.StringWriter sw = new System.IO.StringWriter(); 
      HtmlTextWriter htw = new HtmlTextWriter(sw); 
    
      // turn off paging 
      GridView1.AllowPaging = false; 
      dataBind(); 
      GridView1.RenderControl(htw); 
      Response.Write(sw.ToString()); 
      Response.End(); 
    
      // turn the paging on again 
      GridView1.AllowPaging = true; 
      dataBind(); 
    } 
    
    		
  •  

    二、"UTF8"格式输出还是乱码,则用 "gb2312"

    关键代码为

  •  
  • C# 代码   复制
  • 
    Response.ClearContent(); 
    Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment; filename=sumlate。xls"); 
    
    Response.Charset = "GB2312"; 
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
    Response.ContentType = "application/excel"; 
    System.IO.StringWriter sw = new System.IO.StringWriter(); 
    
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    if (GridView2.Rows.Count > 0) 
    { 
      GridView2.RenderControl(htw); 
    } 
    else 
    { 
      GridView1.RenderControl(htw); 
    } 
    Response.Write(sw.ToString()); 
    Response.End(); 
    
    		
  •  

    附加实例

  •  
  • C# 代码   复制
  • 
            /// <summary> 
            /// 把DataTable内容导出excel并返回客户端 
            /// </summary> 
            /// <param name="dgData">待导出的DataTable</param> 
            
            public static void DataTable2Excel(System.Data.DataTable dtData)
            {
                System.Web.UI.WebControls.DataGrid dgExport = null;
                // 当前对话 
                  System.Web.HttpContext curContext = System.Web.HttpContext.Current;
                // IO用于导出并返回excel文件 
                  System.IO.StringWriter strWriter = null;
                System.Web.UI.HtmlTextWriter htmlWriter = null;
    
                if (dtData != null)
                {
                    // 设置编码和附件格式 
                      curContext.Response.ContentType = "application/vnd.ms-excel";
                    curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                    curContext.Response.Charset = "gb2312";
    
                    // 导出excel文件 
                      strWriter = new System.IO.StringWriter();
                    htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
    
                    // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid 
                    dgExport = new System.Web.UI.WebControls.DataGrid();
                    dgExport.DataSource = dtData.DefaultView;
                    dgExport.AllowPaging = false;
                    dgExport.DataBind();
    
                    // 返回客户端 
                      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();
                }
            }
    
    		
  •  

    标签:Excel乱码