Export GridView to Excel – C#

Posted by rameshch on July 30, 2008

In previous post (Export a GridView to Excel - C#)  am unable to add different colors to head and rows.

The following code solves this problem 

public static void ExportGridView(string fileName,GridView  gv){ 

HttpContext.Current.Response.ClearContent();

 

 

 

 

 

HttpContext.Current.Response.AddHeader(“content-disposition”, string.Format(“attachment; filename={0}”

 

, fileName)); HttpContext.Current.Response.ContentType = “application/ms-excel”; 

 

StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw);gv.RenderControl(htw);

HttpContext.Current.Response.Write(sw.ToString()); 

 

HttpContext.Current.Response.End();}

///

<summary>///

Replace any of the contained controls with literals

/// </summary> // /<param name=”gvcontrol”></param> 

 

 

ublic static void PrepareGridViewForExport(Control gvcontrol){

 

 

 for (int i = 0; i < gvcontrol.Controls.Count; i++){ 

 

 

Control current = gvcontrol.Controls[i]; 

if (current is LinkButton){

gvcontrol.Controls.Remove(current);

 

gvcontrol.Controls.AddAt(i,

 

 

 

 

new LiteralControl((current as LinkButton).Text));}

 

 else if (current is ImageButton){

 

 

gvcontrol.Controls.Remove(current);

 

gvcontrol.Controls.AddAt(i,new LiteralControl((current as ImageButton).AlternateText));}

 

 

 

 else if (current is Image){

 

 

gvcontrol.Controls.Remove(current);

 

gvcontrol.Controls.AddAt(i,new LiteralControl((current as Image).AlternateText));}

 

 

 

 

 

 

 

 

 

else if (current is HyperLink

){

 

gvcontrol.Controls.Remove(current);

 

gvcontrol.Controls.AddAt(i,

 

 

 

 

 

new LiteralControl((current as HyperLink

).Text));}

 

 

 

 

 

 

 

 

else if (current is DropDownList

){

 

gvcontrol.Controls.Remove(current);

 

gvcontrol.Controls.AddAt(i,

 

 

 

 

new LiteralControl((current as DropDownList

).SelectedItem.Text));}

 

 

 

 

 

 

 

 

else if (current is CheckBox

){

 

gvcontrol.Controls.Remove(current);

 

gvcontrol.Controls.AddAt(i,

 

 

 

 

 

new LiteralControl((current as CheckBox).Checked ? “True” : “False”

));}

 

 

 

 

 

 

 

 

if (current.HasControls())

 

{

 

PrepareGridViewForExport(current);

}

 

 

In quite a few cases, developers face an error in the Export functionality – typically the error message is “RegisterForEventValidation can only be called during Render();”.

 

 

  • Step 1: Implement the Export functionality as described above.
  • Step 2: Remove the code to override the VerifyRenderingInServerForm method.
  • Step 3: Modify the code for the ExportGridView function as below. The code highlighted in green creates and HtmlForm on the fly, before exporting the gridview, adds the gridview to this new form and renders the form (instead of rendering the gridview in our original implementation) 

private void ExportGridView()

{

          string attachment = “attachment; filename=Contacts.xls”;

          Response.ClearContent();

          Response.AddHeader(“content-disposition”, attachment);

          Response.ContentType = “application/ms-excel”;

          StringWriter sw = new StringWriter();

          HtmlTextWriter htw = new HtmlTextWriter(sw);

 

          // Create a form to contain the grid

          HtmlForm frm = new HtmlForm();

          GridView1.Parent.Controls.Add(frm);

          frm.Attributes["runat"] = “server”;

          frm.Controls.Add(GridView1);

 

          frm.RenderControl(htw);

          //GridView1.RenderControl(htw);

          Response.Write(sw.ToString());

          Response.End();

}

 

This implementation has the advantage that it can be setup as re-usable code in a separate library, without having to override the base class method each time.

 

Leave a Reply

You must be logged in to post a comment.