使用Access VBA打开从Oracle数据库中的blob字段下载的excel文件时发现错误

时间:2017-08-15 18:58:29

标签: vba ms-access access-vba

我有以下代码,它将存储在链接的Oracle表中的blob字段中的数据下载到文件中。 blob数据存储excel文件(.xlsx),但是当我尝试在excel中打开下载的文件时,我收到一条错误消息,指出该文件已损坏且格式不正确。这是代码 -

Option Explicit
Const BlockSize = 32768

Public Function DownloadBlob()
  Dim db As Database
  Dim rst As Recordset
  Dim NumBlocks As Integer, DestFile As Integer, i As Integer
  Dim FileLength As Long, LeftOver As Long
  Dim FileData, FilePath As String
  Dim RetVal As Variant

  Set db = CurrentDb
  Set rst = db.OpenRecordset("Select Blob_Field FROM Table1;")

  NumBlocks = FileLength / BlockSize
  LeftOver = FileLength Mod BlockSize

  DestFile = FreeFile()
  FilePath = "C:\Desktop\test.xlsx"

  Open FilePath For Output As DestFile
  Close DestFile

  Open FilePath For Binary As DestFile
    FileData = rst.Fields(0).GetChunk(0, LeftOver)
    Put DestFile, , FileData
    For i = 1 To NumBlocks
        FileData = rst.Fields(0).GetChunk((i - 1) * BlockSize + LeftOver, BlockSize)
        Put DestFile, , FileData
    Next i
Close DestFile
End Function

0 个答案:

没有答案