VBA-UTF-8文件的操作

Posted on 2020-02-26 22:38  wzhw2015  阅读(2117)  评论(0编辑  收藏  举报
1.读入UTF-8文件
   Dim value As String
   Dim vLines As Variant

   Call ReadCsvUTF_8(vFile.Path, value)

   vLines = Split(value, vbCrLf)

   For i = 0 To UBound(vLines)
     ...
   next
  Public Sub ReadCsvUTF_8(ByVal fileName As String, ByRef value As String)
    With CreateObject("ADODB.Stream")
        .Charset = "UTF-8"
        .Open
        .LoadFromFile fileName
        value = .ReadText
        .Close
     End With
  End Sub
 
2.写入UTF-8文件
  Call WriteCsvUTF_8(fileSaveName, strLine)
  Public Sub WriteCsvUTF_8(ByVal fileName As String, ByVal strLine As String)

    With CreateObject("ADODB.Stream")

      .Open
      .Charset = "UTF-8"
      .Type = adTypeText
      .writeText strLine
      .Position = 3
      .SaveToFile fileName, adSaveCreateOverWrite
      .Flush
      .Close

   End With

 End Sub

Copyright © 2024 wzhw2015
Powered by .NET 8.0 on Kubernetes