delphi中处理OleVariant和TStream

来源:互联网 发布:淘宝2016促销活动时间 编辑:程序博客网 时间:2024/06/01 07:59
因为客户不知道从数据库中流字段到打开文档,编辑,再保存到数据库中流字段这个流程,这个流程涉及到了接口OpenDocumentWithStream和SaveToStream,而对应的VARIANT类型在delphi中展现为OleVariant,于是中间涉及到OleVariant转为TStream以及TStream转换为OleVariant,我写的一个例子如下:
function   TForm1.OleVariantToMemoryStream(OV:   OleVariant):   TMemoryStream;
  var  
      Data:   PByteArray;  
      Size:   integer;  
  begin  
      Result   :=   TMemoryStream.Create;  
      try
          Size   :=   VarArrayHighBound   (OV,   1)   -   VarArrayLowBound(OV,   1)   +   1;
          if   Size   =   0   then  
          begin  
              if   Result   <>   nil   then   Result.Free;  
              Result   :=   nil;  
              exit;  
          end;  
          Data   :=   VarArrayLock(OV);
          try  
              Result.Position   :=   0;
              Result.WriteBuffer(Data^,   Size);
          finally  
              VarArrayUnlock(OV);  
          end;  
      except  
          VarArrayUnlock(OV);  
          Result.Free;  
          Result   :=   nil;  
      end;  
  end;


function TForm1.StreamToVariant(Stream: TStream): OleVariant;
var
p: Pointer;
begin
  Result := VarArrayCreate([0, Stream.Size - 1], varByte);
  p := VarArrayLock(Result);
  try
    Stream.Position := 0;
    Stream.Read(p^, Stream.Size);
  finally
    VarArrayUnlock(Result);
  end;
end;


调用OpenDocumentWithStream
procedure TForm1.Button1Click(Sender: TObject);
var
    SQLString : string;
    BlobStream : TStream;
    FileStream : TFileStream;
    arr: OleVariant;
begin
    SQLString  := 'select * from 模板信息表 where ID = 4';
  with ADOQuery1 do
  begin
      Close;
      SQL.Clear;
      SQL.Add(SQLString);
      ConnectionString := ConnString;
      Open;
      BlobStream := CreateBlobStream(FieldByName('模板内容'), bmRead);
        if BlobStream.Size = 0 then
        begin
          BlobStream.Free;
          Exit;
        end;
        arr :=  StreamToVariant( BlobStream );
        BlobStream.Free;
    end;
     NsoControl1.OpenDocumentWithStream(arr,2);
end;

调用SaveToStream

procedure TForm1.Button2Click(Sender: TObject);
var
  arr: OleVariant;
  BlobStream: TStream;
  fileStream : TStream;
  SQLString :string;
  memo : TMemoryStream;
  i : Integer;
begin
  SQLString  := 'select * from 模板信息表 where ID = 4';
  NsoControl1.SaveToStream(arr);
  memo := OleVariantToMemoryStream(arr);
  //VariantToStream(arr,fileStream);
  i := memo.Size;
  if memo.Size = 0 then
    exit;
  with ADOQuery1 do
  begin
      Close;
      SQL.Clear;
      SQL.Add(SQLString);
      ConnectionString := ConnString;
      Open;
      if FieldByName('模板内容').IsBlob then
      begin
        Edit;
        BlobStream := CreateBlobStream(FieldByName('模板内容'), bmWrite);
        {Seek to the Begginning of the stream}
        BlobStream.Seek(0, soFromBeginning);
        memo.Position := 0;
        BlobStream.CopyFrom(memo, memo.Size);
        {Free the streams}
        BlobStream.Free;
        memo.Free;
        {Post the record}
        Post;
      end;
    end;
end;
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 运动完感觉很累怎么办 跑步后感觉很累怎么办 输液多了伤脾胃怎么办 运动完后特别晕怎么办 运动过量大腿肌肉酸痛怎么办 13岁发烧38.2度怎么办 头晕恶心想吐四肢无力怎么办 烧退了浑身疼怎么办 下午睡久了头疼怎么办 一天睡久了头疼怎么办 在家躺久了头疼怎么办 4周多儿童睡眠差怎么办 6岁儿童睡眠差怎么办 四年级的孩子不会写作文怎么办 四年级的孩子写不出作文怎么办? 欠了三万网贷怎么办 打完篮球浑身疼怎么办 在部队当兵意外死亡了怎么办 派派没有体力瓶怎么办 e记账登录不上怎么办 洁净净化区湿度高怎么办 政府测量土地少算了面积怎么办 北京武警欠我钱怎么办 被小混混打了怎么办 农保地建房子怎么办 WOW7.3到8.0橙装怎么办 90后离婚有孩子怎么办 90后的我们该怎么办 越南和中国结婚怎么办结婚证 改革怎么看走留怎么办事业怎么干 改革怎么看走留怎么办工作怎么干 军改怎么看我该怎么办 改革怎么办我该怎么干 改革怎么看 走留怎么办 腿又粗又弯怎么办 假发发量太多了怎么办 剃了发际线后悔怎么办 在外面遇到坏人抢劫怎么办 请事假单位不批怎么办 捷普请假不批怎么办 钉钉请假不审批怎么办