使用 VBA 使用 UTF-8 将 Excel (XLS) 转换为 CSV

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40903525/
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-12 11:38:03  来源:igfitidea点击:

Convert Excel (XLS) to CSV with UTF-8 using VBA

excelvbaexcel-vbacsvutf-8

提问by user3392291

I'm trying to convert excel to csv with UTF - 8 in a Macro. But while converting, the contents are changing to unwanted symbols and text.

我正在尝试使用 UTF - 8 在宏中将 excel 转换为 csv。但是在转换时,内容正在更改为不需要的符号和文本。

Please help me in resolving this.

请帮我解决这个问题。

Public Sub convert_UnicodeToUTF8()

   Dim parF1, parF2 As String

   parF1 = "D:\test.xlsx"

   parF2 = "D:\test.csv"

    Const adSaveCreateOverWrite = 2
    Const adTypeText = 2

    Dim streamSrc, streamDst ' Source / Destination
    Set streamSrc = CreateObject("ADODB.Stream")
    Set streamDst = CreateObject("ADODB.Stream")
    streamDst.Type = adTypeText
    streamDst.Charset = "utf-8"
    streamDst.Open

    With streamSrc
        .Type = adTypeText
        .Charset = "Unicode" ' this is also the default value
        .Open
        .LoadFromFile parF1
        .copyTo streamDst
        .Close
    End With
    streamDst.saveToFile parF2, adSaveCreateOverWrite
    streamDst.Close
    Set streamSrc = Nothing
    Set streamDst = Nothing

End Sub

Thanks.

谢谢。

回答by Sin'dorC

If this is not working?

如果这不起作用?

ActiveWorkbook.WebOptions.Encoding = msoEncodingUTF8
ActiveWorkbook.SaveAs Filename:="C:\Book1.csv", FileFormat:=xlCSV

Try this

尝试这个

ActiveWorkbook.SaveAs Filename:="C:\Book1.csv", FileFormat:=xlCSVUTF8