adoQryToExcell

procedure TDataModule1.adoQryToExcell(ADOQuery1: TADOQuery;RptName,TiTle1,TiTle2,TiTle3: string);
  var
   i,j,k:integer;
   EParam:OleVariant;
   DocuType:Olevariant;
   wkbk:_WorkBook;
   FileName:OleVariant;
begin
 if not ADOQuery1.Active then exit;
  if not DirectoryExists(ExtractFilePath(paramstr(0))+'导出文件') then
    if not CreateDir(ExtractFilePath(paramstr(0))+'导出文件') then
    begin
      raise Exception.Create('不能建立文件夹:导出文件 !');
      exit;
    end;

   Try
     EA.Connect;
   Except
     ShowMessage('EXCEL文件打开失败!');
     Exit;
   end;

   EA.Visible[0]:=false;
   EA.Caption:='Excel数据文件';

   EParam:=EmptyParam;
   DocuType:=0;
   try
      wkbk:=EA.Workbooks.Add(EParam,Docutype);
   except
      begin
          EA.Disconnect;
          EA.Quit;
          showmessage('创建EXCEL数据表格失败');
          exit;
      end;
   end;

   EWb.ConnectTo(EA.ActiveWorkbook);

   EWs.ConnectTo(EWb.Worksheets[1] as _worksheet);

   EWs.Cells.Item[1,1]:=TiTle1 ;
   EWs.Cells.Item[2,1]:=TiTle2;
   EWs.Cells.Item[3,1]:=TiTle3;

     begin
       for  I:=1  to ADOQuery1.Fields.Count do
          if ADOQuery1.Fields[I-1].Visible  then
            EWs.Cells.Item[4,I]:=ADOQuery1.Fields[I-1].DisplayName;
     end;

   ADOQuery1.First;
   i:=5;
    while  not ADOQuery1.Eof do
    BEGIN
       K:=1;
       for  j:=1  to ADOQuery1.Fields.Count do
       if ADOQuery1.Fields[j-1].Visible   then
       Begin
         if (ADOQuery1.Fields[J-1].DataType=ftDateTime) then
           EWs.cells.Item[i,K]:=FormatDateTime('yyyy-mm-dd',ADOQuery1.Fields[J-1].AsDateTime)
         else
           EWs.cells.Item[i,K]:= ' ' +Trim(ADOQuery1.Fields[J-1].AsString);
         K:=K+1;
       end;
       ADOQuery1.Next;
       i:=i+1;
    END;

  wkbk:=EA.ActiveWorkbook;
  DocuType:=0;
  FileName:=ExtractFilePath(Application.ExeName);
  FileName:=FileName +'导出文件/'+ Trim(RptName)+ '.xls';
  wkBk.Close(True,FileName,EmptyParam,DocuType);

  EA.Disconnect;
  EA.Quit;
  ShowMessage('数据导出成功');
end; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值