看了几天了,如何把文本文件(以TAB符分隔)读到数组中?谢谢!
下例:文本格式:
1    A1    B1 ...
2    A2    B2 ...
3    A3    B3 ...
4    A4    B4 ...
.
.
先读到数组中,再保存到数据库里.
谢谢!

解决方案 »

  1.   

    用splitdim temparr() as string
    dim temptab() as stringtemparr=split("文件内容",vbcrlf)'这样就分隔开每一行了.
    temptab=split(temparr(N),vbtab)'temparr(N)就是第N行的数据了,你可以用一个循环将所有行分隔
      

  2.   

    你这个比较简单,参考我写的一段代码,可能你就有体会.Sub 提取数据(strSourceFile As String, strTargetFile As String)
        Dim filenum         As Integer
        Dim fileContents    As String
        Dim fileInfo     As Variant
        Dim i               As Integer
        Dim j               As Integer
        Dim temp(1000) As String
        Dim tmpDemData As String
        filenum = FreeFile
        Open strSourceFile For Binary As #filenum
            fileContents = Space(LOF(filenum))
            Get #filenum, , fileContents
        Close filenum
        fileInfo = Split(fileContents, vbCrLf, -1, 1)
        '取出源文件行数,按照回车换行来分隔成数组,也就是为2行数
        filenum = FreeFile
        tmpDemData = ""
        If Dir(strTargetFile, vbNormal) <> "" Then
            Kill strTargetFile
        End If
        Dim Filestr As Variant
        Open strTargetFile For Append As #filenum
            '循环每一行
            k = 1
            For i = 0 To UBound(fileInfo) - 1
              Filestr = Split(Trim(fileInfo(i)), " ", -1, 1) '按照逗号或者空格分隔每一行的数据,为1,2,3,4,5
                 tmpDemData = ""
                 
                 
                 For j = 0 To UBound(Filestr)
                  '判断数据是否正确
                  If Filestr(j) <> "" Then
                  temp(k) = Filestr(j)
                  Debug.Print "temp(" & k & "):" & temp(k)
                  k = k + 1
                  End If
                tmpDemData = tmpDemData & temp(k)
                If j <> UBound(Filestr) Then
                tmpDemData = tmpDemData & ","
                End If            
                Next
                '保存一行如目标文件
                Print #filenum, tmpDemData
             Next
             MsgBox k
        Close #filenum
         Dim str
     str = App.Path
     If Right(str, 1) <> "\" Then
     str = str + "\"
     End If
        MsgBox "文件已写入" + "" & str & "\bb.txt"
    End SubPrivate Sub Command1_Click()
     Dim str
     str = App.Path
     If Right(str, 1) <> "\" Then
     str = str + "\"
     End If
    提取数据 "" & str & "\BOOKCC.txt", "" & str & "\bb.txt"
    End Sub
      

  3.   

    Option Explicit
    Public strError As String
    Dim myDataBase As New clsDataBase
    Dim myOperation As New clsOperation
    Public Function ImportTextFile(FileFullPath As String, strSql As String, _
                                   Optional FieldDelimiter As String = ",", _
                                   Optional RecordDelimiter As String = vbCrLf) As Boolean
        On Error GoTo errHandle:
        Dim rs As New ADODB.Recordset
        Dim sFileContents As String
        Dim iFileNum As Integer
        Dim sTableSplit() As String
        Dim sRecordSplit() As String
        Dim lCtr As Integer
        Dim iCtr As Integer
        Dim iFieldCtr As Integer
        Dim lRecordCount As Long
        Dim iFieldsToImport As Integer
        'These variables prevent
        'having to requery a recordset
        'for each record
        Dim asFieldNames() As String
        Dim abFieldIsString() As Boolean
        Dim iFieldCount As Integer
        Dim sSQL As String
        Dim bQuote As Boolean
        If Dir(FileFullPath) = "" Then
            strError = "临时存储文件:" & FileFullPath & "没有找到!"
            Exit Function
        End If
        Call myDataBase.ExecuteRst(strSql, rs)
        iFieldCount = rs.Fields.Count    iFileNum = FreeFile
        Open FileFullPath For Input As #iFileNum
        sFileContents = Input(LOF(iFileNum), #iFileNum)
        Close #iFileNum
        'split file contents into rows
        sTableSplit = Split(sFileContents, RecordDelimiter)
        lRecordCount = UBound(sTableSplit)
        'make it "all or nothing: whole text
        'file or none of it
        myDataBase.BeginTransaction
        For lCtr = 0 To lRecordCount - 1
            'split record into field values
            sRecordSplit = Split(sTableSplit(lCtr), FieldDelimiter)
            iFieldsToImport = IIf(UBound(sRecordSplit) + 1 < _
                              iFieldCount, UBound(sRecordSplit) + 1, iFieldCount)
            rs.AddNew
            rs.Fields("ModalLength") = myOperation.GetModalLength       '型号长度
            For iCtr = 0 To iFieldsToImport - 1
                If LCase(rs.Fields(iCtr).Name) = LCase("goodsbar") Then
                    rs.Fields("modal_id") = Left(Trim(CStr(sRecordSplit(iCtr))), myOperation.GetModalLength)
                End If
                If LCase(rs.Fields(iCtr).Name) <> "modallength" Then    '长度已经增加完毕
                    rs.Fields(iCtr).Value = IIf(Trim(CStr(sRecordSplit(iCtr))) = "", Null, Trim(CStr(sRecordSplit(iCtr))))
                End If
            Next iCtr
            rs.Update
        Next lCtr
        myDataBase.CommitTransaction
        Close #iFileNum
        Set rs = Nothing
        ImportTextFile = True
        Exit Function
    errHandle:
    '    On Error Resume Next
        myDataBase.RollBackTransaction
        If iFileNum > 0 Then Close #iFileNum
        Set rs = Nothing
        strError = Err.Description
    End Function
    参考一下,你将我以","分隔的,改成以VBtab分隔就可以了.
    Optional FieldDelimiter As String = VBTAB,