vba 使用“ADODB.Stream”将ANSI转换为UTF-8,第一行漏掉1-2个字符

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

Use "ADODB.Stream" to convert ANSI to UTF-8, miss 1-2 character in the first row

excelvbautf-8

提问by maomifadacai

I need to convert "ANSI" csv file to "UTF-8" csv file. Below code can work, but the first character miss Please see attached screen shot, the original file : Customer the output file : 﨏ustomer

我需要将“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

在此处输入图片说明

回答by Axel Richter

You have to set

你必须设置

stream.Type

and

stream.Charset

before you open the stream.

在您打开流之前。

And stream.Position is 0 by default.

并且 stream.Position 默认为 0。

Greetings

你好

Axel

阿克塞尔