简体   繁体   中英

Export Listview to Excel (troubleshoot)

I am working on a web application and need to be able to export a listview to Excel. The listview is bound (I can see the data), but my export function below isn't working. The function seems to be tripping on the <asp:LinkButton> and <asp:Imagebutton> . Anyone aware of a way around this?

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");
    }
}

Update: Here is the listview in its entirety so you can see the <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>

You could try to do it by the data

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM