'读取通过制表符分隔的文本文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\1.txt")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {vbTab}
Dim currentRow As String()
'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()
' Include code here to handle the row.
' 在此处插入代码处理代码。
For Each Str As String In currentRow
MsgBox(Str)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("行 " & ex.Message & " 无效,跳过")
End Try
End While
End Using
End Sub
Public Function ReadCSV(ByVal filename As String, ByVal sep As String) As List(Of String())
Dim info As List(Of String()) = New List(Of String())
Using pars As New FileIO.TextFieldParser(filename)
pars.SetDelimiters(sep)
While Not pars.EndOfData
Dim line() As String
line = pars.ReadFields()
info.Add(line)
End While
End Using
Return info
End Function