【问题标题】:Convert Excel (XLS) to CSV with UTF-8 using VBA使用 VBA 将 Excel (XLS) 转换为带有 UTF-8 的 CSV
【发布时间】:2016-12-01 05:55:46
【问题描述】:

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

请帮我解决这个问题。

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

谢谢。

【问题讨论】:

  • 试试这个:ActiveWorkbook.WebOptions.Encoding = msoEncodingUTF8 和 ActiveWorkbook.SaveAs Filename:="C:\Book1.csv", FileFormat:=xlCSV
  • 完美!这对我有用。非常感谢:)

标签: vba excel csv utf-8


【解决方案1】:

如果这不起作用?

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

试试这个

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

【讨论】:

  • 只有 xlCSVUTF8 似乎可以工作,但似乎在较旧的 excel 版本(2016 年之前)中不受支持,这怎么可能? UTF-8 存在已久
猜你喜欢
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2019-02-10
  • 2015-07-10
  • 1970-01-01
  • 2011-12-07
相关资源
最近更新 更多