vba 使用UTF-8编码的VBA Excel宏写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11227632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:24:24  来源:igfitidea点击:

VBA Excel Macro write file using UTF-8 encoding

excelvbaunicodeutf-8

提问by jasonhughes

I'm creating a macro in excel that processes a spreadsheet and writes the content (text) to a file. I need this file to be encoded as UTF-8. I've tried opening the file as unicode using OpenTextFile(... TristateTrue) and StrConv(.. vbUnicode) but those only convert it to UTF-16. I've searched everywhere online and can't find anything. Is this even possible?

我正在 excel 中创建一个宏来处理电子表格并将内容(文本)写入文件。我需要将此文件编码为 UTF-8。我试过使用 OpenTextFile(... TristateTrue) 和 StrConv(.. vbUnicode) 以 unicode 格式打开文件,但那些只将其转换为 UTF-16。我在网上到处搜索,找不到任何东西。这甚至可能吗?

thanks

谢谢

回答by Jose Ortega

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

Reference: Save text file UTF-8 encoded with VBA

参考: 保存用 VBA 编码的 UTF-8 文本文件