我的文本格式是(如下):张三
北京AAA
111
1111
李四
河北FFF
222
2222
王五
天津RH
333
3333
......
我通过如下代码执行合并:
Dim dlg As new OpenFileDialog
dlg.Filter = "文本文件|*.txt"
If dlg.ShowDialog = DialogResult.OK Then
Using sr As IO.StreamReader = New IO.StreamReader(dlg.FileName) '直接从文件路径生成'
Dim line As String = sr.ReadLine() '读取一行
Dim rs1() As String = line.Split(vbcr)
For i As Integer = 0 To rs1.Length - 1
Dim cs() As String = rs1(i).Split(vbtab)
If cs.Length = 4 Then
Dim dr As DataRow = DataTables("表A").Addnew()
dr("第一列") = cs(0)
dr("第二列") = cs(1)
dr("第三列") = cs(2)
dr("第四列") = cs(3)
End If
Next
Do While line IsNot Nothing '如果不为空.为空说明读取完毕,结束循环
line = sr.ReadLine() '读取下一行
Dim rs() As String = line.Split(vbcr)
For i As Integer = 0 To rs.Length - 1
Dim cs() As String = rs(i).Split(vbtab)
If cs.Length = 4 Then
Dim dr As DataRow = DataTables("表A").Addnew()
dr("第一列") = cs(0)
dr("第二列") = cs(1)
dr("第三列") = cs(2)
dr("第四列") = cs(3)
End If
Next
Loop
End Using
End If
能完成合并,但是出现乱码:
此主题相关图片如下:乱码.png
而且代码 执行完毕后显示错误:
此主题相关图片如下:错误提示.png
求助上面代码如何修改?