[转]ASP.NET中将GridView 导出到Excel 文件中
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)))
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="身份证号" 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>
<!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)))
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="身份证号" 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>
posted on 2008-02-19 15:20 freeliver54 阅读(5026) 评论(3) 编辑 收藏 举报