<div>
<asp:ListView ID=ListView runat=server>
<ItemTemplate>
<tr>
<td id=Td runat=server >
<asp:Label ID=idLabel runat=server Text=<%# Eval(name) %> />
<br />
</td>
<td>
<asp:Label ID=xLabel runat=server Text=<%# Eval(Age) %> />
<br />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table id=Table runat=server border= >
<tr runat=server id=itemPlaceholder />
</table>
</LayoutTemplate>
</asp:ListView>
</div><asp:Button ID=Button runat=server Text=Button
onclick=Button_Click />
后台代码
public partial class ListView_Code : SystemWebUIPage
{
DataAccess da;
protected void Page_Load(object sender EventArgs e)
{
if (Session[da] == null)
{
da = new DataAccess();
Session[da] = da;
}
else
{
da = Session[da] as DataAccess;
}
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
ListViewDataSource = daList;
ListViewDataBind();
}
private void Export(string FileType string FileName)
{
ResponseCharset = GB;
ResponseContentEncoding = SystemTextEncodingUTF;
ResponseAppendHeader(ContentDisposition attachment;filename= + HttpUtilityUrlEncode(FileName EncodingUTF)ToString());
ResponseContentType = FileType;
thisEnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ListViewRenderControl(hw);
ResponseWrite(twToString());
ResponseEnd();
}
protected void Button_Click(object sender EventArgs e)
{
Export(application/msexcel employeexls);
}
}