Adoquery导出到excel文件

procedure TFrm_Xzdmwh.SaveToExcelFile(FileName: String);
var
  eclApp, WorkBook: Variant;
  xlsFileName: string;
  StrXian, StrXiang, StrCun, StrSQL: String;
  SheetLine, i, j: Integer;
begin
  xlsFileName := FileName;
  try
    eclApp := CreateOleObject('Excel.Application');
    WorkBook:=CreateOleobject('Excel.Sheet');
  except
    ShowMessage('您的机器里未安装Microsoft Excel。');
    Exit;
  end;

  try
    workBook := eclApp.workBooks.Add;
    with tab_xian do
    begin
      StrXian := FieldByName('qhdm').AsString;
      eclApp.Cells(1, 2) := FieldByName('xzqm').AsString;
    end;

    SheetLine := 1;
    with ADOQueryIO do
    begin
      Close;
      SQL.Clear;
      StrSQL := Format('Select xzqxdm, xzqxm from xzqx where qhdm = ''%s'' ',[StrXian]);
      SQL.Add(StrSQL);
      Open;
      First;
      for i := 0 to RecordCount - 1 do
      begin
        SheetLine := SheetLine + 1;
        StrXiang := FieldByName('xzqxdm').AsString;
        eclApp.Cells(SheetLine, 3) := FieldByName('xzqxm').AsString;
        with ADOQueryIO1 do
        begin
          Close;
          SQL.Clear;
          StrSQL := Format('Select qhdm, xzqxdm, qsdm, qsqm from qsqy where xzqxdm = ''%s'' ',[StrXiang]);
          SQL.Add(StrSQL);
          Open;
          First;
          for j := 0 to RecordCount - 1 do
          begin
            SheetLine := SheetLine + 1;
            eclApp.Cells(SheetLine, 2) := Char(255) + FieldByName('qhdm').AsString;
            eclApp.Cells(SheetLine, 3) := Char(255) + FieldByName('xzqxdm').AsString;
            StrCun := FieldByName('qsdm').AsString;
            eclApp.Cells(SheetLine, 4) := Char(255) + StrCun;
            eclApp.Cells(SheetLine, 5) := FieldByName('qsqm').AsString;;
            if not eof then
              Next;
          end;
        end;
        if not eof then
          Next;
      end;
    end;

    WorkBook.saveas(xlsFileName);
    WorkBook.close;
    WorkBook:=eclApp.workBooks.Open(xlsFileName);
    if MessageDlg(xlsFileName+'文件已被修改,是否保存?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
      WorkBook.save
    else
      workBook.Saved := True; //放弃修改
    WorkBook.Close;
    eclApp.Quit;
    eclApp:=Unassigned;
  except
    ShowMessage('不能正确操作Excel文件。可能是该文件已被其他程序打开,或系统错误。');
    WorkBook.close;
    eclApp.Quit;
    eclApp:=Unassigned;
  end;
end;  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值