0

UpdatePanel我尝试绑定到 aListView并将其内容导出到 Excel 中。这些功能可以独立工作(我的“运行报告”按钮绑定了ListView,“导出到 Excel”按钮使用 EPPlus 导出 .xlsx 文件)。为了使 .xlsx 导出工作,我必须<Triggers> Postback为“导出到 Excel”按钮创建一个。

我想要“导出到 Excel”按钮来绑定ListView和导出 .xlsx。我已经添加了绑定ListView到导出方法的方法,但它似乎正在跳过它。我想我不理解 PostBack 的某些部分。

这是<Trigger>

<Triggers>
    <asp:PostBackTrigger ControlID="export" />
</Triggers>

这是ListView绑定:

public void runreport(object sender, EventArgs e)
{
    reportlv.DataSource = null;
    reportlv.DataBind();
    bindreportlv();
    elreportdiv.Visible = true;
}

这是嵌入了 runreport 方法的导出方法:

protected void export_Click(object sender, EventArgs e)
{
    runreport(null, null);

    ExcelPackage pck = new ExcelPackage();
    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("worksheet");

    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("Date", typeof(string)));
    dt.Columns.Add(new DataColumn("Payee", typeof(string)));
    dt.Columns.Add(new DataColumn("Amount", typeof(string)));

    foreach (ListViewDataItem li in reportlv.Items)
    {
        if (li.ItemType == ListViewItemType.DataItem)
        {
            DataRow dr = dt.NewRow();
            dr["Date"] = ((Label)li.FindControl("lbldate")).Text.ToString();
            dr["Payee"] = ((Label)li.FindControl("lblpayee")).Text.ToString();
            dr["Amount"] = ((Label)li.FindControl("lblamt")).Text.ToString();
            dt.Rows.Add(dr);
        }
    }

    ws.Cells["A1"].LoadFromDataTable(dt, true);
    pck.SaveAs(Response.OutputStream);
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;  filename=summary.xlsx");
    Response.Flush();
    Response.End();
}

以下是按钮:

<asp:Button runat="server" ID="run" OnClick="runreport" CssClass="button" Text="Run Report" />
<asp:Button runat="server" ID="export" OnClick="export_Click" CssClass="button" Text="Export to Excel" />

更新

这是 Page_Load 方法:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        runreport(null, null);
    }
}
4

1 回答 1

0

<Triggers> <asp:PostBackTrigger ControlID="export" onclick ="CLick" /> </Triggers>

于 2013-10-08T06:39:12.157 回答