7,785
社区成员
 发帖
 发帖 与我相关
 与我相关 我的任务
 我的任务 分享
 分享
    Dim strLine    As String
    Dim strValue() As String
    Dim LineCount  As Integer  '行数
    LineCount = 0
    Open "D:\My Projects\Temp\test.txt" For Input As #1
        Do While Not EOF(1)
            LineCount = LineCount + 1
            Line Input #1, strLine
            If LineCount > 2 Then
                strValue = Split(strLine, vbTab, , vbTextCompare)
                '||……
                '||向数据库中增加一条记录,相应字段的值分别为
                '||strValue(0)、strValue(1)、strValue(2)、strValue(3)
                '||……
            End If
        Loop
    Close #1
Private Sub Command1_Click()
Dim b As Variant                '变体变量b 用与变为数组
Dim c As Variant                '变体变量c 用与变为数组
Open "c:\1.txt" For Input As #1 '打开文件
    Do While EOF(1) = False     '若没循环到文件尾则继续
        Line Input #1, a        '读取一行 用变量 a 存储
        For i = 1 To Len(a)     '这个循环作用是把中间多个空格变为1个空格
            a = Replace(a, "  ", " ") ' 替换函数替换的方法替换2个空格为1个 循环到最后只有1个空格再字串中间
        Next i
        s = s & a & vbCrLf      '把变换后的数据存在一个变量s 中
    Loop
Close #1                        ' 关闭文件
b = Split(s, vbCrLf)            ' 分割S 成每行 用数组b 存储
For i = 2 To UBound(b)          ' 从第3行开始执行循环
If Len(b(i)) > 5 Then           ' 这个判断是为了避免空行也进行操作
    c = Split(b(i), " ")        ' 分割每行 分割符为 空格
    For j = 0 To UBound(c)      ' 把数组C 的每个项目分别赋给text 组
        Text1(j) = Text1(j) & c(j) & vbCrLf ' 各组=个组+数据+换行
    Next j
End If
Next i
End Sub