如何将UTF-8编码的字符串写入vba的文本文件,如
Dim fnum As Integer
fnum = FreeFile
Open "myfile.txt" For Output As fnum
Print #fnum, "special characters: äöüß" 'latin-1 or something by default
Close fnum
应用程序级别是否有一些设置?
答案 0 :(得分:70)
我在web上找到答案:
Dim fsT As Object
Set fsT = CreateObject("ADODB.Stream")
fsT.Type = 2 'Specify stream type - we want To save text/string data.
fsT.Charset = "utf-8" 'Specify charset For the source text data.
fsT.Open 'Open the stream And write binary data To the object
fsT.WriteText "special characters: äöüß"
fsT.SaveToFile sFileName, 2 'Save binary data To disk
当然不像我预期的那样......
答案 1 :(得分:17)
您可以使用CreateTextFile或OpenTextFile方法,两者都具有对编码设置有用的属性“unicode”。
object.CreateTextFile(filename[, overwrite[, unicode]])
object.OpenTextFile(filename[, iomode[, create[, format]]])
示例:覆盖:
CreateTextFile:
fileName = "filename"
Set fso = CreateObject("Scripting.FileSystemObject")
Set out = fso.CreateTextFile(fileName, True, True)
out.WriteLine ("Hello world!")
...
out.close
示例:附加:
OpenTextFile Set fso = CreateObject("Scripting.FileSystemObject")
Set out = fso.OpenTextFile("filename", ForAppending, True, 1)
out.Write "Hello world!"
...
out.Close
详情请见MSDN docs
答案 2 :(得分:7)
这会在文件的开头写一个字节顺序标记,这在UTF-8文件中是不必要的,而某些应用程序(在我的情况下,SAP)不喜欢它。 解决方案:Can I export excel data with UTF-8 without BOM?
答案 3 :(得分:6)
这是另一种方法 - 使用API函数WideCharToMultiByte:
Option Explicit
Private Declare Function WideCharToMultiByte Lib "kernel32.dll" ( _
ByVal CodePage As Long, _
ByVal dwFlags As Long, _
ByVal lpWideCharStr As Long, _
ByVal cchWideChar As Long, _
ByVal lpMultiByteStr As Long, _
ByVal cbMultiByte As Long, _
ByVal lpDefaultChar As Long, _
ByVal lpUsedDefaultChar As Long) As Long
Private Sub getUtf8(ByRef s As String, ByRef b() As Byte)
Const CP_UTF8 As Long = 65001
Dim len_s As Long
Dim ptr_s As Long
Dim size As Long
Erase b
len_s = Len(s)
If len_s = 0 Then _
Err.Raise 30030, , "Len(WideChars) = 0"
ptr_s = StrPtr(s)
size = WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, 0, 0, 0, 0)
If size = 0 Then _
Err.Raise 30030, , "WideCharToMultiByte() = 0"
ReDim b(0 To size - 1)
If WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, VarPtr(b(0)), size, 0, 0) = 0 Then _
Err.Raise 30030, , "WideCharToMultiByte(" & Format$(size) & ") = 0"
End Sub
Public Sub writeUtf()
Dim file As Integer
Dim s As String
Dim b() As Byte
s = "äöüßµ@€|~{}[]²³\ .." & _
" OMEGA" & ChrW$(937) & ", SIGMA" & ChrW$(931) & _
", alpha" & ChrW$(945) & ", beta" & ChrW$(946) & ", pi" & ChrW$(960) & vbCrLf
file = FreeFile
Open "C:\Temp\TestUtf8.txt" For Binary Access Write Lock Read Write As #file
getUtf8 s, b
Put #file, , b
Close #file
End Sub
答案 4 :(得分:2)
我调查了Máťa的答案,他的名字暗示着编码资格和经验。 VBA docs说CreateTextFile(filename, [overwrite [, unicode]])
创建一个文件“作为Unicode或ASCII文件。如果文件创建为Unicode文件,则值为True;如果创建为ASCII文件,则为False。如果省略,则为假设是ASCII文件。“一个文件存储unicode字符,但编码是什么?未编码的unicode无法在文件中表示。
OpenTextFile(filename[, iomode[, create[, format]]])
的{{3}}为格式提供了第三种选择:
Máťa为这个论点传递-1。
从VBA doc page判断(不是VBA,但我认为反映了基础Windows操作系统如何表示unicode字符串并回应MS Office的现实,我不知道)系统默认是使用1字节/ unicode的编码使用区域设置的ANSI代码页的字符。 UnicodeEncoding
是UTF-16。文档还描述了UTF-8也是一种“Unicode编码”,这对我来说很有意义。但我还不知道如何为VBA输出指定UTF-8,也不知道我用OpenTextFile(,,, 1)写入磁盘的数据是UTF-16编码的。 VB.NET documentation很有帮助。
答案 5 :(得分:1)
将字符串转换为UTF-8字符串的传统方法如下:
StrConv("hello world",vbFromUnicode)
所以简单地说:
Dim fnum As Integer
fnum = FreeFile
Open "myfile.txt" For Output As fnum
Print #fnum, StrConv("special characters: äöüß", vbFromUnicode)
Close fnum
不需要特殊的COM对象