首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mac Excel生成UTF-8

Mac Excel生成UTF-8
EN

Stack Overflow用户
提问于 2017-01-31 06:07:49
回答 2查看 2K关注 0票数 2

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

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-04-08 05:41:10

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

代码语言:javascript
复制
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字符串文字,只能输入简单的重音拉丁字母,如上面的“a”,但从单元格的Value分配的字符串将保留在Unicode中。

票数 4
EN

Stack Overflow用户

发布于 2021-09-19 13:21:13

经过4年的研究;),这是我想出的最好的解决方案:

代码语言:javascript
复制
Sub test()
    Dim xmlStr As String
    xmlStr = "<h1 class=""bold"">UTF8 Test -ÄÖÜêçñù-</h1>"
    writeToFile xmlStr, "utf8.html"
End Sub


Function writeToFile(str As String, filename As String)
    'escape double quotes
    str = Replace(str, """", "\\\""")

    ' use Apple Script and shell commands to create and write file
    MacScript ("do shell script ""printf '" & str & "'> " & filename & " "" ")

    ' print file path
    Debug.Print "file path: " & MacScript("do shell script ""pwd""") & "/" & filename
End Function

我还没有找到一种打破Apple沙盒环境的方法。因此,我将所有文件写入Excel沙箱容器路径:

代码语言:javascript
复制
/Users/myuser/Library/Containers/com.microsoft.Excel/Data
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41946262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档