我正在开发一个 Web 应用程序,需要能够将列表视图导出到 Excel。列表视图已绑定(我可以看到数据),但下面的导出功能不起作用。该功能似乎在<asp:LinkButton>
and上跳闸<asp:Imagebutton>
。有人知道解决这个问题的方法吗?
public void ExportIntoExcel(ListView lvExport, string Header, string FileName)
{
try
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xls");
System.Web.HttpContext.Current.Response.Charset = "";
System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWrite = new StringWriter();
stringWrite.Write(Header);
stringWrite.WriteLine();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
HtmlForm frm = new HtmlForm();
lvExport.Parent.Controls.Add(frm);
frm.Controls.Add(lvExport);
frm.RenderControl(htmlWrite);
System.Web.HttpContext.Current.Response.Write(stringWrite.ToString());
}
catch (Exception ex)
{
}
finally
{
System.Web.HttpContext.Current.Response.End();
}
}
protected void export_Click(object sender, EventArgs e)
{
if (paidlv.Visible == true)
{
ExportIntoExcel(paidlv, "Income Summary", "incomesummary");
}
}
更新:
这是完整的列表视图,因此您可以看到<asp:LinkButton>s
and <asp:Imagebutton>s
。
<asp:ListView ID="paidlv" runat="server" OnPagePropertiesChanged="paidlv_PagePropertiesChanged" OnDataBound="paidlv_DataBound" OnSorting="paidlv_Sorting" Visible="true">
<LayoutTemplate>
<table class="detail">
<tr>
<th>
<asp:LinkButton ID="lnkdate" runat="server" CommandName="Sort" CommandArgument="pmtpaydate1">Date</asp:LinkButton>
<asp:Image runat="server" ID="SortRevImage5" ImageUrl="~/img/arrows.png" Visible="false" />
</th>
<th>
<asp:LinkButton ID="lnktname" runat="server" CommandName="Sort" CommandArgument="tname">Transaction Name</asp:LinkButton>
<asp:Image runat="server" ID="SortRevImage2" ImageUrl="~/img/arrows.png" Visible="false" />
</th>
<th>
<asp:LinkButton ID="lnkcname" runat="server" CommandName="Sort" CommandArgument="cname">Client Name</asp:LinkButton>
<asp:Image runat="server" ID="SortRevImage1" ImageUrl="~/img/arrows.png" Visible="false" />
</th>
<th>
<asp:LinkButton ID="lnkhalf" runat="server" CommandName="Sort" CommandArgument="pmtdesc1">Portion</asp:LinkButton>
<asp:Image runat="server" ID="SortRevImage3" ImageUrl="~/img/arrows.png" Visible="false" />
</th>
<th>
<asp:LinkButton ID="lnkamt" runat="server" CommandName="Sort" CommandArgument="pmtamt">Amount</asp:LinkButton>
<asp:Image runat="server" ID="SortRevImage4" ImageUrl="~/img/arrows.png" Visible="false" />
</th>
<th>Delete</th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<div class="pager">
<asp:DataPager ID="paidpager" runat="server" PageSize="20" PagedControlID="paidlv">
<Fields>
<asp:NextPreviousPagerField ShowNextPageButton="False" ButtonCssClass="previousNextLink" />
<asp:NumericPagerField ButtonCount="10" ButtonType="Link" NumericButtonCssClass="numericLink" />
<asp:NextPreviousPagerField ShowPreviousPageButton="False" ButtonCssClass="previousNextLink" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<ItemTemplate>
<tr class="altRow rowcolor">
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lbldate"><%#Eval("pmtdate", "{0:d}") %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lbltname"><%#Eval("tname") %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lblcname"><%#Eval("cname") %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lblhalf"><%#Eval("half").ToString() == "1" ? "First" : Eval("half").ToString() == "2" ? "Second" : "Full" %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lblamt"><%#Eval("pmtamt", "{0:C}") %></asp:Label>
</td>
<td>
<asp:ImageButton runat="server" ID="btndelete" ImageUrl="~/img/delete.png" OnClick="btndelete_Click" PostBackUrl="~/transaction.aspx" CssClass="deletebutton" CommandArgument='<%#Eval("transactionid") %>' />
</td>
<asp:HiddenField runat="server" ID="lblrevid" Value='<%#Eval("transactionid") %>' />
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="rowcolor">
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lbldate"><%#Eval("pmtdate", "{0:d}") %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lbltname"><%#Eval("tname") %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lblcname"><%#Eval("cname") %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lblhalf"><%#Eval("half").ToString() == "1" ? "First" : Eval("half").ToString() == "2" ? "Second" : "Full" %></asp:Label>
</td>
<td class="valigntop" onclick="DoNav('<%# Eval("transactionid","bookdeal.aspx?tid={0}") %>');">
<asp:Label runat="server" ID="lblamt"><%#Eval("pmtamt", "{0:C}") %></asp:Label>
</td>
<td>
<asp:ImageButton runat="server" ID="btndelete" ImageUrl="~/img/delete.png" OnClick="btndelete_Click" PostBackUrl="~/transaction.aspx" CssClass="deletebutton" CommandArgument='<%#Eval("transactionid") %>' />
</td>
<asp:HiddenField runat="server" ID="lblrevid" Value='<%#Eval("transactionid") %>' />
</tr>
</AlternatingItemTemplate>
<EmptyDataTemplate>
<table id="empty">
<tr>
<td id="tdempty">
<br />
Sorry, no data found
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:ListView>