private
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.UTF8;
// curContext.Response.Charset = "";
curContext.Response.Charset="utf-8";
curContext.Response.AppendHeader("Content-Disposition","attachment;filename=ganbuxinxi.xls");
curContext.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
curContext.Response.ContentType = "application/ms-excel";
// 导出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.Write(strWriter.ToString());
curContext.Response.End();
}
}
... {
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.UTF8;
// curContext.Response.Charset = "";
curContext.Response.Charset="utf-8";
curContext.Response.AppendHeader("Content-Disposition","attachment;filename=ganbuxinxi.xls");
curContext.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
curContext.Response.ContentType = "application/ms-excel";
// 导出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.Write(strWriter.ToString());
curContext.Response.End();
}
}
我的方法就是把上面注释的代码换成了它下面的
curContext.Response.Charset="utf-8";
curContext.Response.AppendHeader("Content-Disposition","attachment;filename=ganbuxinxi.xls");
curContext.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
curContext.Response.ContentType = "application/ms-excel";
还有人常会说我怎么改列名,其实改的方法也简单。就是在数据给datatable填充后用
dt.Columns["列名"].ColumnName="你想起的名称";
即可,例如下面的代码:
DataTable dt
=
new
DataTable();
execlcmd.Fill(dt);
dt.Columns[ " mid " ].ColumnName = " 会员编号 " ;
dt.Columns[ " name " ].ColumnName = " 会员姓名 " ;
dt.Columns[ " cardrename " ].ColumnName = " 职务 " ;
dt.Columns[ " jobtime " ].ColumnName = " 任职时间 " ;
dt.Columns[ " deptid " ].ColumnName = " 所属公会 " ;
execlcmd.Fill(dt);
dt.Columns[ " mid " ].ColumnName = " 会员编号 " ;
dt.Columns[ " name " ].ColumnName = " 会员姓名 " ;
dt.Columns[ " cardrename " ].ColumnName = " 职务 " ;
dt.Columns[ " jobtime " ].ColumnName = " 任职时间 " ;
dt.Columns[ " deptid " ].ColumnName = " 所属公会 " ;