引用http://www.cnblogs.com/chwkai/archive/2005/10/08/250426.html的代码
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.UTF8;
curContext.Response.Charset = "";
// 导出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 = "";
// 导出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();
}
}
VB的
'
待导出的DataTable
Public Shared Sub DataTable2Excel() Sub DataTable2Excel(ByVal dtData As System.Data.DataTable)
Dim dgExport As System.Web.UI.WebControls.DataGrid = Nothing
'当前对话
Dim curContext As System.Web.HttpContext = System.Web.HttpContext.Current
'IO用于导出并返回excel文件
Dim strWriter As System.IO.StringWriter = Nothing
Dim htmlWriter As System.Web.UI.HtmlTextWriter = Nothing
If Not (dtData Is Nothing) Then
'设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel"
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8
curContext.Response.Charset = ""
'导出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()
End If
End Sub
Public Shared Sub DataTable2Excel() Sub DataTable2Excel(ByVal dtData As System.Data.DataTable)
Dim dgExport As System.Web.UI.WebControls.DataGrid = Nothing
'当前对话
Dim curContext As System.Web.HttpContext = System.Web.HttpContext.Current
'IO用于导出并返回excel文件
Dim strWriter As System.IO.StringWriter = Nothing
Dim htmlWriter As System.Web.UI.HtmlTextWriter = Nothing
If Not (dtData Is Nothing) Then
'设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel"
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8
curContext.Response.Charset = ""
'导出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()
End If
End Sub
该代码能用,但是存在一个问题,DataTable不像DataGrid那样有样式的,导出来后只有简单的结构,会比较难看,当然可以修改代码里面的dgExport的Style来设置,只是导出n个不同样式的报表时就会感觉有点麻烦~
例如,修改导出Excel的标题的样式,在dgExport绑定之前加入
dgExport.HeaderStyle.BackColor
=
Drawing.Color.Green
dgExport.HeaderStyle.Font.Bold = True
dgExport.HeaderStyle.ForeColor = Drawing.Color.White
dgExport.HeaderStyle.Font.Bold = True
dgExport.HeaderStyle.ForeColor = Drawing.Color.White