Prepare controls to remove while exporting GridView to Excel


In this artical i'm trying to explain remove controls while exporting gridview data to excel. Previously we did export data to excel or some other formats but here i'm trying to explain in Grid Header having controls means i want to remove all those controls while exporting and then export to excel.

Prepare Controls to remove while export GridView to Excel:



In this artical i'm trying to explain remove controls while exporting gridview data to excel. Previously we did export data to excel or some other formats but here i'm trying to explain in Grid Header having controls means i want to remove all those controls while exporting and then export to excel.

My GridView is look like this

GridView:



GV

In my GridView Header i have some controls, while exporting with that controls i'm facing some what dificulties. So, before exporting i must delete those controls and then exported using the below code.

Code Behind:




private static void RemoveControls(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is DropDownList)
{
control.Controls.Remove(current);

}
else if (current is TextBox)
{
control.Controls.Remove(current);

}
else if (current is Button)
{
control.Controls.Remove(current);

}
else if (current is AjaxControlToolkit.TextBoxWatermarkExtender)
{
control.Controls.Remove(current);

}
else if (current is LinkButton)
{
control.Controls.Remove(current);

}


if (current.HasControls())
{
PrepareControlForExport(current);
}
}
}
public static void ExportToExcel(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}.xls", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";

using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table();

// add the header row to the table
if (gv.HeaderRow != null)
{
RemoveControls(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}

// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
RemoveControls(row);
table.Rows.Add(row);
}

// add the footer row to the table
if (gv.FooterRow != null)
{
RemoveControls(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}

// render the table into the htmlwriter
table.RenderControl(htw);

// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}


OutPut:



Exporting

Using above code we remove controls in HeaderTemplate and export data to excel we fetch our desired out put using this.


Article by naveensanagasetti
I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

Comments

Author: Phagu Mahato23 Dec 2013 Member Level: Gold   Points : 6

You can also use given code snippet for controls to remove while exporting GridView to Excel

protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=User.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.Columns[14].Visible = false;
GridView1.Columns[15].Visible = false;
GridView1.Columns[16].Visible = false;
BindGird();
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}

GridView1.RenderControl(hw);

string style = @"";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

GridView1.Dispose();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/*control is rendered */
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: