OleContainer 的LoadFromStream 和SaveToStream(100分)

  • 主题发起人 主题发起人 chengjian
  • 开始时间 开始时间
C

chengjian

Unregistered / Unconfirmed
GUEST, unregistred user!
我将OleContainer 中的 WORD 文档,存入到数据库中,然后取出来显示,为什么出错?
代码如下:

OleContainer1.OldStreamFormat := True;
Word_FileStream := TFileStream.Create('TMP.doc',fmOpenWrite or fmCreate);
Word_FileStream.Position:=0;
try
OleContainer1.SaveToStream(Word_FileStream);
ADOQuery1.Edit;
TBlobField(ADOQuery1.FieldByName('filedefine_data')).loadfromStream(Word_FileStream);
ADOQuery1.Post;
MessageBox(Handle,'保存文件成功!','系统提示', MB_OK + MB_ICONINFORMATION);
ShowMessage('close');
OleContainer1.Close;
ShowMessage('load');
OleContainer1.LoadFromStream(Word_FileStream); ////出错了!
//出错信息:Stream read error
finally
Word_FileStream.Free;
end;


请各位大侠帮助!
 

Var
OleStream : TMemoryStream;
begin
OleStream := TMemoryStream.Create;
try
DBOA.QModuleList.Locate('DocModuleID',ModuleID,[loCaseInsensitive]);
(DBOA.QModuleList.FieldByName('content') as TBlobField).SaveToStream(OleStream);
OleStream.Position := 0;
if DBOA.QModuleList.FieldByName('content').IsNull then
FrmWord.OleContainer1.CreateObjectFromFile(ExtractFilePath(application.ExeName)+'sample',false)
else
FrmWord.OleContainer1.LoadFromStream(OleStream);
FrmWord.OleContainer1.DoVerb(1); //ÏÔʾ
finally
OleStream.Free;
end;
end;


Var
OleStream : TMemoryStream;
begin
OleStream := TMemoryStream.Create;
try
FrmWord.OleContainer1.SaveToStream(OleStream);

OleStream.Position := 0;
DBOA.DSPlan.DataSet.edit;
//(DBOA.DSPlan.DataSet.FieldByName('content') as TBlobField).LoadFromStream(OleStream);
QPlanInDept.Post;
QPlanInDept.UpdateBatch;
DBOA.Qtmp.Close;
DBOA.Qtmp.SQL.Text := ' update DocInfo set content = :content where DocID = :DocID ';
DBOA.Qtmp.Parameters.ParamByName('content').LoadFromStream(OleStream,ftBlob);
DBOA.Qtmp.Parameters.ParamByName('DocID').Value := DBOA.DSPlan.DataSet.fieldbyname('DocID').AsInteger;
DBOA.Qtmp.ExecSQL;
finally
OleStream.Free;
end;
end;

 
To: Jafyliu
谢谢,但是问题还在。
我将TFileStream 改为TMemoryStream,还是不对!
 
1. 将 TFileStream 改为 TMemoryStream,
2. 在出错行前加一行 Word_FileStream.Position:=0;
 
to:myid0602
好了,谢谢!
 
后退
顶部