调用方法:
utf8Filetransfor(utf8file As String, newtextfile As String)
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Const CP_UTF8 = 65001
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
lReadL = 2500 * b '每次读入数据的长度字节长度,除以整行字节数后,余数不要太大,因为后面要进行行结束符的判断
If lReadL >= lFileLong Or LreadNow = lFileLong Then Exit Do '如果块大小等于文件大小,不用循环,直接退出
If LreadNow > lFileLong Then lFileLong = lFileLong - LreadNow
Loop
Close #Fn
f_txtstream.Close
Set f_txtstream = Nothing
Set fle = Nothing
Set fso = Nothing
writeGBKtext utf8file & "bak", utf8file
If Command = "" Or IsNull(Command) = True Then MsgBox "txtfile trim successful!"
Exit Sub
errexit:
If Err = 5 Then msg = "maybe have two reasons:" & Chr(10) & "1.This textfile's code not is Utf-8" & Chr(10) & "2.This textfile include East Asian language's words,but your computer not setup it"
If Command = "" Or IsNull(Command) = True Then MsgBox "error :" & Err.Number & "," & Err.Description & Chr(10) & msg, vbCritical, "err"
End Sub
Sub writeGBKtext(gbkfileName As String, UTFflileNAme As String) '文本整理
Dim fso As New FileSystemObject
Dim jj As Long
Dim ss_line As String, ss_new As String, ss_mid As String '存储旧的gbk单行文本,存储新的一行文本,存储单个文本
Dim ii As Integer, int_txtlong As Integer '存储单行文件循环变量
jj = 1
If Dir(gbkfileName & "x") <> "" Then Kill gbkfileName & "x" '判断新文件是否存在,如果存在则删除
Open gbkfileName & "x" For Append As 2# '打开新文件,如果不存在,则创建一个
Open gbkfileName For Input As 1# '打开已经transfor为gbk编码的文本文件
Do Until EOF(1) 'gbk文件循环开始
DoEvents
Line Input #1, ss_line '读入一行到ss_line
ss_new = "" 'ss_new赋值为空
ss_mid = "" 'ss_mid
int_txtlong = Len(ss_line)
For ii = 1 To int_txtlong '从第一个字符开始循环,取得每一个字的ascii码
ss_mid = Mid(ss_line, ii, 1) '取得一个字
If Asc(ss_mid) < 0 Then '判断字符asc码,如果小于0,则是非ascii文字