1

我需要将选定的 asp.net gridview 行下载到 Excel 表中。我正在做的是一次尝试检查全部或仅选择几个,然后在按下下面的下载按钮后,所有选定的行都被下载为 excel。当我按下下载按钮时,一切都在这里正常工作,但是所有行都被下载而忽略了选择。网格视图示例 以下是我的代码

public void ExportGridToExcel(GridView grdGridView, string fileName)
{
    Response.Clear();

    Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
    Response.Charset = "";

    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

    Response.ContentType = "application/vnd.xls";

    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

// I Tried using following (but with no success)
//-----Trial Starts----------------
//foreach (GridViewRow gvr in gvProgramList.Rows)
//    {
//        CheckBox cbox = (CheckBox)gvr.FindControl("cboxSelect");
//        if(cbox.Checked)
//            gvr.Visible = true;
//        else
//            gvr.Visible = false;
//    }
//--------Trial ends---------------
    grdGridView.DataBind();
    ClearControls(grdGridView);

    // Throws exception: Control 'ComputerGrid' of type 'GridView'
    // must be placed inside a form tag with runat=server.
    // ComputerGrid.RenderControl(htmlWrite);

    // Alternate to ComputerGrid.RenderControl above
    System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
    Controls.Add(form);



    form.Controls.Add(grdGridView);
    form.RenderControl(htmlWriter);
    Response.Write(stringWriter.ToString());
    Response.End();
    foreach (GridViewRow gvr in gvProgramList.Rows)
    {
        CheckBox cbox = (CheckBox)gvr.FindControl("cboxSelect");
            gvr.Visible = true;
    }
    grdGridView.DataBind();
}

private void ClearControls(Control control)
{
    for (int i = control.Controls.Count - 1; i >= 0; i--)
    {
        ClearControls(control.Controls[i]);
    }

    if (!(control is TableCell))
    {
        if (control.GetType().GetProperty("SelectedItem") != null)
        {
            LiteralControl literal = new LiteralControl();
            control.Parent.Controls.Add(literal);
            try
            {
                literal.Text =
                    (string)control.GetType().GetProperty("SelectedItem").
                        GetValue(control, null);
            }
            catch
            { }
            control.Parent.Controls.Remove(control);
        }
        else if (control.GetType().GetProperty("Text") != null)
        {
            LiteralControl literal = new LiteralControl();
            control.Parent.Controls.Add(literal);
            literal.Text =
                (string)control.GetType().GetProperty("Text").
                    GetValue(control, null);
            control.Parent.Controls.Remove(control);
        }
    }
    return;
}

protected void btnDownload_Click(object sender, EventArgs e)
{
    if (gvProgramList.Rows.Count > 0)
    {
        ExportGridToExcel(gvProgramList, "ProgramList");
    }
}
4

2 回答 2

0

我可以建议你这样做的逻辑:

1.使用您的gridview的选定行创建一个动态数据表。它只是遍历gridview行并将选定的行获取并附加到新的数据表中。

2.然后编写将这个新数据表转换为excel表的代码(谷歌“将数据表转换为excel”的结果很多)

于 2013-01-01T14:55:11.360 回答
0
Try like the below code,hope it helps you..

在下载按钮点击事件上,调用这个 fn

private void ExportToExcell()
{       
        DataTable dt = new DataTable();
        dt.Columns.Add("Plan ID");
        dt.Columns.Add("Plan Name");
        dt.Columns.Add("Balance");
        foreach (GridViewRow row in gdvBal.Rows)
        {
            CheckBox chkCalls = (CheckBox)row.FindControl("chkCalls");
            if (chkCalls.Checked == true)
            {
                int i = row.RowIndex;
                Label lblPlanId = (Label)gdvBal.Rows[i].FindControl("lblPlanId");
                Label lblPlanName = (Label)gdvBal.Rows[i].FindControl("lblPlanName");
                Label lblBalance = (Label)gdvBal.Rows[i].FindControl("lblBalance");

                DataRow dr = dt.NewRow();
                dr["Plan ID"] = Convert.ToString(lblPlanId.Text);
                dr["Plan Name"] = Convert.ToString(lblPlanName.Text);
                dr["Balance"] = Convert.ToString(lblBalance.Text);
                dt.Rows.Add(dr);
            }
        }
        GridView gdvExportxls = new GridView();
        gdvExportxls.DataSource = dt;
        gdvExportxls.DataBind();
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/ms-excel";
        Response.AddHeader("content-disposition", string.Format("attachment;filename=BillingForfBalances.xls", "selectedrows"));
        Response.Charset = "";
        StringWriter stringwriter = new StringWriter();
        HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
        gdvExportxls.RenderControl(htmlwriter);
        Response.Write(stringwriter.ToString().Replace("<div>", " ").Replace("</div>", " "));
        Response.End();

}
于 2013-01-02T09:11:21.007 回答