January 19, 2009

Export GridView to Excel




Users often post a requirement to export the data in an ASP.NET GridView to Excel. Here's a simple way to do so:

C#


protected void Button1_Click(object sender, EventArgs e)

{

    Response.AddHeader("content-disposition", 
       "attachment;filename=FileName.xls");

    Response.Charset = String.Empty;

    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter sw = new System.IO.StringWriter();

    System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.RenderControl(hw);

    Response.Write(sw.ToString());

    Response.End();

}



VB.NET


    Protected Sub Button1_Click(ByVal sender As Object, _
     ByVal e As EventArgs)

        Response.AddHeader("content-disposition", _
         "attachment;filename=FileName.xls")

        Response.Charset = String.Empty

        Response.ContentType = "application/vnd.xls"

        Dim sw As System.IO.StringWriter = _
          New System.IO.StringWriter()

        Dim hw As System.Web.UI.HtmlTextWriter = _
            New HtmlTextWriter(sw)

        GridView1.RenderControl(hw)

        Response.Write(sw.ToString())

        Response.End()

    End Sub



To read about some more tips and tricks about the GridView, check GridView Tips and Tricks using ASP.NET 2.0

'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

8 Responses to "Export GridView to Excel"
  1. Peggy said...
    January 27, 2009 2:36 PM

    Trying the VB code I get: System.Web.HttpException: Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

  2. Suprotim Agarwal said...
    January 28, 2009 3:12 AM

    Peggy, This was a known issue filed over here

    A possible solution: http://forums.asp.net/t/1239899.aspx

  3. Abhilash said...
    February 16, 2009 6:25 AM

    Hi,
    I doubt.
    I tried the code-segment in the post. When i click 'Button_1', the page simply refreshes & no excel files are generated.
    Please note, my pc doesnt have any version of MS office installed. Im using OpenOffice3.0. Is it due to that...???
    Thanks.

  4. Abhilash said...
    February 17, 2009 1:52 AM

    Oh!
    I solved it. Has to do some searching in MSDN gallery and solved via including the below code inot my code-behind.

    ---
    [code-behind]
    public override void VerifyRenderingInServerForm(Control control)
    {}

  5. Suprotim Agarwal said...
    February 18, 2009 6:02 AM

    Glad to hear that you solved it! Can you share the complete solution for the benefit of future users.Thanks.

  6. Abhilash said...
    February 22, 2009 10:34 PM

    Thanks for your attention.
    While building the sample, i encountered 2 problems. First, when i click the Button_1 for downloading, the page simply refreshes & no excel files are generated. When I build the website second time, I's getting the same error what Peggy was having.
    The first error was solved automatically(i dont know how!). Peggy's error was solved when i include the 'VerifyRenderingInServerForm()' method in the code-behind.

    Hence the complete code looks like
    [Code-Behind]
    protected void Button1_Click(object sender, EventArgs e)
    {
    // Same as above code
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    // Empty Method
    }
    ---

    Thankz.

  7. Praveen said...
    October 9, 2010 2:35 AM

    Here is complete details about all problems we face in this process:
    http://praveenbattula.blogspot.com/2010/09/gridview-and-export-
    to-excel.html

  8. Anonymous said...
    November 22, 2011 1:32 PM

    Wow Abhilash!!!! thank you! you save my life! with that response!!!!

    really work!

    greetings from Colombia

 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions