Mac Excel使用VBA生成UTF-8

时间:2017-01-30 22:07:49

标签: excel vba excel-vba

是否可以在Excel 2016 for Mac中使用Visual Basic(VBA)生成UTF-8文件? 我需要生成一个编码的XML或TXT文件。

由于

1 个答案:

答案 0 :(得分:4)

不是UTF-8,但 UTF-16 是VBA在内部使用的,因此您可以直接转储字符串数据:

Dim fnum As Integer
fnum = FreeFile()
Open "utf16.txt" For Binary Access Write As fnum

'write UTF-16 Byte Order Marker
'
Put #fnum, 1, &HFE
Put #fnum, 2, &HFF

printLineUtf16 fnum, "Olá Mundo!"

Close #fnum

Function printLineUtf16(fnum As Integer, str As String)

    str = str & ChrW(10)            'append newline

    Dim data() As Byte              'coerce string to bytes
    data = str

    Put #fnum, LOF(fnum) + 1, data  'append bytes to file

End Function

这应该适用于Excel Mac 2011和2016以及Windows版本。请注意,您将无法在编辑器中键入Unicode字符串文字,只需使用简单的重音拉丁字母,如上面的“á”,但从单元格Value分配的字符串将以Unicode格式保留。