encoding=’UTF-8′ 打开 strFile 以供输入,作为 intFile。

huangapple go评论63阅读模式
英文:

encoding='UTF-8' Open strFile For Input As intFile

问题

以下代码 VBA 无法正确读取波斯字母,需要使用正确的编码;编码为 'UTF-8'

Dim strFile As String
Dim intFile As Integer
Dim strBody As String
strFile = "C:\Plus.csv"
intFile = FreeFile
Open strFile For Input As intFile
strBody = Input(LOF(intFile), intFile)
Close intFile
英文:

the following code vba does not read Persian letters with a proper encoding ; encoding='UTF-8'

Dim strFile As String
Dim intFile As Integer
Dim strBody As String
strFile = "C:\Plus.csv"
intFile = FreeFile
Open strFile For Input As intFile
strBody = Input(LOF(intFile), intFile)
Close intFile

答案1

得分: 0

你可以使用Microsoft ActiveX数据对象库中的Stream对象来读取UTF-8编码文件的内容。

早期绑定

对于早期绑定,你首先需要设置一个引用(Visual Basic >> 工具 >> 引用)到以下库...

Microsoft ActiveX数据对象 x.x库

然后你可以使用关键字New来创建一个Stream对象的实例,并读取文件的内容...

Dim theStream As ADODB.Stream
Dim theContents As String

Set theStream = New ADODB.Stream

With theStream
    .Charset = "UTF-8"
    .Mode = adModeReadWrite
    .Type = adTypeText
    .Open
    .LoadFromFile "c:\users\domta\desktop\sample.txt"
    theContents = .ReadText
    .Close
End With

晚期绑定

对于晚期绑定,无需设置引用。但是你需要使用CreateObject方法创建一个Stream对象的实例,并读取文件的内容。

Dim theStream As Object
Dim theContents As String

Set theStream = CreateObject("ADODB.Stream")

With theStream
    .Charset = "UTF-8"
    .Mode = 3 'adModeReadWrite
    .Type = 2 'adTypeText
    .Open
    .LoadFromFile "c:\users\domta\desktop\sample.txt"
    theContents = .ReadText
    .Close
End With
英文:

You can use the Stream object from the Microsoft ActiveX Data Objects library the read the contents of a UTF-8 encoded file.

Early Binding

For early binding, you'll first need to set a reference (Visual Basic >> Tools >> References) to the following library...

Microsoft ActiveX Data Objects x.x Library

Then you can use the keyword New to create an instance of the Stream object, and read the contents of the file...

Dim theStream As ADODB.Stream
Dim theContents As String

Set theStream = New ADODB.Stream

With theStream
    .Charset = "UTF-8"
    .Mode = adModeReadWrite
    .Type = adTypeText
    .Open
    .LoadFromFile "c:\users\domta\desktop\sample.txt"
    theContents = .ReadText
    .Close
End With

Late Binding

For late binding, no reference needs to be set. However you'll need to use the CreateObject method to create an instance of the Stream object, and read the contents of the file.

Dim theStream As Object
Dim theContents As String

Set theStream = CreateObject("ADODB.Stream")

With theStream
    .Charset = "UTF-8"
    .Mode = 3 'adModeReadWrite
    .Type = 2 'adTypeText
    .Open
    .LoadFromFile "c:\users\domta\desktop\sample.txt"
    theContents = .ReadText
    .Close
End With

huangapple
  • 本文由 发表于 2023年6月5日 04:55:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定