如果导出数据只有几行,没有问题。当导出行数有十几行的时候就出现中文乱码。导出函数
public void ExportGridiewData(GridView obj, string Name)
        {
            Name += "工资";
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="+Name+".xls");
            //设置输出流为简体中文
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //设置输出文件类型为excel文件。
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            obj.RenderControl(htw);
            HttpContext.Current.Response.Output.Write(sw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();        }
GridView的数据绑定以及导出
try
            {
                string getwage = "select a.Name,b.BaseWage,b.Award,b.Punish,b.Total from (select * from Employee where Department='" + DeptID + "') a left join (select * from Wage) b  on a.EmployeeID=b.EmployeeID";
                ds1 = tr.SelectEID(getwage);
            }
            catch (Exception exception)
            {
                Response.Write("<script>alert('获得部门工资信息过程中出错,请重试!')</script>");
                return;
            }
            if (ds1.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds1;
                GridView1.DataBind();
                GridView1.HeaderRow.Cells[0].Text = "员工名称";
                GridView1.HeaderRow.Cells[1].Text = "基本工资";
                GridView1.HeaderRow.Cells[2].Text = "奖励";
                GridView1.HeaderRow.Cells[3].Text = "惩罚";
                GridView1.HeaderRow.Cells[4].Text = "合计";
               
                export.ExportGridiewData(GridView1, DropDownList1.Text.ToString());            }
            else
            {
                Response.Write("未找到员工信息!"); GridView1.DataSource = null; GridView1.DataBind();                return;
            }        GridView

解决方案 »

  1.   

    Refer:
    http://www.cnblogs.com/insus/articles/1400266.htmlsee also:
    http://www.cnblogs.com/insus/archive/2013/01/16/2862121.html
      

  2.   

    不要用GB2312;    用 System.Text.Encoding.Default         
      

  3.   

    那你处理的时候不要传
    public void ExportGridiewData(GridView obj, string Name)改成public void ExportGridiewData(datatable mydatatable, string Name)
    去处理呢。
      

  4.   

    就是导出10行以内数据时候没有问题,超过10行就出现中文乱码了。GridView中的10行大概就是默认一页的内容。我导出其他数据的时候没有出现这个问题,可能哪里出了错了。