Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Export GridView Only to Excel in asp.net

Export GridView Only to Excel in asp.net

Scheduled Pinned Locked Moved ASP.NET
csharpcssasp-netdatabasetutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abdul Rahman Hamidy
    wrote on last edited by
    #1

    Dear All, I am exporting grid view to excel using following code

    void Export()
    {
    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);
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
    }

    but what happens, It exports all the control to excel. I have button, listbox and other controls above my GridView but when exporting I want to export only my gridview to Excel using above code all the page is exported, any ideas how to export only gridview to excel?

    Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

    D A 2 Replies Last reply
    0
    • A Abdul Rahman Hamidy

      Dear All, I am exporting grid view to excel using following code

      void Export()
      {
      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);
      GridView1.RenderControl(htw);
      Response.Write(sw.ToString());
      Response.End();
      }

      but what happens, It exports all the control to excel. I have button, listbox and other controls above my GridView but when exporting I want to export only my gridview to Excel using above code all the page is exported, any ideas how to export only gridview to excel?

      Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

      D Offline
      D Offline
      Dinesh Mani
      wrote on last edited by
      #2

      Your code looks clean to me. It should work. Have you overridden VerifyRenderingInServerForm method?

      public override void VerifyRenderingInServerForm(Control control)
      {
      
          // Confirms that an HtmlForm control is rendered for the 
          // specified ASP.NET server control at run time.
      
      }
      
      A 1 Reply Last reply
      0
      • A Abdul Rahman Hamidy

        Dear All, I am exporting grid view to excel using following code

        void Export()
        {
        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);
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        }

        but what happens, It exports all the control to excel. I have button, listbox and other controls above my GridView but when exporting I want to export only my gridview to Excel using above code all the page is exported, any ideas how to export only gridview to excel?

        Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

        A Offline
        A Offline
        Arun Jacob
        wrote on last edited by
        #3

        This is a working sample, Export Gridview in ASP.NET[^]

        Arun Jacob http://codepronet.blogspot.com/

        A 1 Reply Last reply
        0
        • D Dinesh Mani

          Your code looks clean to me. It should work. Have you overridden VerifyRenderingInServerForm method?

          public override void VerifyRenderingInServerForm(Control control)
          {
          
              // Confirms that an HtmlForm control is rendered for the 
              // specified ASP.NET server control at run time.
          
          }
          
          A Offline
          A Offline
          Abdul Rahman Hamidy
          wrote on last edited by
          #4

          I have already done that but no success.

          Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

          1 Reply Last reply
          0
          • A Arun Jacob

            This is a working sample, Export Gridview in ASP.NET[^]

            Arun Jacob http://codepronet.blogspot.com/

            A Offline
            A Offline
            Abdul Rahman Hamidy
            wrote on last edited by
            #5

            thanks for your reply. I have done the same thing, but all controls within the page are rendered and exported to excel file. I want to export only the gridview not other controls.

            Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups