//ytwin你好,现把我的源代码给你,希望有用
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtnrs, ExtCtrls, DBTables, Db, DBCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Doc_Ole: TOleContainer;
Button1: TButton;
Table1: TTable;
Button2: TButton;
OpenDialog1: TOpenDialog;
Button3: TButton;
StaticText1: TStaticText;
StaticText2: TStaticText;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
MyFile: TFileStream;
Stream: TBlobStream;
filename:string;
begin
OpenDialog1.Filter:='WORD文档(*.DOC)|*.DOC';
if OpenDialog1.Execute then
begin
filename:=OpenDialog1.FileName;
MyFile:=TFileStream.Create(fileName,fmOpenRead);
with table1 do
begin
Open;
edit;
Stream := TBlobStream.Create(FieldByName('Doc') as TBlobField, bmWrite);{‘Doc’为BLOB字段名}
MemSize := MyFile.Size;
Inc(MemSize);
Buffer := AllocMem(MemSize);
try
Stream.Seek(0, soFromBeginning);
MyFile.Read(Buffer^,MemSize);
Stream.Write(Buffer^,MemSize);
FieldByName('Name').Asstring:=ExtractFileName(fileName);
FieldByName('Size').Asstring:=inttostr(MemSize-1);
statictext1.caption:='文件名:'+ExtractFileName(fileName);
statictext2.caption:='文件大小:'+inttostr(Memsize-1)+'字节';
finally
MyFile.Free;
Stream.Free;
end;
try
table1.Post;
except
showmessage('保存失败!');
halt;
end;
showmessage('保存成功,正在加载文档!');
Doc_ole.CreateObjectFromFile(fileName,False);
Doc_ole.Run;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
MyFile: TFileStream;
Stream: TBlobStream;
begin
MyFile:=TFileStream.Create('c:/temp.tmp',fmCreate);
with table1 do
begin
Open;
Stream:=TBlobStream.Create(FieldByName('Doc') as TBlobField, bmRead);
MemSize := Stream.Size;
Inc(MemSize);
Buffer := AllocMem(MemSize);
try
Stream.Read(Buffer^,MemSize);
MyFile.Write(Buffer^,MemSize);
statictext1.caption:='文件名:'+FieldByName('Name').Asstring;
statictext2.caption:='文件大小:'+FieldByName('Size').Asstring+'字节';
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;
procedure TForm1.Button3Click(Sender: TObject);
begin
doc_ole.CleanupInstance;
doc_Ole.Refresh;
statictext1.caption:='';
statictext2.caption:='';
end;
end.