Сначала я взял код из документов MS https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser?view=netcore-3.1 Мне нужно было знать, сколько столбцов мне нужно в таблице данных.
Private Sub OpCode1()
Dim maxColumnCount As Integer
Using MyReader As New TextFieldParser("C:\Users\xxx\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
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.
If currentRow.Count > maxColumnCount Then
maxColumnCount = currentRow.Count
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
MessageBox.Show(maxColumnCount.ToString)
End Sub
Как только у меня будет необходимое количество столбцов , Я создал DataTable
и добавил необходимое количество столбцов. Затем, когда пример инструктировал вас обрабатывать строку, я добавил строку в DataTable
. Наконец, я отобразил DataTable
в DataGridView
.
Private Sub OPCode()
Dim dt As New DataTable
For i = 1 To 38
dt.Columns.Add(i.ToString)
Next
Using MyReader As New TextFieldParser("C:\Users\xxx\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
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.
dt.Rows.Add(currentRow)
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
DataGridView1.DataSource = dt
End Sub
Подробнее c для ваших данных ...
Private Sub OpCode()
Dim dt As New DataTable
dt.Columns.Add("Date", GetType(Date))
dt.Columns.Add("Customer Name", GetType(String))
dt.Columns.Add("Address", GetType(String))
dt.Columns.Add("City", GetType(String))
dt.Columns.Add("State", GetType(String))
dt.Columns.Add("Zipcode", GetType(String))
dt.Columns.Add("RefID", GetType(String))
dt.Columns.Add("Amount", GetType(Decimal))
Dim DataDate As Date
Dim RefID As String = ""
Dim Amount As Decimal
Using MyReader As New TextFieldParser("C:\Users\maryo\test.txt")
MyReader.TextFieldType = FieldType.Delimited
MyReader.Delimiters = {"~"}
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.
If currentRow(0) = "D" Then
DataDate = CDate(currentRow(3))
RefID = currentRow(17)
Amount = CDec(currentRow(20))
End If
If currentRow(0) = "O" Then
dt.Rows.Add({DataDate, currentRow(1), currentRow(2), currentRow(6), currentRow(7), currentRow(8), RefID, Amount})
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
DataGridView1.DataSource = dt
End Sub
Я предположил, что применяется строка D в следующую строку O. Я сохранил данные из строки D в трех переменных и использовал их при чтении следующей строки.
Помните, что коллекции (включая массивы) равны нулю на основе. net.