使用“ADODB.Stream”将ANSI转换为UTF-8,在第一行中错过1-2个字符

时间:2014-08-11 12:54:15

标签: excel vba utf-8

我需要转换" ANSI" csv文件到" UTF-8" csv文件。 下面的代码可以工作,但第一个字符错过 请参阅随附的屏幕截图, 原始文件:客户 输出文件:﨏ustomer

Function Convert(myFileIn, myFileOut)
  Dim stream ,strTextText
  Set stream = CreateObject("ADODB.Stream")

  stream.Open
  stream.Type = 2 'text
  stream.LoadFromFile myFileIn
  stream.Position = 0
  stream.Charset = "gb2312"
  strText = stream.ReadText
  stream.Close

  stream.Open
  stream.Type = 2
  stream.Position = 0
  stream.Charset = "utf-8"
  stream.WriteText strText
  stream.SaveToFile myFileOut, 2
  stream.Close
  Set stream = Nothing

End Function

enter image description here

1 个答案:

答案 0 :(得分:6)

你必须设置

stream.Type

stream.Charset
在打开流之前

默认情况下,stream.Position为0。

问候

阿克塞尔