Repeater控件數據導出Excel


本演示中,我們實現這個Repeater控件數據導出Excel的功能。

我們准備一個對象:

View Code
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


准備數據來填充上面創建好的對象:

View Code
    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


在.aspx頁面拉一個Repeater控件:

View Code
<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>


然在.aspx.vb為Repeater控件綁定數據:

View Code
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,一切准備緒,我們在.aspx拉一個銨鈕,讓用戶點擊此銨鈕時,能對Repeater控件的數據導出Excel。

<asp:Button ID="Button1" runat="server" Text="Export to Excel" OnClick="Button1_Click" />


銨鈕拉好,我們要去.aspx.vb寫onClick事件,在寫之前,首先下載一個InsusExportToExcel Library 解壓之后放入BIN目錄中。

View Code
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        Dim obj As New InsusExportToExcel()  '實例化對象。
        obj.ExportToExcel(Me.RepeaterCatalog, "catalog") '傳入Repeater控件以入導出的Excel文件名。
    End Sub


當然最后,少不了演示:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM