In this demo, we implement the functionality of this Repeater control data export Excel.
we prepare an object :
Copy Code code as follows:
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
prepare data to populate the objects created above:
Copy Code code as follows:
Private Function GetData () as List (of Catalog) Dim cls As New List (of Catalog) Dim cl As Catalog = New Catalog () cl.id = 1 All Name = "Lipstick" CLS. ADD (CL) cl = New Catalog () Cl.id = 2 cl. Name = "Rouge" CLS. ADD (CL) cl = New Catalog () Cl.id = 3 cl. Name = "Cosmetic Water" cls. ADD (CL) cl = New Catalog () Cl.id = 4 cl. Name = "Hand Cream" cls. ADD (CL) return CLS End Function
pull a Repeater control on the. aspx page:
Copy Code code as follows:
<asp:repeater id= "Repeatercatalog" runat= "Server" >
<HeaderTemplate>
<table border= "1" 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>
But in. Aspx.vb bind data for the Repeater control:
Copy Code code as follows:
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
OK, all ready threads, we are in. aspx pull an ammonium button, so that when the user clicks on the Ammonium button, the Repeater control data can be exported Excel.
Copy Code code as follows:
<asp:button id= "Button1" runat= "Server" text= "Export to Excel" onclick= "Button1_Click"/>
The Ammonium button pulls well, we're going to. aspx.vb write the OnClick event, before writing, first download a insusexporttoexcel Library after decompression into the bin directory.
Copy Code code as follows:
Protected Sub button1_click (sender as Object, E as EventArgs)
Dim obj as New insusexporttoexcel () instantiates the object.
Obj. exportToExcel (Me.repeatercatalog, "catalog") ' passes in the Repeater control to the exported Excel file name.
End Sub
of course, finally, the demo: