Repeater控件数据导出Excel(附演示动画)

它是那样的舒展,那样高雅。特别是盛夏时节,热青奔放,有粉的,白的,紫的……,互不相让,竟相开放,它们好象正在表演一场时装表演。在巨大的碧绿的荷叶上,小水珠滚来滚去,偶尔一些小青蛙跳上一片荷叶,把荷叶当做歌台。在陽光的照耀下,水珠闪闪发光,显得楚楚动人。

本演示中,我们实现这个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" OnClick="Button1_Click" />

铵钮拉好,我们要去.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

当然最后,少不了演示

到此这篇关于Repeater控件数据导出Excel(附演示动画)就介绍到这了。身无半亩,心忧天下。读书万卷,志在中华。更多相关Repeater控件数据导出Excel(附演示动画)内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
把字符串转为HtmlTable演示动画

FileUpload上传图片前实现图片预览功能(附演示动画)

读取XML并绑定至RadioButtonList实现思路及演示动画

为密码文本框要求不可粘帖字符串只可手动输入(附演示动画)

限制CheckBoxList控件只能单选实现代码及演示动画