|
- Sub Test()
- Dim arrFiles As Variant, strFileName As String
- Dim lngID As Long
-
- arrFiles = Application.GetOpenFilename(filefilter:="导入文件(*.csv;*.txt),*.csv;*.txt", MultiSelect:=True)
- If IsArray(arrFiles) Then
- For lngID = LBound(arrFiles) To UBound(arrFiles)
- strFileName = arrFiles(lngID)
- AnyCodeToAnsi strFileName
- Next
- End If
- End Sub
- Function AnyCodeToAnsi(strFilePath As String)
- Dim ObjStream As Object, strType As String
- Dim btyBom As Variant, strNewFilePath As String
- Dim strOldCode As String, strNewCode As String
- Dim strContent As String
-
- Set ObjStream = CreateObject("adodb.stream")
- With ObjStream
- .Type = 1
- .Mode = 3
- .Open
- .Position = 0
- .LoadFromFile strFilePath
- btyBom = .read(2)
- End With
-
- strType = Hex(btyBom(0)) & Hex(btyBom(1))
- Select Case strType
- Case "FFFE"
- strOldCode = "Unicode"
- Case "FEFF"
- strOldCode = "Unicode big endian"
- Case "EFBB"
- strOldCode = "UTF-8"
- Case Else 'ANSI
- strOldCode = "GB2312" '汉字
- End Select
- If strOldCode = "GB2312" Then
- ' MsgBox "文件格式为ANSI,不需要转换!"
- Exit Function
- End If
- strNewCode = "GB2312"
- btyBom = Split(strFilePath, ".")
- strNewFilePath = btyBom(0) & Int(Rnd(1) * 1000) & "-Gb2312." & btyBom(1)
-
-
-
- With ObjStream
- .Position = 0
- .Type = 2
- .Charset = strOldCode
- strContent = .ReadText
-
- .Position = 0
- .SetEOS
- .Type = 2
- .Charset = strNewCode
- .WriteText strContent
- .SaveToFile strNewFilePath, 2
- End With
-
- ObjStream.Close
- Set ObjStream = Nothing
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|