手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >Repeater控件数据导出Excel(附演示动画)
Repeater控件数据导出Excel(附演示动画)
摘要:本演示中,我们实现这个Repeater控件数据导出Excel的功能。我们准备一个对象:复制代码代码如下:ImportsMicrosoft.V...

本演示中,我们实现这个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

在.aspx页面拉一个Repeater控件:

复制代码 代码如下:

<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控件绑定数据:

复制代码 代码如下:

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" />

铵钮拉好,我们要去.aspx.vb写onClick事件,在写之前,首先下载一个InsusExportToExcel Library 解压之后放入BIN目录中。

复制代码 代码如下:

Protected Sub Button1_Click(sender As Object, e As EventArgs)

Dim obj As New InsusExportToExcel() '实例化对象。

obj.ExportToExcel(Me.RepeaterCatalog, "catalog") '传入Repeater控件以入导出的Excel文件名。

End Sub

当然最后,少不了演示:

1

【Repeater控件数据导出Excel(附演示动画)】相关文章:

.Net连接Oracle数据库的实现代码

DataSet 添加数据集、行、列、主键和外键等操作示例

asp.net导出EXCEL的功能代码

Repeater事件OnItemCommand取得行内控件的方法

DropDownList绑定数据表实现两级联动示例

ASP.NET为Grid控件添加删除前确认的功能

将datagrid控件内容输出到excel文件

Asp.net:常见数据导入Excel

asp.net Linq把数据导出到Excel的代码

Repeater控件与PagedDataSource结合实现分页功能

精品推荐
分类导航