需求源头:
OleContainer操作Excel,想把Excel以二进制方式存入数据库,并且以二进制方式读取
存入流:
Procedure SaveToStream(ADOTable1: TAdoTable)
var
vStream: TMemoryStream;
begin
try
try
vStream := TMemoryStream.Create;
vStream.Clear;
OleContainer1.SaveToStream(vStream);
vStream.Position := 0;
ADOTable1.Append;
TBlobField(ADOTable1.FieldByName("BlobField")).LoadFromStream(vStream);
ADOTable1.Post;
finally
FreeAndNil(vStream);
end;
except
exit;
end;
end;
读取流:
procedure LoadFromStream(ADOTable1: TAdoTable)
var
//注意:这里要用TADOBlobStream,如果用TMemoryStream等流类,则会出现错误
//OleContainer的二进制流格式和Access的二进制流格式不同
//TADOBlobStream是专门用来Access的流转化的,TADOBlobStream继承自TMemoryStream
vStream: TADOBlobStream;
begin
try
vStream := TADOBlobStream.Create(TBlobField(ADOTable1.FieldByName("BlobField")), bmread);
if (OleContainer1.State = osOpen) or (OleContainer1.State = osUIActive) then
begin
OleContainer1.DestroyObject;
end;
vStream.Position := 0;
Olecontainer1.LoadFromStream(vStream);
except
end;
end;
问题总结:
注意流格式的问题。
注:附件里有我的一个小工具,用来帮助自己平时操作Excel在Access中的读取。http://www.delphibbs.com/keylife/iblog_show.asp?xid=21941
http://blog.csdn.net/shuaihj/article/details/725534