0

Web アプリケーションで作業しており、リストビューを Excel にエクスポートできるようにする必要があります。リストビューはバインドされています (データを表示できます) が、以下のエクスポート機能が機能していません。関数は と でつまずいているよう<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>
4

1 に答える 1

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();
}
于 2013-10-07T05:31:30.550 に答える