Results 1 to 8 of 8

Thread: Converting textfile to datatable and converting datatable to textfile

Threaded View

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,372

    Converting textfile to datatable and converting datatable to textfile

    I wrote a function that will convert the contents of a text file to a datatable:
    Code:
    Private Function Text_To_DataTable(ByVal path As String, ByVal delimitter As Char, ByVal header As Boolean) As DataTable
        Dim source As String = String.Empty
        Dim dt As DataTable = New DataTable
    
        If IO.File.Exists(path) Then
            source = IO.File.ReadAllText(path)
        Else
            Throw New IO.FileNotFoundException("Could not find the file at " & path, path)
        End If
    
        Dim rows() As String = source.Split({Environment.NewLine}, StringSplitOptions.None)
    
        For i As Integer = 0 To rows(0).Split(delimitter).Length - 1
            Dim column As String = rows(0).Split(delimitter)(i)
            dt.Columns.Add(If(header, column, "column" & i + 1))
        Next
    
        For i As Integer = If(header, 1, 0) To rows.Length - 1
            Dim dr As DataRow = dt.NewRow
    
            For x As Integer = 0 To rows(i).Split(delimitter).Length - 1
                If x <= dt.Columns.Count - 1 Then
                    dr(x) = rows(i).Split(delimitter)(x)
                Else
                    Throw New Exception("The number of columns on row " & i + If(header, 0, 1) & " is greater than the amount of columns in the " & If(header, "header.", "first row."))
                End If
            Next
    
            dt.Rows.Add(dr)
        Next
    
        Return dt
    End Function
    Code:
    Private Sub DataTable_To_Text(ByVal table As DataTable, ByVal path As String, ByVal header As Boolean, ByVal delimiter As Char)
        If table.Columns.Count < 0 OrElse table.Rows.Count < 0 Then
            Exit Sub
        End If
    
        Using sw As IO.StreamWriter = New IO.StreamWriter(path)
            If header Then
                For i As Integer = 0 To table.Columns.Count - 2
                    sw.Write(table.Columns(i).ColumnName & delimiter)
                Next
                sw.Write(table.Columns(table.Columns.Count - 1).ColumnName & Environment.NewLine)
            End If
    
            For row As Integer = 0 To table.Rows.Count - 2
                For col As Integer = 0 To table.Columns.Count - 2
                    sw.Write(table.Rows(row).Item(col).ToString & delimiter)
                Next
                sw.Write(table.Rows(row).Item(table.Columns.Count - 1).ToString & Environment.NewLine)
            Next
    
            For col As Integer = 0 To table.Columns.Count - 2
                sw.Write(table.Rows(table.Rows.Count - 1).Item(col).ToString & delimiter)
            Next
            sw.Write(table.Rows(table.Rows.Count - 1).Item(table.Columns.Count - 1).ToString)
        End Using
    
    End Sub
    I would advice against storing data into a textfile, but if you must here is a way to convert it to a datatable and back.
    Last edited by dday9; Nov 6th, 2014 at 01:07 PM. Reason: Updated Code
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width