如果导出数据只有几行,没有问题。当导出行数有十几行的时候就出现中文乱码。导出函数
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
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
解决方案 »
- win7 64位下,asp.net连接oracle问题。
- 关于AJAX包中自带的闹钟问题
- 上传文件,说我没有权限...给aspnet加了完全控制目录的权限也不行,求救
- 大家好,我的一个模板里的一个服务器按钮,事件无法响应,为什么啊?急急。。
- C# TabControl 全部动态并且每个TabPanel里动态绑定数目不定的GridView
- 要求非负数。参数名: count 紧急求助!!!!
- 输入字符串的格式不正确
- 关于OWC的一个小问题,望大家帮忙呀
- 请问:怎样实现将一个网站翻译成多语言板呢?
- asp连接sql数据库代码错误,新手请求;急!急!急!
- 如何模拟__VIEWSTATE和__EVENTVALIDATION的值
- 服务器控件可以写js/jquery验证吗?
http://www.cnblogs.com/insus/articles/1400266.htmlsee also:
http://www.cnblogs.com/insus/archive/2013/01/16/2862121.html
public void ExportGridiewData(GridView obj, string Name)改成public void ExportGridiewData(datatable mydatatable, string Name)
去处理呢。