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. converting gridview to excel in asp.net

converting gridview to excel in asp.net

Scheduled Pinned Locked Moved ASP.NET
csharpasp-net
7 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.
  • J Offline
    J Offline
    jadhavShubhangi
    wrote on last edited by
    #1

    hi i am using following code to export gridview to excel..but the generated excel file does not contain rows and columns as normal excel file ... can any on e suggest a solution for that here is my code StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); String savefile = heading; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + savefile + ".xls"); HttpContext.Current.Response.Charset = ""; gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();

    K N 2 Replies Last reply
    0
    • J jadhavShubhangi

      hi i am using following code to export gridview to excel..but the generated excel file does not contain rows and columns as normal excel file ... can any on e suggest a solution for that here is my code StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); String savefile = heading; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + savefile + ".xls"); HttpContext.Current.Response.Charset = ""; gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();

      K Offline
      K Offline
      Khaja A Imtiaz
      wrote on last edited by
      #2

      use the below code in you ExporttoExcel function

      using conn as new SqlConnection(Constring)
      conn.open()
      da=new sqlDataAdapter(query,conn)
      da.fill(ds)
      End Using

      Dim Gridview1 as New Gridview
      Gridview1.datasource=ds
      Gridview1.databind()
      Response.clear()
      Response.Buffer=true
      Response.AddHeader("content-disposition","attachment;filename=testfile.xls")
      Response.charset=""
      Response.ContentType="application/vdn.ms-excel"
      Dim sw as new StringWriter()

      Dim hw as new HtmlTextWriter(sw)

      For i as integer=0 To Gridview1.Rows.Count-1

      Gridview1.Row(i).Attribute.Add("class","textmode")
      Next
      Gridview1.RenderControl(hw)

      dim style as String="<style> .textmode(mso-number-format:\@;}</style>"

      Response.write(style)
      Response.output.write(sw.ToString())
      Response.Flush()
      Response.End()

      Imtiaz A.K

      J 1 Reply Last reply
      0
      • K Khaja A Imtiaz

        use the below code in you ExporttoExcel function

        using conn as new SqlConnection(Constring)
        conn.open()
        da=new sqlDataAdapter(query,conn)
        da.fill(ds)
        End Using

        Dim Gridview1 as New Gridview
        Gridview1.datasource=ds
        Gridview1.databind()
        Response.clear()
        Response.Buffer=true
        Response.AddHeader("content-disposition","attachment;filename=testfile.xls")
        Response.charset=""
        Response.ContentType="application/vdn.ms-excel"
        Dim sw as new StringWriter()

        Dim hw as new HtmlTextWriter(sw)

        For i as integer=0 To Gridview1.Rows.Count-1

        Gridview1.Row(i).Attribute.Add("class","textmode")
        Next
        Gridview1.RenderControl(hw)

        dim style as String="<style> .textmode(mso-number-format:\@;}</style>"

        Response.write(style)
        Response.output.write(sw.ToString())
        Response.Flush()
        Response.End()

        Imtiaz A.K

        J Offline
        J Offline
        jadhavShubhangi
        wrote on last edited by
        #3

        thanks for reply i am new in asp.net and unaware in vb.net pls give me code in asp.net. Thnaks a lot

        K J 2 Replies Last reply
        0
        • J jadhavShubhangi

          thanks for reply i am new in asp.net and unaware in vb.net pls give me code in asp.net. Thnaks a lot

          K Offline
          K Offline
          Khaja A Imtiaz
          wrote on last edited by
          #4

          It is in Asp.Net only... When u write code in ASP.net, it means you have to choose the language you prefer to code either vb.net or C# etc.. the code which i have send is used in asp.net application only... and if u are using the C# as code behind then.. convert this piece of code into c# as we have many online converters... paste the code they will give converted format in C#... Regards Imtiaz

          Imtiaz A.K

          1 Reply Last reply
          0
          • J jadhavShubhangi

            thanks for reply i am new in asp.net and unaware in vb.net pls give me code in asp.net. Thnaks a lot

            J Offline
            J Offline
            jadhavShubhangi
            wrote on last edited by
            #5

            thanx it works as it is I previously return code for this........

            1 Reply Last reply
            0
            • J jadhavShubhangi

              hi i am using following code to export gridview to excel..but the generated excel file does not contain rows and columns as normal excel file ... can any on e suggest a solution for that here is my code StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); String savefile = heading; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + savefile + ".xls"); HttpContext.Current.Response.Charset = ""; gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();

              N Offline
              N Offline
              nagendrathecoder
              wrote on last edited by
              #6

              Have you tried earlier solutions?

              J 1 Reply Last reply
              0
              • N nagendrathecoder

                Have you tried earlier solutions?

                J Offline
                J Offline
                jadhavShubhangi
                wrote on last edited by
                #7

                yes, I tried this one.

                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