【问题标题】:Unable to Export the Dynamically added row to Gridview to Excel无法将动态添加的行导出到 Gridview 到 Excel
【发布时间】:2011-08-02 10:53:41
【问题描述】:

我曾尝试将 GridView 导出到 Excel,但观察到 动态添加到 Gridview 的最后一行不会导出到 excel。

我有两个数据集,第一个将数据直接绑定到 Gridview。

之后我从另一个数据集中添加最后一行。

在页面中,我可以看到预期的结果,但导出 excel 时却没有。 以下是我的代码:

DataSet dsgrid = SqlHelper.ExecuteDataset(DBConnectionString.ConnectionString, CommandType.StoredProcedure, "usp_Training_GetCirclescoreCardReport  ", sqlparam);

    if (TrainingUtil.isDataSetValid(dsgrid))
    {

        RSGScoreCard_Grid.DataSource = dsgrid;
        RSGScoreCard_Grid.DataBind();
        AddOverallRow(dsgrid);
    }
    else RSGScoreCard_Grid.DataBind();

在底部添加整体行:

 #region Add OverallRow
private void AddOverallRow(DataSet dsgrid)
{
    using (GridViewRow gr = new GridViewRow(RSGScoreCard_Grid.Rows.Count + 1, 0, DataControlRowType.DataRow, DataControlRowState.Normal))
    {
        for (int i = 0; i < 6; i++)//6 is the column count for overall row
        {
            using (TableCell tc = new TableCell())
            {
                gr.Cells.Add(tc);
                if (i == 0)
                {
                    gr.Cells[i].ColumnSpan = 4;
                    gr.Cells[i].Text = "Overall";
                    gr.Cells[i].Attributes.Add("class", "fcol");
                    gr.Cells[i].Attributes.Add("style", "font-weight:bold;padding-left:20%");
                }
                else gr.Cells[i].Attributes.Add("style", "font-weight:bold");

            }
        }


        if (dsgrid.Tables[1] != null)//creating a dynamic row to gridview
            if (dsgrid.Tables[1].Rows.Count > 0)
            {
                gr.Cells[1].Text = dsgrid.Tables[1].Rows[0][5].ToString();
                gr.Cells[1].Width = Unit.Percentage(8);
                gr.Cells[2].Text = dsgrid.Tables[1].Rows[0][6].ToString();
                gr.Cells[2].Width = Unit.Percentage(8);
                gr.Cells[3].Text = dsgrid.Tables[1].Rows[0][7].ToString();
                gr.Cells[3].Width = Unit.Percentage(8);
                gr.Cells[4].Text = dsgrid.Tables[1].Rows[0][8].ToString();
                gr.Cells[4].Width = Unit.Percentage(8);
                gr.Cells[5].Text = dsgrid.Tables[1].Rows[0][9].ToString();
                gr.Cells[5].Width = Unit.Percentage(8);
            }
        gr.Attributes.Add("class", "row2");

        RSGScoreCard_Grid.Controls[0].Controls.AddAt(RSGScoreCard_Grid.Rows.Count + 1, gr);


    }
}
#endregion

最后我的代码导出 GrieView:

 protected void btnExport_Click(object sender, EventArgs e)
{
    TrainingUtil.Export(ddlOptions.SelectedItem.Text.ToString().Replace(" ", string.Empty) + "_" + ddlVerticals.SelectedItem.Text.ToString().Replace(" ", string.Empty) + "_" + ddlLernerGroups.SelectedItem.Text.ToString().Replace(" ", string.Empty), RSGScoreCard_Grid, "For the Month/Year: " + ddlFromMonths.SelectedItem.Text.ToString()+"/"+ddlYears.SelectedItem.Text.ToString(), RSGScoreCard_Grid.HeaderRow.Cells.Count);
}
public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
       server control at run time. */
}

TrainingUtil 类中的导出方法

  #region Export

public static void Export(string filename, GridView grid, string Heading, int ColumnsCount)
{

    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename + ".xls"));
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter htw = new HtmlTextWriter(sw))
        {
            grid.HeaderStyle.BackColor = System.Drawing.Color.Cyan;
            //Cells color settings
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
            TableCell cell = new TableCell();
            cell.Text = String.Format("{0}", Heading);
            cell.ColumnSpan = ColumnsCount;
            cell.Attributes.Add("align", "center");
            cell.Attributes.Add("class", "yellow");
            row.Cells.Add(cell);
            grid.Controls[0].Controls.AddAt(0, row);
            foreach (GridViewRow gridRow in grid.Rows)
            {
                foreach (TableCell tcGridCells in gridRow.Cells)
                {
                    tcGridCells.Attributes.Add("class", "sborder");
                }
            }

            grid.RenderControl(htw);
            //Add the style sheet class here
            HttpContext.Current.Response.Write(@"<style> .sborder { color : Black;border : 1px Solid Black; } .yellow {background-color:yellow;color:black;} </style> ");
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }

}
#endregion

谁能帮帮我。为什么我无法导出最后一行。 提前致谢

【问题讨论】:

    标签: c# asp.net export-to-excel


    【解决方案1】:

    我认为在每篇文章中你都没有绑定动态添加的行。 尝试找到导致回发的控件并重新绑定数据。

    查找回发控件前的代码:-

     public static Control GetPostBackControl(Page page)
    {
        Control control = null;
    
        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if (ctrlname != null && ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    } 
    

    【讨论】:

    • 谢谢@Ganesha。它帮助了我。
    • 您好@Ganesha 在考虑了回发过程之后,我直接在按钮单击事件中编写了代码,而不是找到控件并调用函数。
    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 2011-10-05
    • 2016-07-08
    • 1970-01-01
    • 2013-09-25
    相关资源
    最近更新 更多