任何一个文件都是以字节的形式存储。
任何一个文件都可以使用Open语句打开进行读写操作。
当以Input 或者 Binary 模式用Open语句打开文件时,可以使用Input函数或者InputB函数读取打开的文件的字符流或者字节流,代码如下:
Sub QQ1722187970()
Dim iFN As Integer
iFN = VBA.FreeFile
Dim sPath As String
sPath = "c:\1.xls"
Dim bFileSize As Long
bFileSize = VBA.FileLen(sPath)
Debug.Print bFileSize
' Dim s() As Byte
Open sPath For Binary Access Read As iFN
Dim arr() As Byte
'读取字节流
arr = InputB(bFileSize, iFN)
Dim sResult As String
'读取字符流,vba中1个字符用2个字节存储
sResult = Input(bFileSize / 2, iFN)
'遍历字节流,返回16进制表示的字节
For i = 0 To UBound(arr)
Debug.Print VBA.Hex(arr(i))
Next i
Close
End Sub


发表评论