Mainfrm.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:3k
- unit Mainfrm;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, DB, DBTables, StdCtrls, OleCtnrs;
- type
- TForm1 = class(TForm)
- Database1: TDatabase;
- Table1: TTable;
- Button1: TButton;
- OpenDialog1: TOpenDialog;
- Button2: TButton;
- Doc_Ole: TOleContainer;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- procedure TForm1.Button1Click(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);
- //判断word文件大小是否大于BDE blobSize属性
- Listparam := TStringlist.Create;
- database1.Session.GetAliasParams('test', ListParam);
- blobSize := strToInt(ListParam.Values['Blob size']);
- if (myfile.Size) div 1024 > blobSize then
- raise exception.Create('word文件大于指定的大小');
- //将Doc文件转换成文件流
- with table1 do
- begin
- Open;
- insert;
- FieldByName('id').Value := 1;
- //将Doc字段的值存到Stream变量中,并标明Stream是可写的
- Stream := TBlobStream.Create(FieldByName('Doc') as TBlobField, bmWrite);
- MemSize := MyFile.Size;
- Inc(MemSize);
- //给缓冲区分配大小
- Buffer := AllocMem(MemSize);
- try
- //将Stream的指针移到最前面
- Stream.Seek(0, soFromBeginning);
- //将MyFile文件流读到Buffer缓冲区中
- MyFile.Read(Buffer^, MemSize);
- //将缓冲区的内容写到Stream
- Stream.Write(Buffer^, MemSize);
- finally
- MyFile.Free;
- Stream.Free;
- end;
- try
- //保存表的修改
- Post;
- except
- end;
- end;
- //Doc_ole是TOleContainer控件,放在窗体上,用它来显示Word文档
- Doc_ole.CreateObjectFromFile(OpenDialog1.FileName, False);
- Doc_ole.Run;
- end;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- var
- MemSize: Integer;
- Buffer: PChar;
- MyFile: TFileStream;
- Stream: TBlobStream;
- begin
- //创建一个临时文件
- MyFile := TFileStream.Create('c:temp.tmp', fmCreate);
- with Table1 do
- begin
- //将Doc字段的内容读到Stream中
- Stream := TBlobStream.Create
- (FieldByName('Doc') as TBlobField, bmRead);
- MemSize := Stream.Size;
- Inc(MemSize);
- //分配缓冲区的大小
- Buffer := AllocMem(MemSize);
- try
- //将Stream的字节读到Buffer缓冲区中
- Stream.Read(Buffer^, MemSize);
- //将Buffer缓冲区的内容读到MyFile文件流中
- MyFile.Write(Buffer^, MemSize);
- finally
- MyFile.Free;
- Stream.Free;
- end;
- end;
- //如果存在'c:temp.doc'则删除
- if FileExists('c:temp.DOC') then
- DeleteFile('c:temp.DOC');
- if FileExists('c:temp.tmp') then
- begin
- //重命名临时文件,将其后缀改为.Doc
- RenameFile('c:temp.tmp', 'c:temp.DOC');
- //用Doc_Ole控件来显示Word文档
- Doc_ole.CreateObjectFromFile('c:temp.DOC', False);
- Doc_ole.Run;
- end;
- end;
- end.