急!!!看了几天了,如何把文本文件(以TAB符分隔)读到数组中?谢谢!

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

...全文
216 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wumylove1234 2005-01-06
  • 打赏
  • 举报
回复
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,
cz_liu_xj 2005-01-05
  • 打赏
  • 举报
回复
你这个比较简单,参考我写的一段代码,可能你就有体会.

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 Sub

Private 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


tztz520 2005-01-05
  • 打赏
  • 举报
回复
用split

dim temparr() as string
dim temptab() as string

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


7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧