简体   繁体   中英

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM