【发布时间】:2017-03-30 04:58:46
【问题描述】:
我有一个读取文本文件并从中提取某些数据的子程序。这是一个例子:
NamePrefix = "Example"
OutputPath = "C:\Example"
DbSize = 65536
LstStr = ""
Dim Success() As Boolean
Dim Value() As Double
ReDim Success(1 to DbSize)
ReDim Value(1 to DbSize)
For ID = 1 to DbSize
'Read string
FileName = NamePrefix & Format(ID,"000000") & ".lst"
FilePath = OutputPath & "\" & FileName
Open FilePath For Input As 1
LstStr = Input(LOF(1),1)
Close 1
'Extract data
If InStr(1, LstStr, "SUCCESS") <> 0 Then Success(i) = True Else Success(i) = False
Pos1 = InStr(1, LstStr, "TH 1 value: ") 'Position varies for each file
Value(i) = Val(Mid(LstStr, Pos1 + 13, 10)) 'Value in scientific notation
Next ID
当只有字母、数字和符号时,使用 InStr 按位置定位字符串非常有效。但是,有时文件中包含中文字符,Input 函数会向LstStr 返回一个空字符串“”。我尝试使用其他一些建议的方法但徒劳无功(例如Extract text from a text file with Chinese characters using vba)。我应该如何成功读取带有汉字的文件,而无需修改按位置提取数据的代码的其他部分?谢谢!
【问题讨论】:
-
您是否尝试使用
ADODB.Stream(具有正确的字符集)而不是 VBA 文件访问方法?