In this demonstration, we implement the Repeater control data export Excel function.
We prepare an object:
Copy codeThe Code is 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 fill in the object created above:Copy codeThe Code is 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 cl. 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 = "Lotion" 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 codeThe Code is 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>
However, in. aspx. vb, bind data to the Repeater control.:Copy codeThe Code is 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 preparations. We pull an button in. aspx so that you can click this button to export the data of the Repeater control to Excel.Copy codeThe Code is as follows: <asp: Button ID = "Button1" runat = "server" Text = "Export to Excel" OnClick = "button#click"/>
Pull the button. We will go to. aspx. vb to write the onClick event. before writing, first download an InsusExportToExcel Library and decompress it into the BIN directory.Copy codeThe Code is As follows: Protected Sub button#click (sender As Object, e As EventArgs)
Dim obj As New InsusExportToExcel () 'instantiate an object.
Obj. ExportToExcel (Me. RepeaterCatalog, "catalog") 'imports the Repeater control to import the exported Excel file name.
End Sub
At the end of the lecture: