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 a Dataset to a csv file.

Export a Dataset to a csv file.

Scheduled Pinned Locked Moved Visual Basic
3 Posts 2 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.
  • U Offline
    U Offline
    UniBond
    wrote on last edited by
    #1

    I dont think this wouldbe to difficult but can anyone provide me a piece of code to do this: I would like the end result to look like this: ID,MyName,MyAddrerss,MyAge ect...

    C 1 Reply Last reply
    0
    • U UniBond

      I dont think this wouldbe to difficult but can anyone provide me a piece of code to do this: I would like the end result to look like this: ID,MyName,MyAddrerss,MyAge ect...

      C Offline
      C Offline
      chrismerrill
      wrote on last edited by
      #2

      Here is one possible method to write a DataTable to a csv file. If you want to write out a whole DataSet to csv files, then you could loop through each table in your DataSet and produce a file for each table. Hope this helps :)

      Public Sub WriteCsv(dt as datatable, filename as string, delimiter as char)

       dim writer as System.IO.StreamWriter
      
       try
            writer = new System.IO.StreamWriter(filename)
      
            'write the column names
            for i as integer = 0 to dt.Columns.Count - 1
                 writer.write(dt.Columns(i).ColumnName)
                 if i <> dt.Columns.Count - 1 then writer.write (delimiter)
            next
      
            writer.write(controlchars.newline)
      
            'write the data
            For i As Integer = 0 To dt.Rows.Count - 1
                 For j As Integer = 0 To dt.Columns.Count - 1
                      writer.Write(dt.Rows(i).Item(j))
                      If j <> dt.Columns.Count - 1 Then writer.Write(delimiter)
                 Next
                 if i <> dt.Rows.Count - 1 then writer.Write(ControlChars.NewLine)
            Next
      
            writer.Flush()
            writer.Close()
            writer = Nothing
       Catch ex As Exception
            If Not writer Is Nothing Then
                 writer = Nothing
            End If
      End Try
      

      End Sub

      U 1 Reply Last reply
      0
      • C chrismerrill

        Here is one possible method to write a DataTable to a csv file. If you want to write out a whole DataSet to csv files, then you could loop through each table in your DataSet and produce a file for each table. Hope this helps :)

        Public Sub WriteCsv(dt as datatable, filename as string, delimiter as char)

         dim writer as System.IO.StreamWriter
        
         try
              writer = new System.IO.StreamWriter(filename)
        
              'write the column names
              for i as integer = 0 to dt.Columns.Count - 1
                   writer.write(dt.Columns(i).ColumnName)
                   if i <> dt.Columns.Count - 1 then writer.write (delimiter)
              next
        
              writer.write(controlchars.newline)
        
              'write the data
              For i As Integer = 0 To dt.Rows.Count - 1
                   For j As Integer = 0 To dt.Columns.Count - 1
                        writer.Write(dt.Rows(i).Item(j))
                        If j <> dt.Columns.Count - 1 Then writer.Write(delimiter)
                   Next
                   if i <> dt.Rows.Count - 1 then writer.Write(ControlChars.NewLine)
              Next
        
              writer.Flush()
              writer.Close()
              writer = Nothing
         Catch ex As Exception
              If Not writer Is Nothing Then
                   writer = Nothing
              End If
        End Try
        

        End Sub

        U Offline
        U Offline
        UniBond
        wrote on last edited by
        #3

        Nice one worked a treat..

        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