Suppose I have .csv (Comma Separated values) file and I need to read certain columns of this file and plot them (related post: using .NET Chart Control). So, here is the code to read .csv files:
Imports System.IO.FileInfo
Imports System.IO
Dim x As Double, y1 As Double, y2 As Double
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(fn) 'fn =.csv File name
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
Dim currentRow As String()
Dim i As Integer
Dim j As Integer
j = 0
x = 0.0
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
Dim currentField As String
If j <> 0 Then ‘Skipping Header Row
i = 0
For Each currentField In currentRow
If i = 0 Then ‘First column
x = currentField
ElseIf i = 4 Then ‘Fifth column
y1 = currentField
ElseIf i = 5 Then
y2 = currentField
End If
i = i + 1
Next
‘Do the operation on retrieved values. E.g. plotting them using .Chart Control (see relevant post)
End If
j = j + 1
Catch ex As Microsoft.VisualBasic.
FileIO.MalformedLineException
MsgBox("Line " & ex.Message &
"is not valid and will be skipped.")
End Try
End While
End Using
0 Comments