我一直在使用ADODB.Stream
在我的VBA应用程序中读取和编写utf-8文件并取得了一些成功。唯一真正的问题是ADODB.Stream
与本机VBA文件读取相比相当慢。通常,我会使用库存VBA Open
,Input
,Print
,Close
等功能,这些功能足够快。我没有尝试过任何Windows API或DLL解决方案,因为正确或错误的趋势是禁用API调用,DLL等。
我使用它来编辑一组文档的Open Office XML文件,然后将它们合并在一起。
这是我对utf-8文件的读写函数。这些是从互联网上复制的。
Public Function readUtf8(fn As String) As String
Dim stream As ADODB.stream: Set stream = New ADODB.stream
stream.Charset = "utf-8"
stream.Open
On Error GoTo nofile
stream.LoadFromFile fn
readUtf8 = stream.ReadText() & ""
stream.Close
Set stream = Nothing
Exit Function
nofile:
stream.Close
Set stream = Nothing
readUtf8 = ""
End Function
反向写入函数或多或少是相同的。
Public Function writeUtf8(fn As String, txt As String) As Integer
Dim stream As ADODB.stream: Set stream = New ADODB.stream
stream.Charset = "utf-8"
stream.Open
stream.WriteText txt
stream.SaveToFile fn
stream.Close
Set stream = Nothing
writeUtf8 = 0
End Function
是否有更快的替代ADODB.Stream
?