返回首页
当前位置: 主页 > 编程语言 > VB教程 >

VB:如何读出unicode编码的txt文件内容

时间:2014-10-07 23:47来源:电脑教程学习网 www.etwiki.cn 编辑:admin

其实,这个问题很简单 ,可偏偏很多人不知道,于是写了这个
先用2进制方式打开文件,读出数据到byte数组中,然后用copymemory去掉文件头(头两个字符分别是FF FE

(16进制))得到一个新的byte数组,最后利用strconv函数即可,下面给出代码:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, source

As Any, ByVal Length As Long)

Private Function ReadUniFile(ByVal sFile As String) As String
'没加错误处理,大家自己加吧
Dim a As Long
a = FileLen(sFile)
ReDim buff(a - 1) As Byte
ReDim buff1(a - 3) As Byte
Open sFile For Binary As #1
Get #1, , buff
Close #1
CopyMemory buff1(0), buff(2), a - 2
Dim s As String
s = StrConv(buff1, vbNarrow)
ReadUniFile = s
End Function


自己的宏


Sub 读取txt文件()
With ActiveSheet.QueryTables.Add(Connection:="TEXT;c:\123.txt", Destination:=Range("A1"))
.Name = "123"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
 

------分隔线----------------------------
标签(Tag):vb教程
------分隔线----------------------------
推荐内容
猜你感兴趣