下面代码实现将 GridView 导出到 Excel文件中。
值得注意的是VerifyRenderingInServerForm重载方法:
MSDN上的 VerifyRenderingInServerForm 方法的描述:
必须位于 <form runat=server> 标记中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息。发送回或依赖于注册的脚本块的控件应该在 Control.Render 方法的重写中调用此方法。呈现服务器窗体元素的方式不同的页可以重写此方法以在不同的条件下引发异常。
如果回 发或使用客户端脚本的服务器控件没有包含在 HtmlForm 服务器控件 (<form runat="server">) 标记中,它们将无法正常工作。这些控件可以在呈现时调用该方法,以在它们没有包含在 HtmlForm 控件中时提供明确的错误信息。
开发自定义服务器控件时,通常在为任何类型的输入标记重写 Render 方法时调用该方法。这在输入控件调用 GetPostBackEventReference 或发出客户端脚本时尤其重要。复合服务器控件不需要作出此调用。
没有这个方法,程序将报错。
C# 代码
<%
...
@ Page Language="C#" EnableEventValidation="false"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<
script
runat
="server"
>
...
ICollection CreateDataSource( )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("id", typeof(Int32)));
dt.Columns.Add(new System.Data.DataColumn("PkID", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("Title", typeof(string)));
for (int i = 0; i < 6; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "123456789123456789123456789";
dr[2] = "<a href='http://dotnet.aspx.cc/'>欢迎光临【孟宪会之精彩世界】</a>";
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected void Page_Load( object sender, EventArgs e )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (!IsPostBack)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
GridView1.BorderWidth = Unit.Pixel(2);
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
protected void Button1_Click( object sender, System.EventArgs e )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
public override void VerifyRenderingInServerForm( Control control )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{ }
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (e.Row.RowType == DataControlRowType.DataRow)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}
</
script
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
将 GridView 导出到 Excel 文件中
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
asp:GridView
ID
="GridView1"
runat
="server"
OnRowDataBound
="GridView1_RowDataBound"
AutoGenerateColumns
="false"
>
<
Columns
>
<
asp:BoundField
HeaderText
="序号"
DataField
="id"
/>
<
asp:BoundField
HeaderText
="身份证号"
DataField
="PkID"
/>
<
asp:BoundField
HeaderText
="网址"
DataField
="Title"
ReadOnly
="true"
HtmlEncode
="false"
/>
</
Columns
>
</
asp:GridView
>
<
asp:Literal
ID
="HiddenOut"
runat
="server"
/>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="导出"
OnClick
="Button1_Click"
/>
</
form
>
</
body
>
</
html
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
VB.NET 代码
<%
...
@ Page Language="VB"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<
script
runat
="server"
>
...
Function CreateDataSource() As ICollection
Dim dt As System.Data.DataTable = New System.Data.DataTable
Dim dr As System.Data.DataRow
dt.Columns.Add(New System.Data.DataColumn("id", GetType(Int32)))
dt.Columns.Add(New System.Data.DataColumn("PkID", GetType(String)))
dt.Columns.Add(New System.Data.DataColumn("Title", GetType(String)))
Dim i As Integer = 0
While i < 6
dr = dt.NewRow
dr(0) = i
dr(1) = "123456789123456789123456789"
dr(2) = "<a href='http://dotnet.aspx.cc/'>欢迎光临【孟宪会之精彩世界】</a>"
dt.Rows.Add(dr)
System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)
End While
Dim dv As System.Data.DataView = New System.Data.DataView(dt)
Return dv
End Function
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
GridView1.BorderWidth = Unit.Pixel(2)
GridView1.BorderColor = System.Drawing.Color.DarkOrange
GridView1.DataSource = CreateDataSource
GridView1.DataBind
End If
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Clear
Response.Buffer = True
Response.Charset = "GB2312"
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls")
Response.ContentEncoding = System.Text.Encoding.UTF7
Response.ContentType = "application/ms-excel"
Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
Me.GridView1.RenderControl(oHtmlTextWriter)
Response.Output.Write(oStringWriter.ToString)
Response.Flush
Response.End
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(1).Attributes.Add("style", "vnd.ms-excel.numberformat:@;")
End If
End Sub
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
</
script
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
id
="Head1"
runat
="server"
>
<
title
>
将 GridView 导出到 Excel 文件中
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
asp:GridView
ID
="GridView1"
runat
="server"
OnRowDataBound
="GridView1_RowDataBound"
AutoGenerateColumns
="false"
>
<
Columns
>
<
asp:BoundField
HeaderText
="序号"
DataField
="id"
/>
<
asp:BoundField
HeaderText
="身份证号"
DataField
="PkID"
/>
<
asp:BoundField
HeaderText
="网址"
DataField
="Title"
ReadOnly
="true"
HtmlEncode
="false"
/>
</
Columns
>
</
asp:GridView
>
<
asp:Literal
ID
="HiddenOut"
runat
="server"
/>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="导出"
OnClick
="Button1_Click"
/>
</
form
>
</
body
>
</
html
>