⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tip07.htm

📁 对于学习很有帮助
💻 HTM
字号:
<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 3.0"><title>在Delphi中处理word文档与数据库的互联</title></head><body bgcolor="#FFFBDF"><table border="0" width="100%" style="font-size: 9pt">  <tr>    <td width="100%"><p align="center"><strong>在Delphi中处理word文档与数据库的互联</strong>     </p>    <p><font color="#ffffff">----</font> 目前,Delphi被越来越多的人选中作为MIS系统开发中的前台工具。在以Delphi为前台,一些大型数据库为后台的MIS系统中,图形的处理不可避免;即从以Delphi开发的前台界面输入图形,并保存到相应的数据库字段中。在这种形式的图形处理中,BMP文件的处理比较简单,因为Delphi本身有Image和DBImage构件,用这些构件与数据库中可以保存图形的大型字段BLOB比较容易地进行数据交换。以这种方式进行图形处理已应用在许多MIS软件中,包括处理人员照片的人事档案系统等。     </p>    <p><font color="#ffffff">----</font> 但是,BMP文件一般都比较大。而且有时要录入的是自己在计算机上画的简图,并伴随大量文字说明。这种情况用Win95中的画图板等处理BMP文件的工具处理就比较困难。一般应用人员都喜欢用WORD画图和写说明文字,然后保存到数据库中。     </p>    <p><font color="#ffffff">----</font>     经过一段时间的摸索,我们解决了这个问题,并经过完善,在应用中运行较好。程序如下:     <br>    procedure TsampleForm.OpenDOCClick(Sender: TObject);<br>    var<br>    &nbsp; MemSize: Integer;<br>    &nbsp; Buffer: PChar;<br>    &nbsp; MyFile: TFileStream;<br>    &nbsp; Stream: TBlobStream;<br>    begin<br>    &nbsp; OpenDialog1.Filter:='WORD文档(*.DOC)|*.DOC';{从对话窗选择文件}<br>    &nbsp; if OpenDialog1.Execute then <br>    &nbsp; begin<br>    &nbsp;&nbsp;&nbsp; MyFile:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);<br>    &nbsp;&nbsp;&nbsp; with table1 do&nbsp;&nbsp; {‘table1’为含BLOB字段的表名}<br>    &nbsp;&nbsp;&nbsp; begin<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Open;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Edit;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream := TBlobStream.Create(FieldByName('Doc') as     TBlobField, bmWrite);{‘Doc’为BLOB字段名} <br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemSize := MyFile.Size;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inc(MemSize); {Make room for the&nbsp; buffer's null     terminator.}<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Buffer := AllocMem(MemSize); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     {Allocate the memory.} <br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream.Seek(0, soFromBeginning);&nbsp; {Seek 0     bytes from the stream's end point}<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyFile.Read(Buffer^,MemSize);<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream.Write(Buffer^,MemSize);<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finally<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyFile.Free;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stream.Free;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Post;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; except<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; on E: EDatabaseError do<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if HandelException(E)&lt; &gt;0 then <br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     exit<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise;<br>    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<br>    &nbsp;&nbsp;&nbsp;&nbsp; end;<br>    &nbsp;&nbsp;&nbsp;&nbsp; Doc_ole.CreateObjectFromFile(OpenDialog1.FileName,False);<br>    &nbsp;&nbsp;&nbsp;&nbsp; Doc_ole.Run;{Doc_ole为ToleContainer构件名}<br>    &nbsp;&nbsp; end;<br>    end;<br>    </p>    <p><font color="#ffffff">----</font>     以上为向数据库中写入的程序,应用中从对话窗取出文件在ToleContainer构件中显示的同时存入数据库。     </p>    <pre>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;</pre>    <p><font color="#ffffff">----</font> 以上程序为从数据库从将WORD文档取出,并放在temp.doc的临时文件上并在ToleContainer构件中显示。     </p>    <p><font color="#ffffff">----</font>     在程序的其他部份应准确控制表记录指针,使WORD文档的存取发生在正确的记录位置。     </p>    <hr>    <p align="center">中国计算机世界出版服务公司版权所有</td>  </tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -