JG Vimalan's Blog

It is all about coding!

Export GridView, Repeater to Excel.

In this post I am going to explain exporting a gridview, repeater to excel. I assume that, you have a gridview in your asp.net web page. Now place a button in the web page and set the text property to ‘Export’. So your web page will look like,

Place the following code in the click event of the export button.

protected void imbExportToExcel_Click(object sender, ImageClickEventArgs e)
        Response.Buffer = true;
        Response.AddHeader(“content-disposition”, “attachment;filename=Sample.xls”);
        Response.ContentType = “application/vnd.ms-excel”;

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);


        String sHtmlText = RemoveHyperLinkTag(stringWrite.ToString());

        //here, i am removing the unwanted style settings

       sHtmlText = sHtmlText.
       Replace(“background-color:#CACACA;”, “”).
       Replace(“background-color:#990000;”, “”).
       Replace(“font-size:8pt;”, “font-size:10pt;”);
       Response.Write(sHtmlText.Replace(“<div>”, “”).Replace(“</div>”,””));

Here, you could see that, I have a method named ‘PrepareGridBeforeExport’. The purpose of this method is to hide unnecessary controls in my gridview.

void PrepareGridBeforeExport(bool value)
        myGridView.Columns[0].Visible = value;
        DropDownList oDDLDate = (DropDownList)
       oDDLDate.Visible = value;       


if you are using a master page then, you need to just override the VerifyRenderingInServerForm method. No code is required in that method.

public override void VerifyRenderingInServerForm(Control control)

Also, add the following in the page attribute,


Now, when you click the export button, your GridView contents will be exported to excel and a popup dialog will be displayed as shown below,

The same logic is applicable to Repeater control.


October 19, 2010 - Posted by | ASP.NET, C#.NET


  1. hi, although the code works fine in one of my gridview control, how do I hide my asp.net controls (like dropdown list, label) in the gridview header?

    Comment by Ben | October 25, 2010 | Reply

    • You need to make the unwanted controls invisible. Please see the PrepareGridBeforeExport method in the same post. Just locate the controls in gidview header part and make them invisible. That will solve your problem.

      Comment by jgvimalan | October 25, 2010 | Reply

  2. Hi,

    You don’t need to have this “EnableEventValidation=”false”” for Repeater to Excel.

    Comment by Ramesh | October 26, 2010 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: