惊
惊风未动
Unregistered / Unconfirmed
GUEST, unregistred user!
使用ole对象来存储.doc类型的文档时,编译执行后总提示'invaild stream format'
我的代码如下
procedure TForm1.Button1Click(Sender: TObject);
var
olestream:Tblobstream;//这里用TMemoryStream也不行
begin
olestream:=nil;
try
adotable1.Open;
try
olestream:=tblobstream.Create(adotable1.fieldbyname('doc') as tblobfield,bmread);
// olestream.Position:=0;
olecontainer1.LoadFromStream(olestream);
olecontainer1.
except
messagedlg(' 打开数据库失败',mtwarning,[mbok],0);
end;
finally
olestream.Free;
adotable1.Close;
end;
end;
//我用的是access数据库,Doc字段的数据类型为ole对象,实在解决不了,请各位高手指点指点
另外,下面这段代码是从书上copy下来的,但编译执行的时候总是提示要转换文件类型,转换之后
word 文件就变成乱码了,保存文件那部分根本无法实现
procedure TForm1.Button2Click(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
MyFile: TFileStream;
Stream: TBlobStream;
filename:string;
begin
Stream:=nil;
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, bmReadWrite);{‘Doc’为BLOB字段名}
MemSize := MyFile.Size;
Inc(MemSize);
//增加的一字节给结尾的null
// 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)+'字节';
// doc_ole.CreateObjectFromFile(filename,true);
// doc_ole.SaveToStream(stream);
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;
OLEStream: TBlobStream;
begin
MyFile:=TFileStream.Create('c:/temp.tmp',fmCreate);}
with table1 do
begin
Open;
OLEStream:=TBlobStream.Create(FieldbyName('Doc')as TBlobField,bmRead);
MemSize := OLEStream.Size;
Inc(MemSize);
Buffer := AllocMem(MemSize);
try
Stream.Read(Buffer^,MemSize);
MyFile.Write(Buffer^,MemSize);
statictext1.caption:='文件名:'+table1.FieldByName('Name').Asstring;
statictext2.caption:='文件大小:'+table1.FieldByName('Size').Asstring+'字节';
finally
MyFile.Free;
OLEStream.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;
//顺便问一下,要做一个文档管理系统(包括图象,word 文件),除了用ole 对象存储,还可以有
什么方法,可不可以提供源代码,谢谢。我的email:fpefight@sohu.com 或fanfpefight@hotmail.com
我的代码如下
procedure TForm1.Button1Click(Sender: TObject);
var
olestream:Tblobstream;//这里用TMemoryStream也不行
begin
olestream:=nil;
try
adotable1.Open;
try
olestream:=tblobstream.Create(adotable1.fieldbyname('doc') as tblobfield,bmread);
// olestream.Position:=0;
olecontainer1.LoadFromStream(olestream);
olecontainer1.
except
messagedlg(' 打开数据库失败',mtwarning,[mbok],0);
end;
finally
olestream.Free;
adotable1.Close;
end;
end;
//我用的是access数据库,Doc字段的数据类型为ole对象,实在解决不了,请各位高手指点指点
另外,下面这段代码是从书上copy下来的,但编译执行的时候总是提示要转换文件类型,转换之后
word 文件就变成乱码了,保存文件那部分根本无法实现
procedure TForm1.Button2Click(Sender: TObject);
var
MemSize: Integer;
Buffer: PChar;
MyFile: TFileStream;
Stream: TBlobStream;
filename:string;
begin
Stream:=nil;
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, bmReadWrite);{‘Doc’为BLOB字段名}
MemSize := MyFile.Size;
Inc(MemSize);
//增加的一字节给结尾的null
// 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)+'字节';
// doc_ole.CreateObjectFromFile(filename,true);
// doc_ole.SaveToStream(stream);
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;
OLEStream: TBlobStream;
begin
MyFile:=TFileStream.Create('c:/temp.tmp',fmCreate);}
with table1 do
begin
Open;
OLEStream:=TBlobStream.Create(FieldbyName('Doc')as TBlobField,bmRead);
MemSize := OLEStream.Size;
Inc(MemSize);
Buffer := AllocMem(MemSize);
try
Stream.Read(Buffer^,MemSize);
MyFile.Write(Buffer^,MemSize);
statictext1.caption:='文件名:'+table1.FieldByName('Name').Asstring;
statictext2.caption:='文件大小:'+table1.FieldByName('Size').Asstring+'字节';
finally
MyFile.Free;
OLEStream.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;
//顺便问一下,要做一个文档管理系统(包括图象,word 文件),除了用ole 对象存储,还可以有
什么方法,可不可以提供源代码,谢谢。我的email:fpefight@sohu.com 或fanfpefight@hotmail.com