0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

自分用VBA

Posted at

Sub ReadLargeUTF8File()
Dim filePath As String
Dim stream As Object
Dim chunkSize As Long
Dim buffer As String
Dim tempStr As String
Dim pos As Integer
Dim line As String

filePath = "C:\path\to\your\file.txt" ' 替换成你的文件路径
chunkSize = 10240 ' 每次读取 10 KB(可以调整)

' 使用 ADODB.Stream 处理 UTF-8 文件
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2 ' 设定为文本模式
stream.Mode = 3 ' 设定为读写模式
stream.Charset = "UTF-8" ' 设定字符集为 UTF-8
stream.Open
stream.LoadFromFile filePath

Do Until stream.EOS
    buffer = stream.ReadText(chunkSize) ' 读取 10 KB 数据
    tempStr = tempStr & buffer ' 累积数据

    ' 处理完整的行
    Do
        pos = InStr(tempStr, Chr(10)) ' 查找 LF 换行符
        If pos > 0 Then
            line = Left(tempStr, pos - 1) ' 取出一行
            tempStr = Mid(tempStr, pos + 1) ' 剩余部分
            Debug.Print line ' 处理该行(这里可以换成写入文件或其他操作)
        Else
            Exit Do
        End If
    Loop
Loop

' 处理最后剩余的内容(如果没有 LF 结尾)
If Len(tempStr) > 0 Then Debug.Print tempStr

' 关闭 ADODB.Stream
stream.Close
Set stream = Nothing

End Sub

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?