test.txt文件
a1,a2,a3,a4,
b1,b2,b3,b4,
c1,c2,c3,c4,
代码:
Private Sub Command1_Click()
Dim strPath As String '调试文件路径
Dim FileNum As Integer
Dim strData(3, 4) As String
Dim str1 As String
Dim i As Integer
'On Error Resume Next
strPath = App.Path & "\test.txt"
'实例化一个文件类型
FileNum = FreeFile()
'打开文件
Open strPath For Input As FileNum
For j = 1 To 3
'取得一行的字符
Line Input #FileNum, newline
'将文本中的一行赋给str1
str1 = newline
i = 0
'取得第一行的值并赋给数组
Do While str1 <> ""
i = i + 1
strData(j, i) = Mid(str1, 1, InStr(str1, ",") - 1)
str1 = Mid(str1, InStr(str1, ",") + 1)
Loop
Next j
'设置控件的行列数
MSHFlexGrid1.Rows = 4
MSHFlexGrid1.Cols = 5
'添加数据
For i = 1 To 3
For j = 1 To 4
MSHFlexGrid1.Row = i
MSHFlexGrid1.Col = j
MSHFlexGrid1.Text = strData(i, j)
Next j
Next i
End Sub
Private Sub Command1_Click()
Me.MSHFlexGrid1.Cols = 4
Dim LineStr As String
Dim a() As String
Dim i As Integer
Dim x As Integer
Open "D:\11.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, LineStr
a() = Split(LineStr, ",")
Me.MSHFlexGrid1.AddItem ""
For i = 0 To UBound(a)
Me.MSHFlexGrid1.TextMatrix(x + 1, i) = a(i)
Next
x = x + 1
Loop
Close #1