我要导入的文本文件里面的数据格式是(每个文件大概要几百条这样的数据):
10003`0157`JF7013528042`2102112321N078000305``2102120291N078000361`
10003`0157`JF7013528042`2102112321N078000305``2102100053N078000145`
10003`0157`JF7013528042`2102112321N078000305``2102350768N078000540`
10003`0157`JF7013528042`2102112321N078000305``21210109956T78000226`
10003`0157`JF7013528041`2102300535N078000568``028892R078000306`
10003`0157`JF7013528041`2102350768N078000283``2102120289N078000495`
10003`0157`JF7013528041`2102350768N078000283``2102300535N078000568`
10003`0157`JF7013528041`2102350768N078000283``2102120289N078000496`
10003`0157`JF7013528041`2102350768N078000283``0355651078000724`
下面代码就是实现文本文件导入到grid,但在测试时总是死机,无法通过,大概几分钟后跳到err处:
Private Sub Command1_Click()
On Error GoTo err
Text1.Text = ""
Dim a() As Integer
Dim i As Integer
Dim j As Integer
Dim n As String
Dim line As String
With CommonDialog1
.Filter = "全部文件(*.*)|*.*|文本文件(*.txt)|*.txt"
.FilterIndex = 2
.CancelError = True
.InitDir = App.Path
.ShowOpen
End With
If CommonDialog1.FileName <> "" Then
Open CommonDialog1.FileName For Input As #1
Do While Not EOF(1)
'Line Input #1, line
Line Input #1, n
Text1.Text = Text1.Text & n & vbCrLf
Loop
Close #1
End If
Dim lineCount As Integer
lineCount = SendMessage(Text1.hwnd, _
EM_GETLINECOUNT, 0&, 0&)
'MsgBox lineCount - 1
Dim x() As String
x = Split(Text1.Text, vbCrLf, -1)
Dim y() As String
y = Split(x(0), "'", -1)
ReDim a(lineCount - 1, UBound(y) + 1) As Integer
Open CommonDialog1.FileName For Input As #1
For i = 1 To lineCount - 1
For j = 1 To UBound(y) + 1
Input #1, a(i, j)
Next
Next
Close #1
MSFlexGrid1.Rows = lineCount - 1
MSFlexGrid1.Cols = UBound(y) + 1
For i = 0 To lineCount - 2
MSFlexGrid1.Row = i
For j = 0 To UBound(y)
MSFlexGrid1.Col = j
MSFlexGrid1.Text = a(i + 1, j + 1)
Next
Next
Exit Sub
err:
MsgBox "错误终止了,请重新选择"
Exit Sub
End Sub
我感觉可能是我定义的数据类型有问题,请大家指教一下!~~