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


当然最后,少不了演示:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值