我有中继器连接到我的数据库,我想将其数据导出到 xls 文件中。我能够做到这一点,除了因为我的 repater 包含一个控制按钮来操作行(删除)我的 xls 是用这个按钮呈现的。
<asp:Repeater ID="Repeater1" runat="server" EnableTheming="true"onitemcommand="Repeater1_ItemCommand" >
<HeaderTemplate>
<table class="datatable fullwidthpercent">
<tr>
<td>ID</td>
<td> Nom</td>
<td>Prénom</td>
<td>Email</td>
<td>Adresse</td>
<td>Ville</td>
<td>Tél</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("id") %></td>
<td><%# Eval("nom") %></td>
<td><%# Eval("prenom") %></td>
<td><%# Eval("email") %></td>
<td><%# Eval("adresse") %></td>
<td><%# Eval("ville") %></td>
<td><%# Eval("tel") %></td>
<td><asp:Button ID="Btn1" runat="server" CommandArgument= ' <%# Eval("id") %> ' Text="Eliminer" OnClick = "buttonEliminer_click" ></asp:Button></td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan="6"><hr /></td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
代码背后:
protected void ButtonExporter_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=Gagnants.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Repeater1.RenderControl(htmlWrite);
Response.Write("<table>");
Response.Write(stringWrite.ToString());
Response.Write("</table>");
Response.End();
}
使用内部控制渲染中继器时克服表单应答器问题的覆盖方法:
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
我在输出 xls 得到这个: