Repeater控件数据导出Excel(附演示动画)
程序员文章站
2024-03-05 19:54:07
本演示中,我们实现这个repeater控件数据导出excel的功能。 我们准备一个对象: 复制代码 代码如下: imports microsoft.visualbasic...
本演示中,我们实现这个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
当然最后,少不了演示: