read csv file in vb.net

read csv file in vb.net

.Net has a built in CSV reader in the TextFieldParser. It will handle things like extra commas or quoted delimiters for you. For example, you could do this:

Dim dialcode As String
Dim chargecode As String
Dim mincharge As String

Dim tfp As New TextFieldParser(Z:temptest.csv)
tfp.Delimiters = New String() {,}
tfp.TextFieldType = FieldType.Delimited


tfp.ReadLine()  skip header
While tfp.EndOfData = False
    Dim fields = tfp.ReadFields()
    dialcode = fields(0)
    chargecode = fields(1)
    mincharge = fields(2)
    Console.WriteLine(String.Format({0} - {1} - {2}, dialcode, chargecode, mincharge))
End While
Private Sub ParseCSV(ByVal path As String)
    Dim inidex As Integer
    Dim oxygenSaturation As Object
    Dim pulse As Object

    Using MyReader As New Microsoft.VisualBasic.FileIO.
    TextFieldParser(path)

        MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        MyReader.Delimiters = New String() {,}

        MyReader.ReadLine()  skip header
        Loop through all of the fields in the file. 
        If any lines are corrupt, report an error and continue parsing. 
        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()
                Dim fields = MyReader.ReadFields()

                inidex = fields(0)
                oxygenSaturation = fields(28)
                pulse = fields(30)

                Dim a As String
                a = Index =  & inidex & oxygenSaturation =  & oxygenSaturation & pulse =  & pulse
                MessageBox.Show(a, Has been Changed, MessageBoxButtons.OKCancel)

                 Include code here to handle the row.
            Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox(Line  & ex.Message &
                 is invalid.  Skipping)
            End Try
        End While
    End Using
End Sub

read csv file in vb.net

Leave a Reply

Your email address will not be published.