如何用DELPHI实现把WORD、EXCEL和图片等存储到数据库中
用image字段保存這些文檔。??
? var??
????? word_stream:?? TMemoryStream;??
????? filename:?? string;??
? begin??
????? if?? odgDoc.Execute?? then//odgDoc:OpenDialog??
????? begin??
????????? filename?? :=?? ExtractFileName(odgDoc.FileName);??
????????? word_stream?? :=?? TMemoryStream.Create;??
????????? word_stream.LoadFromFile(odgDoc.FileName);??
????????? word_stream.Position?? :=?? 0;??
????????? cdsPACT.Append??
????????? cdsPACT.FieldByName('DocName').Value?? :=?? filename;??
????????? TBlobField(cdsPACT.FieldByName('PactText')).LoadFromStream(word_stream);??
????????? cdsPACT.Post;??
????????? word_stream.Free;??
????? end;??
? end;
----?? 目前,Delphi被越來越多的人選中作為MIS系統(tǒng)開發(fā)中的前臺工具。在以Delphi為前臺,一些大型數(shù)據(jù)庫為后臺的MIS系統(tǒng)中,圖形的處理不可避 免;即從以Delphi開發(fā)的前臺界面輸入圖形,并保存到相應的數(shù)據(jù)庫字段中。在這種形式的圖形處理中,BMP文件的處理比較簡單,因為Delphi本身 有Image和DBImage構件,用這些構件與數(shù)據(jù)庫中可以保存圖形的大型字段BLOB比較容易地進行數(shù)據(jù)交換。以這種方式進行圖形處理已應用在許多 MIS軟件中,包括處理人員照片的人事檔案系統(tǒng)等。??????
? ----?? 但是,BMP文件一般都比較大。而且有時要錄入的是自己在計算機上畫的簡圖,并伴隨大量文字說明。這種情況用Win95中的畫圖板等處理BMP文件的工具 處理就比較困難。一般應用人員都喜歡用WORD畫圖和寫說明文字,然后保存到數(shù)據(jù)庫中。????
? ----?? 經(jīng)過一段時間的摸索,我們解決了這個問題,并經(jīng)過完善,在應用中運行較好。程序如下:????
? procedure?? TsampleForm.OpenDOCClick(Sender:?? TObject);??
? var??
????? MemSize:?? Integer;??
????? Buffer:?? PChar;??
????? Myfile:?? TFileStream;??
????? Stream:?? TBlobStream;??
? begin??
????? OpenDialog1.Filter:='WORD文檔(*.DOC)|*.DOC';{從對話窗選擇文件}??
????? if?? OpenDialog1.Execute?? then????
????? begin??
????????? Myfile:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);??
????????? with?? table1?? do?????? {‘table1’為含BLOB字段的表名}??
????????? begin??
????????????? Open;??
????????????? Edit;??
????????????? Stream?? :=?? TBlobStream.Create(FieldByName('Doc')?? as?? TBlobField,?? bmWrite);{‘Doc’為BLOB字段名}????
????????????? MemSize?? :=?? MyFile.Size;??
????????????? Inc(MemSize);?? {Make?? room?? for?? the???? buffer's?? null?? terminator.}??
????????????? Buffer?? :=?? AllocMem(MemSize);?????????????? {Allocate?? the?? memory.}????
????????????? try??
????????????????? Stream.Seek(0,?? soFromBeginning);???? {Seek?? 0?? bytes?? from?? the?? stream's?? end?? point}??
????????????????? MyFile.Read(Buffer^,MemSize);??
????????????????? Stream.Write(Buffer^,MemSize);??
????????????? finally??
????????????????? MyFile.Free;??
????????????????? Stream.Free;??
????????????? end;??
??????????????????????? try??
????????????????????????? Post;??
??????????????????????? except??
????????????????????????? on?? E:?? EDatabaseError?? do??
??????????????????? if?? HandelException(E)<?? >0?? then????
????????????????????????????????????????????????? exit??
????????????????? else??
????????????????? raise;??
??????????????????????? end;??
??????????? end;??
??????????? Doc_ole.CreateObjectFromFile(OpenDialog1.FileName,False);??
??????????? Doc_ole.Run;{Doc_ole為ToleContainer構件名}??
??????? end;??
? end;??
???
? ----?? 以上為向數(shù)據(jù)庫中寫入的程序,應用中從對話窗取出文件在ToleContainer構件中顯示的同時存入數(shù)據(jù)庫。????
? procedure?? TsampleForm.GetDocClick(Sender:?? TObject);??
? var??
????? MemSize:?? Integer;??
????? Buffer:?? PChar;??
????? Myfile:?? TFileStream;??
????? Stream:?? TBlobStream;??
? begin??
????????? Myfile:=TFileStream.Create('c:\temp.tmp',fmCreate);??
????????? with?? Query1?? do??
????????? begin??
????????????? Stream?? :=?? TBlobStream.Create(FieldByName('Doc')?? as?? TBlobField,?? bmRead);??
????????????? MemSize?? :=?? Stream.Size;??
????????????? Inc(MemSize);?? {Make?? room?? for?? the?? buffer's?? null?? terminator.}??
????????????? Buffer?? :=?? AllocMem(MemSize);?????????? {Allocate?? the?? memory.}??
????????????? try??
????????????????? Stream.Read(Buffer^,MemSize);??
????????????????? MyFile.Write(Buffer^,MemSize);??
????????????? finally??
????????????????? MyFile.Free;??
????????????????? Stream.Free;??
????????????? end;??
????????? end;??
????????????? if?? FileExists('c:\temp.DOC')?? then????
????? DeleteFile('c:\temp.DOC');??
????????????? if?? FileExists('c:\temp.tmp')?? then????
????????????? begin??
????????????????? RenameFile('c:\temp.tmp',?? 'c:\temp.DOC');??
????????????????? Doc_ole.CreateObjectFromFile('c:\temp.DOC',False);??
????????????????? Doc_ole.Run;??
????????????? end;??
? end;??
? ----?? 以上程序為從數(shù)據(jù)庫從將WORD文檔取出,并放在temp.doc的臨時文件上并在ToleContainer構件中顯示。????
? ----?? 在程序的其他部份應準確控制表記錄指針,使WORD文檔的存取發(fā)生在正確的記錄位置。
?
posted on 2009-07-27 12:44 江宇旋 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/jiangyuxuan/archive/2009/07/27/1531952.html
總結
以上是生活随笔為你收集整理的如何用DELPHI实现把WORD、EXCEL和图片等存储到数据库中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 得到计算机名
- 下一篇: DataGridView加入CheckB