将Listview导出到Excel(疑难解答)

时间:2013-10-06 23:25:00

标签: c# asp.net

我正在开发一个Web应用程序,需要能够将listview导出到Excel。 listview是绑定的(我可以看到数据),但我的导出函数不起作用。该函数似乎在<asp:LinkButton><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<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>

1 个答案:

答案 0 :(得分:1)

你可以尝试通过数据

来做到这一点
private void DataTableToExcel(DataTable dataTable)
{
    StringWriter writer = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
    GridView gridView = new GridView();
    gridView.DataSource = dataTable;
    gridView.AutoGenerateColumns = true;
    gridView.DataBind();
    gridView.RenderControl(htmlWriter);
    htmlWriter.Close();

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
    Response.Charset = "";
    Response.Write(writer.ToString());
    Response.End();
}