本演示中,我们实现这个Repeater控件数据导出Excel的功能。
我们准备一个对象:
复制代码 代码如下:
Imports Microsoft.VisualBasic
Namespace Insus.NET
Public Class Catalog
Private _ID As Integer
Private _Name As String
Public Property ID As Integer
Get
Return _ID
End Get
Set(value As Integer)
_ID = value
End Set
End Property
Public Property Name As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
End Class
End Namespace
复制代码 代码如下:
Private Function GetData() As List(Of Catalog) Dim cls As New List(Of Catalog) Dim cl As Catalog = New Catalog() cl.ID = 1 cl.Name = "唇膏" cls.Add(cl) cl = New Catalog() cl.ID = 2 cl.Name = "胭脂" cls.Add(cl) cl = New Catalog() cl.ID = 3 cl.Name = "化妆水" cls.Add(cl) cl = New Catalog() cl.ID = 4 cl.Name = "护手霜" cls.Add(cl) Return cls End Function
复制代码 代码如下:
<asp:Repeater runat="server">
<HeaderTemplate>
<table cellpadding="3" cellspacing="0">
<tr>
<td>ID
</td>
<td>Name
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("ID")%>
</td>
<td>
<%# Eval("Name")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
复制代码 代码如下:
Imports Insus.NET
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Data_Binding()
End If
End Sub
Private Sub Data_Binding()
Me.RepeaterCatalog.DataSource = GetData()
Me.RepeaterCatalog.DataBind()
End Sub
End Class
复制代码 代码如下:
<asp:Button runat="server" Text="Export to Excel" />
复制代码 代码如下:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim obj As New InsusExportToExcel() '实例化对象。
obj.ExportToExcel(Me.RepeaterCatalog, "catalog") '传入Repeater控件以入导出的Excel文件名。
End Sub
新闻热点
疑难解答
图片精选