你必须使用"二进制访问"打开它.
请参阅http://www.vbforums.com/showthread.php?t=430424
Sub Temp()
Dim intFileNum%, bytTemp As Byte, intCellRow%
intFileNum = FreeFile
intCellRow = 0
Open "C:\temp.bin" For Binary Access Read As intFileNum
Do While Not EOF(intFileNum)
intCellRow = intCellRow + 1
Get intFileNum, , bytTemp
Cells(intCellRow, 1) = bytTemp
Loop
Close intFileNum
End Sub
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用ADODB.Stream:
With CreateObject("ADODB.Stream")
.Open
.Type = 1 ' adTypeBinary
.LoadFromFile file.Path
bytes = .Read
.Close
End With
Run Code Online (Sandbox Code Playgroud)
(对不起,我真的不确定它是什么库,这就是为什么这个示例代码使用CreateObject和文字值1而不是命名常量adTypeBinary!)
如果要将整个文件读入一个大数组中,则可以使用以下代码:
Dim byteArr() As Byte
Dim fileInt As Integer: fileInt = FreeFile
Open "C:\path\to\my\file.ext" For Binary Access Read As #fileInt
ReDim byteArr(0 To LOF(fileInt) - 1)
Get #fileInt, , byteArr
Close #fileInt
Run Code Online (Sandbox Code Playgroud)
结果与Todd Owen的答案相同,但无需使用外部库即可实现。