Excel中VBA转换文件编码到UTF-8

Excel中VBA转换文件编码到UTF-8

网上找了很多, 真正好用的代码不多, 自己研究了下,写下例子备份

 

Private Sub ConvFile(InputFile As String, OutputFile As String)
 
    Dim ReadStream As Object
    Set ReadStream = CreateObject("ADODB.Stream")
    
    Dim FileContent As String
    
    With ReadStream
        .Type = 2               'adTypeText
        .Charset = "UNICODE"
        .Open
        .LoadFromFile InputFile
        FileContent = .ReadText
        .Close
        
    End With
    
    Set ReadStream = Nothing
    
    
    
    Dim WriteStream As Object
    Set WriteStream = CreateObject("ADODB.Stream")
       
    
    With WriteStream
        .Type = 2               'adTypeText
        .Charset = "UTF-8"
        .Open
        .WriteText FileContent
        .SaveToFile OutputFile, 2  'adSaveCreateOverWrite
        
        .Flush
        .Close
        
    End With
    
    Set WriteStream = Nothing
 
    
End Sub

 

上半截是读取文件, 下半截是写入文件, 需要转换不同格式, 请自行更换

你可能感兴趣的:(Excel中VBA转换文件编码到UTF-8)