下面是我的代码,这不是完美但有效,循环文本文件需要30多分钟。如何通过使用其他代码或方法来提高循环文件的速度。请帮忙。
Open "C:\Users\steven.EOPESTATE\Desktop\Sharp Sales\TRMSAVE01.txt" For Input As #1
Do Until EOF(1)
Dim ITEMSQL As String
Line Input #1, varLine
testvarline = Split(varLine, ",")
If testvarline(0) = "$ITEM" Then
'Debug.Print testvarline(0), testvarline(1), testvarline(2), testvarline(3), testvarline(4), testvarline(5), testvarline(6), testvarline(7), testvarline(8), testvarline(9)
testvarline(0) = Replace(testvarline(0), "$", " ")
testvarline(7) = Replace(testvarline(7), ",,", " ")
ITEMSQL = "Insert into SalesItem([ITEMID], [2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) Values (" & Chr(34) & "" & (testvarline(0)) & "" & Chr(34) & "," & (testvarline(1)) & "," & (testvarline(2)) & "," & (testvarline(3)) & "," & (testvarline(4)) & "," & (testvarline(5)) & "," & Chr(34) & "" & (testvarline(6)) & "" & Chr(34) & "," & (testvarline(9)) & "," & (testvarline(10)) & "," & (testvarline(11)) & "," & (testvarline(12)) & "," & (testvarline(14)) & ")"
Debug.Print ITEMSQL
DoCmd.RunSQL ITEMSQL
DoCmd.SetWarnings False
DoCmd.Echo False
End If
Loop
Close #1
答案 0 :(得分:1)
你可以在电脑上做的最慢的事情之一是从硬盘加载。 CPU和RAM比硬盘驱动器快得多(见this SO question),因此它们最终等待硬盘读取完成。因此,一次将整个文件读入内存将大大加快您的程序。
我不建议再次使用Split函数拆分行进行处理,我建议使用Mid函数自己解析它们,这实际上避免了正常字符串处理的大量开销,因为它不会创建临时副本内存中的子字符串。这是UNTESTED示例代码:
Dim strFileText As String
Dim lngCurrIndex As Long
Dim lngEndOfLine As Long
Open "C:\Users\steven.EOPESTATE\Desktop\Sharp Sales\TRMSAVE01.txt" For Binary As #1
strFileText = Space(LOF(1)) 'create space for the whole file'
Get 1, , strFileText 'read in the whole file at once'
lngCurrIndex = 1
Do While lngCurrIndex < Len(strFileText)
Dim ITEMSQL As String
lngEndOfLine = InStr(lngCurrIndex, strFileText, vbCrLf, vbBinaryCompare) 'find the end of this line. NOTE: This assumes that this text files uses the MS convention of CrLf line endings'
varLine = Mid(strFileText, lngCurrIndex, lngEndOfLine - lngCurrIndex) 'get the line'
lngCurrIndex = lngEndOfLine + 2 'set lngCurrIndex to the start of the next line'
testvarline = Split(varLine, ",")
If testvarline(0) = "$ITEM" Then
'Debug.Print testvarline(0), testvarline(1), testvarline(2), testvarline(3), testvarline(4), testvarline(5), testvarline(6), testvarline(7), testvarline(8), testvarline(9)'
testvarline(0) = Replace(testvarline(0), "$", " ")
testvarline(7) = Replace(testvarline(7), ",,", " ")
ITEMSQL = "Insert into SalesItem([ITEMID], [2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) Values (" & Chr(34) & "" & (testvarline(0)) & "" & Chr(34) & "," & (testvarline(1)) & "," & (testvarline(2)) & "," & (testvarline(3)) & "," & (testvarline(4)) & "," & (testvarline(5)) & "," & Chr(34) & "" & (testvarline(6)) & "" & Chr(34) & "," & (testvarline(9)) & "," & (testvarline(10)) & "," & (testvarline(11)) & "," & (testvarline(12)) & "," & (testvarline(14)) & ")"
Debug.Print ITEMSQL
DoCmd.RunSQL ITEMSQL
DoCmd.SetWarnings False
DoCmd.Echo False
End If
Loop
Close #1
根据文件的大小,可能无法一次将其全部加载到内存中。如果是这种情况,您可以将其拆分为大块并一次执行一个。