这个问题因扰了我很久,先在VB6 中测试多种方法,但都没有成功,最后先使用VB.Net 产生一个转换用的dll ,然后再在VB6 中调用此DLL, 以前是这样解决的。可以,有时也有问题,也是很笨的方法。
最终找到一个很简单的方法,如下,使用adodb.stream 可很方便的解决。
(其中参数为UTF-8格式文件的路径和文件名;返回一个字符串)
Public Function ReadUTF8(ByVal sUTF8File As String) As String
If Len(sUTF8File) = 0 Or Dir(sUTF8File) = vbNullString Then Exit Function
Dim ados As Object
Set ados = CreateObject("adodb.stream")
With ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile sUTF8File
ReadUTF8 = .ReadText
.Close
End With
Set ados = Nothing
End Function
2008-06-03 增加直接文件对文件的转换,代码如下: (其中的两个参数均为文件路径及名件名)
Public Function UTF8_ANSI(ByVal sUTF8File As String, ByVal sANSIFile As String) As Boolean
On Error GoTo errHandler
UTF8_ANSI = False
If Len(sUTF8File) = 0 Or Dir(sUTF8File) = vbNullString Then Exit Function
Dim ados As Object
Set ados = CreateObject("adodb.stream")
Dim fso As Object
Dim txtFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.CreateTextFile(sANSIFile, True)
With ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile sUTF8File
txtFile.Write (.ReadText)
txtFile.Close
.Close
End With
Set ados = Nothing
UTF8_ANSI = True
errHandler:
If Err.Number <> 0 Then
MsgBox Err.Description, vbOKOnly + vbInformation, "UTF8_ANSI"
End If
End Function