无法将动态添加的行导出到 Gridview 到 Excel
我尝试过将 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
可以帮助我吗?为什么我无法导出最后一行。 提前致谢
I have tried Exporting GridView to Excel but observed that
the Dynamically added last Row to Gridview is not exported to excel.
I have two datasets first one binds the data directly to Gridview.
After Which I add the last row from another DataSet.
In the page I'm able to see the result as Expected but when exported excel I'm not.
Below is my code:
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();
Adding Overall row at bottom:
#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
and Last my code to Export the 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. */
}
the Export Method in TrainingUtil class
#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
Can any help me out.Why I'm not able to export the last row.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在每一篇文章中你都没有绑定动态添加的行。
尝试找到引起回发的控件,重新绑定数据。
查找回发控件的代码例如:-
I think in every post back your not binding the dynamically added rows.
Try to find the control which cause the postback and bind the data once again.
Code to find the postback control ex:-