下面代碼實現將 GridVIEw 導出到 Excel文件中。
值得注意的是VerifyRenderingInServerForm重載方法:
MSDN上的 VerifyRenderingInServerForm 方法的描述:
必須位於 <form runat=server> 標記中的控件可以在呈現之前調用此方法,以便在控件被置於標記外時顯示錯誤信息。發送回或依賴於注冊的腳本塊的控件應該在 Control.Render 方法的重寫中調用此方法。呈現服務器窗體元素的方式不同的頁可以重寫此方法以在不同的條件下引發異常。
如果回發或使用客戶端腳本的服務器控件沒有包含在 HtmlForm 服務器控件 (<form runat="server">) 標記中,它們將無法正常工作。這些控件可以在呈現時調用該方法,以在它們沒有包含在 HtmlForm 控件中時提供明確的錯誤信息。
開發自定義服務器控件時,通常在為任何類型的輸入標記重寫 Render 方法時調用該方法。這在輸入控件調用 GetPostBackEventReference 或發出客戶端腳本時尤其重要。復合服務器控件不需要作出此調用。
沒有這個方法,程序將報錯。
C# 代碼
<%@ Page Language="C#" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<script runat="server">
ICollection CreateDataSource( )
{
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++)
{
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;
}
protected void Page_Load( object sender, EventArgs e )
{
if (!IsPostBack)
{
GridVIEw1.BorderWidth = Unit.Pixel(2);
GridVIEw1.BorderColor = System.Drawing.Color.DarkOrange;
; GridVIEw1.DataSource = CreateDataSource();
GridVIEw1.DataBind();
}
}
protected void Button1_Click( object sender, System.EventArgs e )
{
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();
}
public override void VerifyRenderingInServerForm( Control control )
{ }
protected void GridView1_RowDataBound( object sender, GridVIEwRowEventArgs e )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Attributes.Add("style", "vnd.ms-Excel.numberformat:@;");
}
}
</script>
<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="SFZ號" DataFIEld="PkID" /> <br> <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>
VB.Net 代碼
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHtml 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<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))) <br> 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
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
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
Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub
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
</script>
<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="SFZ號" 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>