excel - 如何解析非常大的 Excel 文件 (>6 GB) 并在 VBScript 中解析?
问题描述
我有一个非常大的 Excel 文件(>6 GB),我需要在 VBScript 中解析它。
Function LineCount(sFName As String) As Long
Const Bloc = 32 * 1024& '-- 32K bloc
Dim n As Long
Dim sText As String
Dim LfCount As Long
Dim t As Single: t = Timer '-- simple timing
Open sFName For Input As #1
If LOF(1) = 0 Then Close #1: Exit Function
n = LOF(1) Mod Bloc
If n = 0 Then n = Bloc
LineCount = 1
sText = Input(n, #1)
Do
'-- short code: --------------------------------
'LineCount = LineCount + UBound(Split(sText, vbLf))
'-----------------------------------------------
'-- longer code: ~10% faster -------------------
n = -1
Do
n = InStrB(n + 2, sText, vbLf)
If n Then LineCount = LineCount + 1 Else Exit Do
Loop
'-----------------------------------------------
If EOF(1) Then Exit Do
sText = Input(Bloc, #1)
Loop
Close #1
'-- subtract blank line at the bottom of the file
If Right(sText, 1) = vbLf Then LineCount = LineCount - 1
Debug.Print LineCount, Timer - t
End Function
我需要能够读取非常大的 Excel 文件并从中创建 1,000,000 行 Excel 文件。有什么建议可以快速阅读而不会遇到没有内存的运行时错误吗?
解决方案
您将打开大文件进行输入,一次读取一行,然后打开一系列其他文件进行输出,在打开下一个之前向每个文件写入 1M 行,依此类推。
缩小版:
Sub SplitTextFile()
Dim fso As Object, t, n As Long, ln, t2 As Object, outNum As Long
Set fso = CreateObject("scripting.filesystemobject")
'create a dummy text file for testing
Set t = fso.createtextfile("C:\Temp\dummy.txt")
For n = 1 To 1000
t.writeline "This is line " & n
Next n
t.Close
Set t = fso.opentextfile("C:\Temp\dummy.txt")
n = 0
outNum = 0
Do While Not t.atendofstream
If n Mod 100 = 0 Then
If Not t2 Is Nothing Then t2.Close
outNum = outNum + 1
Debug.Print "Writing file # " & outNum
Set t2 = fso.createtextfile("C:\Temp\dummy_" & outNum & ".txt", 2)
End If
t2.writeline t.readline
n = n + 1
Loop
t.Close
t2.Close
End Sub
推荐阅读
- javascript - 是否可以将 javascript 对象的 get 项定义为函数?
- java - onClick int 值更新到 Firebase 实时数据库
- react-native - React Native:成功登录后将电子邮件数组存储在 AsyncStorage 中
- node.js - 如何使用 Cloud Function 从 Cloud Storage for Firebase 获取对象?
- node.js - 直到我在 Visual Studio Code 中点击开始调试,一切都很好
- jenkins-pipeline - Docker in Docker - Jenkins Docker Pipeline - 如何连接到外部服务
- python - opencv 在使用 Kivy 和 Buildozer 创建 Android 应用程序时出现问题:/site-packages/cv2/cv2.so" 是 64 位而不是 32 位,targetsdk:27
- email - 如何在 Roundcube 上启用密码插件
- c# - 传递给 ViewDataDictionary 的模型项是“*”类型的,但是这个 ViewDataDictionary 实例需要一个“..IEnumerable[*]”类型的模型项
- google-cloud-platform - Google APIs Service Agent 服务帐号被误删