请问,在asp.net中,给datagrid数据绑定数据源后,把datagrid中的数据导出为excel文件格式。
有时保存下来的excel文件为乱码,正常情况为简体中文;没有找到原因。有excel报表经验的同行,请帮忙分析一下,该如何避免excel文件出现乱码呢,谢谢!附:部分导出代码
//之前已经给datagrid控件进行了数据绑定
Response.Clear();
         Response.Buffer= true;
       Response.Charset="GB2312";
sring strTmp=DateTime.Now.ToShortDateString()+DateTime.Now.Ticks.ToString();
strTmp="attachment;filename=AssessMonthReport"+strTmp+".xls";
Response.AppendHeader("Content-Disposition",strTmp);
        Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
         Response.ContentType = "application/ms-excel";
// // 定义一个输入流
         System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
         System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dgrExcel.RenderControl(oHtmlTextWriter);
         Response.Write(oStringWriter.ToString());
        Response.End();

解决方案 »

  1.   

    http://www.aspxboy.com/private/showthread.asp?threadid=341http://www.aspxboy.com/private/showthread.asp?threadid=401
      

  2.   

    private void Button1_Click(object sender, System.EventArgs e)
    {
    if(TextBox1.Text=="")
    {
    Response.Write("<script language=javascript>");
    Response.Write("window.alert(\"请输入文件名\")");
    Response.Write("</script>");
    }
    else
    {
    Response.Clear();
    Response.Buffer=true;
    Response.Charset="utf-8";//设置了类型为中文防止乱码的出现 
    Response.AppendHeader("Content-Disposition","attachment;filename="+System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(TextBox1.Text.ToString()))+".xls");
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文 
    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
    this.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); 
    this.LogGrid.RenderControl(oHtmlTextWriter); 
    Response.Write(oStringWriter.ToString()); 
    }
    }
      

  3.   

    中文系统设置为默认的就可以了吧。
    Response.ContentEncoding=System.Text.Encoding.Default
      

  4.   

    用这个保证没问题
    http://aliketen.cnblogs.com/articles/363650.html
      

  5.   

    http://dev.csdn.net/article/83/83254.shtm
      

  6.   

    http://aliketen.cnblogs.com/articles/363650.html