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. General Programming
  3. Visual Basic
  4. Export DataGridView to Excel (xls) format

Export DataGridView to Excel (xls) format

Scheduled Pinned Locked Moved Visual Basic
questionhelp
7 Posts 4 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.
  • V Offline
    V Offline
    veljkoz
    wrote on last edited by
    #1

    Hi! I need to export data contained in the DataGridView control to Excel. The code provided here does the work, but not fully - when Excel opens the file, it recognises it like a "Tab delimited values" and import wizard appears. This is too complicated for (my) end users, so I need a way to write "real" xls file, without the need of end user action. Do you know how can I do this? The code is: '(tab is DataGridView control containing data) Dim fileText As System.Text.StringBuilder = New System.Text.StringBuilder() 'exporting column names For Each col As DataGridViewColumn In tab.Columns If col.Visible = True Then fileText.Append(col.Name) fileText.Append(vbTab) End If Next fileText.Append(vbCrLf) 'export cell values For Each row As DataGridViewRow In tab.Rows For Each theCell As DataGridViewCell In row.Cells If theCell.Visible = True Then fileText.Append(theCell.Value.ToString()) fileText.Append(vbTab) End If Next fileText.Append(vbCrLf) 'moving to the next row Next My.Computer.FileSystem.WriteAllText("Test.xls", fileText.ToString(), False) Any help would be appreciated! Thank you in advance! Veljko

    B M 2 Replies Last reply
    0
    • V veljkoz

      Hi! I need to export data contained in the DataGridView control to Excel. The code provided here does the work, but not fully - when Excel opens the file, it recognises it like a "Tab delimited values" and import wizard appears. This is too complicated for (my) end users, so I need a way to write "real" xls file, without the need of end user action. Do you know how can I do this? The code is: '(tab is DataGridView control containing data) Dim fileText As System.Text.StringBuilder = New System.Text.StringBuilder() 'exporting column names For Each col As DataGridViewColumn In tab.Columns If col.Visible = True Then fileText.Append(col.Name) fileText.Append(vbTab) End If Next fileText.Append(vbCrLf) 'export cell values For Each row As DataGridViewRow In tab.Rows For Each theCell As DataGridViewCell In row.Cells If theCell.Visible = True Then fileText.Append(theCell.Value.ToString()) fileText.Append(vbTab) End If Next fileText.Append(vbCrLf) 'moving to the next row Next My.Computer.FileSystem.WriteAllText("Test.xls", fileText.ToString(), False) Any help would be appreciated! Thank you in advance! Veljko

      B Offline
      B Offline
      Bulbul Bhuwania
      wrote on last edited by
      #2

      is this a windows application Bulbul Bhuwania

      V 1 Reply Last reply
      0
      • V veljkoz

        Hi! I need to export data contained in the DataGridView control to Excel. The code provided here does the work, but not fully - when Excel opens the file, it recognises it like a "Tab delimited values" and import wizard appears. This is too complicated for (my) end users, so I need a way to write "real" xls file, without the need of end user action. Do you know how can I do this? The code is: '(tab is DataGridView control containing data) Dim fileText As System.Text.StringBuilder = New System.Text.StringBuilder() 'exporting column names For Each col As DataGridViewColumn In tab.Columns If col.Visible = True Then fileText.Append(col.Name) fileText.Append(vbTab) End If Next fileText.Append(vbCrLf) 'export cell values For Each row As DataGridViewRow In tab.Rows For Each theCell As DataGridViewCell In row.Cells If theCell.Visible = True Then fileText.Append(theCell.Value.ToString()) fileText.Append(vbTab) End If Next fileText.Append(vbCrLf) 'moving to the next row Next My.Computer.FileSystem.WriteAllText("Test.xls", fileText.ToString(), False) Any help would be appreciated! Thank you in advance! Veljko

        M Offline
        M Offline
        mayhem_rules
        wrote on last edited by
        #3

        I am having the same problem. Please provide me the solution if you happen to get one. With Best Regards, Mayur

        A 1 Reply Last reply
        0
        • B Bulbul Bhuwania

          is this a windows application Bulbul Bhuwania

          V Offline
          V Offline
          veljkoz
          wrote on last edited by
          #4

          Yes, this is a windows application (there are many solutions on the net for asp / web, but I've found none for windows app). Can you help me?

          1 Reply Last reply
          0
          • M mayhem_rules

            I am having the same problem. Please provide me the solution if you happen to get one. With Best Regards, Mayur

            A Offline
            A Offline
            albCode
            wrote on last edited by
            #5

            check this http://gridviewguy.com/ArticleDetails.aspx?articleID=26

            V 1 Reply Last reply
            0
            • A albCode

              check this http://gridviewguy.com/ArticleDetails.aspx?articleID=26

              V Offline
              V Offline
              veljkoz
              wrote on last edited by
              #6

              albCode, thank you for the reply, but I have allready found many sources for converting in ASP using web pages.. What I need has to be done using Windows application.

              A 1 Reply Last reply
              0
              • V veljkoz

                albCode, thank you for the reply, but I have allready found many sources for converting in ASP using web pages.. What I need has to be done using Windows application.

                A Offline
                A Offline
                albCode
                wrote on last edited by
                #7

                http://www.vbfrance.com/codes/REQUETE-SQL-MULTI-TABLES-CHARGEE-DANS-DATAGRID-EXPORTATION\_34985.aspx http://www.readablog.com/feed9160.aspx

                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