dcsimg
Results 1 to 2 of 2

Thread: Read CSV Files in Vb.net

  1. #1

    Thread Starter
    Addicted Member ryanframes's Avatar
    Join Date
    Apr 2012
    Posts
    208

    Read CSV Files in Vb.net

    Hello , i'm sorry if this is a silly question.
    But i'm new to vb.net and it's my first time working with CSV so i don't know what to do.
    i have csv files like this :
    Code:
    Hello,"First Timer, welcome ",to,vb.net
    i want to split it into :
    Code:
    arr(0) = "Hello"
    arr(1) = "First Timer, welcome"
    arr(2) = "to"
    arr(3) = "vb.net"
    i searched on google and found that i can use TextFieldParser to achieve what i want to do, but it still returns :
    Code:
    Hello,"First Timer, welcome ",to,vb.net
    here's my code :
    Code:
    Using parser As New FileIO.TextFieldParser(txtPath.Text)
                Dim j As Integer = 0, i As Integer = 0
                Dim curRec As String()
                parser.TextFieldType = FileIO.FieldType.Delimited
                parser.Delimiters = New String() {","}
                parser.HasFieldsEnclosedInQuotes = True
                File.Delete("c:\test.txt")
                While Not parser.EndOfData
                    Try
                        curRec = parser.ReadFields
                        If Not curRec Is Nothing Then
                            For i = 0 To curRec.GetUpperBound(0)
                                My.Computer.FileSystem.WriteAllText("c:\test.txt", curRec(i) & vbCrLf, True)
                            Next
                        End If
                        j = j + 1
                    Catch ex As Exception
                        MsgBox(ex.Message, MsgBoxStyle.Critical)
                        Exit While
                    End Try
                End While
            End Using
    Sorry for bad english.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,642

    Re: Read CSV Files in Vb.net

    You need to learn how to debug. If you'd done that then you'd see that 'curRec' contains exactly what you said you wanted. If you want to then write those array elements to separate lines in a file then just call File.WriteAllLines and pass the array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width