Mac Excel使用VBA生成UTF-8 [英] Mac Excel generate UTF-8 using VBA

查看:433
本文介绍了Mac Excel使用VBA生成UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to generate a UTF-8 file using Visual Basic (VBA) in Excel 2016 for Mac? I need to generate an XML or TXT file that is encoded.

谢谢

推荐答案

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

Not UTF-8, but UTF-16 is what VBA uses internally, so you can dump string data directly:

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字符串文字,只需使用简单的重音拉丁字母,如上述á,但从中分配的字符串单元格将以Unicode格式保存。

This should work in Excel Mac 2011 and 2016, and in Windows versions. Note that you won't be able to type Unicode string literals in the editor, just simple accented Latin letters like the "á" above, but strings assigned from the Value of a cell will be preserved in Unicode.

这篇关于Mac Excel使用VBA生成UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆