以下代码可将二进制文件读进数据库以及读出 写进数据库的代码: Dim delsql As String Dim db As Database Dim reporttab As Recordset Set db = OpenDatabase("f:\报表系统\reportmodal.mdb") Set reporttab = db.OpenRecordset("reportmodal", dbOpenDynaset)
Dim FileNumber As Integer, DataLen As Long Dim Chunks As Long, ChunkAry() As Byte Dim ChunkSize As Long, Fragment As Long Dim str5 As String ''On Error GoTo errh:
FileNumber = FreeFile Open FileName For Binary Access Read As FileNumber DataLen = LOF(FileNumber) ' 档案中资料的长度 If DataLen = 0 Then Close FileNumber: Exit Function Chunks = DataLen \ ChunkSize Fragment = DataLen Mod ChunkSize reporttab.AddNew reporttab!frptname = RPTNAME ReDim ChunkAry(Fragment - 1) Get FileNumber, , ChunkAry() reporttab.Fields("frptdata").AppendChunk ChunkAry ReDim ChunkAry(ChunkSize - 1) For i = 1 To Chunks Get FileNumber, , ChunkAry() reporttab.Fields("frptdata").AppendChunk ChunkAry Next i reporttab.Update Close FileNumber AppendBlobFromFile = True db.Close Set reporttab = Nothing 从数据库读出的代码: Dim intfilenum As Integer Dim lngsize As Long Dim intchunks As Integer Dim intremainder As Integer Dim ardata() As Byte Dim i As Integer Dim lngaffected As Long Dim befordata As Long Dim findstr As String Dim db As Database Dim reporttab As Recordset Set db = OpenDatabase("f:\方文书用\报表系统\reportmodal.mdb", dbOpenDynaset) Set reporttab = db.OpenRecordset("reportmodal", dbOpenDynaset)
If reporttab.NoMatch Then MsgBox "打开报表错误,请与灵通公司联系!", vbExclamation, msgtitle Exit Function End If
FileName = App.Path + "\rpttmp.kts" intfilenum = FreeFile Open FileName For Binary Access Write As intfilenum lngsize = reporttab.Fields("frptdata").FieldSize
intchunks = lngsize \ 32768 intremainder = lngsize Mod 32768 ReDim ardata(8192) For i = 1 To intchunks ardata() = reporttab.Fields("frptdata").GetChunk(befordata, 32768) befordata = befordata + 32768 Put intfilenum, , ardata() Next i
ReDim ardata(intremainder) ardata() = reporttab.Fields("frptdata").GetChunk(befordata, intremainder) Put intfilenum, , ardata()
Close intfilenum
For i = 1 To 800 .row = i .col = .MaxCol - 1 If Trim(.Text) = "" Then Exit For formulastr(i, 1) = Trim(.Text) .col = .MaxCol formulastr(i, 2) = Trim(.Text) Next i
cellformulas(1) = i - 1 .ColHidden(.MaxCol - 1) = True .ColHidden(.MaxCol) = True End With db.Close Set reporttab = Nothing
可以用VB中的引用,将WORD.APPLICATION 做为一个对象,然后再打开文件,再用WORD.document 对象的另存为方法将其保存为RTF 格式,再用RICHBOX 的 loadfile 方法来调入RICHBOX 显示。 Option Explicit Dim w As New Word.Application Dim doc As Word.DocumentPrivate Sub Form_Resize() rb.Left = 0 rb.Top = 0 rb.Width = Me.ScaleWidth rb.Height = Me.ScaleHeight End SubPrivate Sub Form_Unload(Cancel As Integer) w.Quit Set w = Nothing End SubPrivate Sub mun_Load_Click() '假设你已将文件完整的从数据库中DUMP 出来,存为一个文件。 'Set doc = w.Documents.Open("C:\My Documents\《新神雕侠侣》一篇全攻略.doc", , True) Set doc = w.Documents.Open("C:\My Documents\FlightSim_Com Review Phoenix Simulation Software Boeing 777.htm", , True) doc.SaveAs "c:\temp\temp.rtf", wdFormatRTF doc.Close Set doc = Nothing rb.LoadFile "c:\temp\temp.rtf" End Sub
HTML的话需要建立一个过程分析代码,可以用ShdocVW+MSHtml
www.applevb.com
2.使用ADO的getchunk()和appendchunk()
用HTMLView
FSO方式不支持二进制方式。
写进数据库的代码:
Dim delsql As String
Dim db As Database
Dim reporttab As Recordset
Set db = OpenDatabase("f:\报表系统\reportmodal.mdb")
Set reporttab = db.OpenRecordset("reportmodal", dbOpenDynaset)
Dim FileNumber As Integer, DataLen As Long
Dim Chunks As Long, ChunkAry() As Byte
Dim ChunkSize As Long, Fragment As Long
Dim str5 As String
''On Error GoTo errh:
FileName = App.Path + "\rpttmp.kts"
AppendBlobFromFile = False
ChunkSize = 2048
FileNumber = FreeFile
Open FileName For Binary Access Read As FileNumber
DataLen = LOF(FileNumber) ' 档案中资料的长度
If DataLen = 0 Then Close FileNumber: Exit Function
Chunks = DataLen \ ChunkSize
Fragment = DataLen Mod ChunkSize
reporttab.AddNew
reporttab!frptname = RPTNAME
ReDim ChunkAry(Fragment - 1)
Get FileNumber, , ChunkAry()
reporttab.Fields("frptdata").AppendChunk ChunkAry
ReDim ChunkAry(ChunkSize - 1)
For i = 1 To Chunks
Get FileNumber, , ChunkAry()
reporttab.Fields("frptdata").AppendChunk ChunkAry
Next i
reporttab.Update
Close FileNumber
AppendBlobFromFile = True
db.Close
Set reporttab = Nothing
从数据库读出的代码:
Dim intfilenum As Integer
Dim lngsize As Long
Dim intchunks As Integer
Dim intremainder As Integer
Dim ardata() As Byte
Dim i As Integer
Dim lngaffected As Long
Dim befordata As Long
Dim findstr As String
Dim db As Database
Dim reporttab As Recordset
Set db = OpenDatabase("f:\方文书用\报表系统\reportmodal.mdb", dbOpenDynaset)
Set reporttab = db.OpenRecordset("reportmodal", dbOpenDynaset)
findstr = "frptname='" & Trim(reportname) & "'"
reporttab.FindFirst findstr
If reporttab.NoMatch Then
MsgBox "打开报表错误,请与灵通公司联系!", vbExclamation, msgtitle
Exit Function
End If
FileName = App.Path + "\rpttmp.kts"
intfilenum = FreeFile
Open FileName For Binary Access Write As intfilenum
lngsize = reporttab.Fields("frptdata").FieldSize
intchunks = lngsize \ 32768
intremainder = lngsize Mod 32768
ReDim ardata(8192)
For i = 1 To intchunks
ardata() = reporttab.Fields("frptdata").GetChunk(befordata, 32768)
befordata = befordata + 32768
Put intfilenum, , ardata()
Next i
ReDim ardata(intremainder)
ardata() = reporttab.Fields("frptdata").GetChunk(befordata, intremainder)
Put intfilenum, , ardata()
Close intfilenum
For i = 1 To 800
.row = i
.col = .MaxCol - 1
If Trim(.Text) = "" Then Exit For
formulastr(i, 1) = Trim(.Text)
.col = .MaxCol
formulastr(i, 2) = Trim(.Text)
Next i
cellformulas(1) = i - 1
.ColHidden(.MaxCol - 1) = True
.ColHidden(.MaxCol) = True
End With
db.Close
Set reporttab = Nothing
2.使用ADO/DAO的appendchunk() 写入到数据库的字段(binary)
3.使用ADO/DAO的getchunk() 读入到文件
4.使用ole 控件显示内容
OLE1.SourceDoc = "c:\test.doc"
OLE1.Action = 0
我试过,对.doc 可以
Option Explicit
Dim w As New Word.Application
Dim doc As Word.DocumentPrivate Sub Form_Resize()
rb.Left = 0
rb.Top = 0
rb.Width = Me.ScaleWidth
rb.Height = Me.ScaleHeight
End SubPrivate Sub Form_Unload(Cancel As Integer)
w.Quit
Set w = Nothing
End SubPrivate Sub mun_Load_Click()
'假设你已将文件完整的从数据库中DUMP 出来,存为一个文件。
'Set doc = w.Documents.Open("C:\My Documents\《新神雕侠侣》一篇全攻略.doc", , True)
Set doc = w.Documents.Open("C:\My Documents\FlightSim_Com Review Phoenix Simulation Software Boeing 777.htm", , True)
doc.SaveAs "c:\temp\temp.rtf", wdFormatRTF
doc.Close
Set doc = Nothing
rb.LoadFile "c:\temp\temp.rtf"
End Sub
谢谢各位了。