导出excel的方法

procedure TfmSJGOrderMain.ActImportExecute(Sender: TObject);
var j,k,col:integer;
    sheet,exele:variant;
begin
  if not QDMain.Active then exit;
  if QDMain.IsEmpty then
  begin
    Application.MessageBox('数据表为空!','运行提示',mb_iconInformation+mb_ok);
    exit;
  end;
  Screen.Cursor:=crHourGlass;
  try
  exele:=CreateOleobject('Excel.Application');
  except
    Application.MessageBox('对不起!您可能没有装Excel,不能导出!','导出提示',mb_iconWarning+mb_ok);
    exit;
  end;
  exele.Workbooks.Add;
  exele.Worksheets[1].Name:='订单';
  sheet:=exele.Workbooks[1].Worksheets['单'];
  exele.WorkSheets[1].Activate;
  sheet.Cells[1,7].Value:='订单';
  sheet.cells[2,1].value:='客户代码';
  sheet.cells[3,1].value:=QDmain.FieldByName('CLCode').AsString;
  sheet.cells[2,3].value:='客户名称';
  sheet.cells[3,3].value:=QDmain.FieldByName('CLCLient').AsString;
  sheet.cells[2,5].value:='客户P.';
  sheet.cells[3,5].value:=QDmain.FieldByName('ORPO').AsString;
  sheet.cells[2,7].value:='订单类别';
  sheet.cells[3,7].value:=QDmain.FieldByName('ORSort').AsString;
  sheet.cells[2,9].value:='单 号';
  sheet.cells[3,9].value:=QDmain.FieldByName('ORDH').AsString;
  sheet.cells[2,11].value:='交货日期';
  sheet.cells[3,11].value:=QDmain.FieldByName('ORConsignDate').AsString;
  sheet.cells[2,13].value:='业务员';
  sheet.cells[3,13].value:=QDmain.FieldByName('ORSaleMan').AsString;
    col:=0;
    for j:=0 to grMain.fieldcount-1 do      //生成表头
      if grmain.columns[j].Visible then
        begin
          sheet.Cells[5,col+1].Value:=grmain.Columns[j].Title.Caption;
          inc(col);
        end;

    k:=6;   //从Excel表的第三行开始添加数据。
    if not Qdetail.IsEmpty then
      begin
        Qdetail.first;
        while not Qdetail.Eof do
          begin
            col:=0;
            for j:=0 to grmain.Columns.Count-1 do
              if grmain.Columns[j].Visible then
                begin
                  sheet.Cells[k,col+1].Value:=grmain.Columns[j].Field.AsString;
                  inc(col);
                end;
            inc(k);
            QDetail.next;
          end;
      end;

      sheet.Cells[k+gtsum+1,1].Value:='产品明细';
      col:=0;
      for j:=0 to gSJGOrderAluDet.fieldcount-1 do      //生成表头
       if gSJGOrderAluDet.columns[j].Visible then
         begin
           sheet.Cells[k+gtsum+2,col+1].Value:=gSJGOrderAluDet.Columns[j].Title.Caption;
           inc(col);
         end;

     k:=k+gtsum+3;   //从Excel表的第三行开始添加数据。
     if not QSJGOrderAluDet.IsEmpty then
       begin
         QSJGOrderAluDet.first;
         while not QSJGOrderAluDet.Eof do
           begin
             col:=0;
             for j:=0 to gSJGOrderAluDet.Columns.Count-1 do
               if gSJGOrderAluDet.Columns[j].Visible then
                 begin
                   sheet.Cells[k,col+1].Value:=gSJGOrderAluDet.Columns[j].Field.AsString;
                   inc(col);
                 end;
             inc(k);
             QSJGOrderAluDet.next;
           end;
         if gSJGOrderAluDet.FooterRowCount>0 then
         for j:=0 to gSJGOrderAluDet.Columns.Count-1 do
           if gSJGOrderAluDet.Columns[j].Visible then
           begin
               sheet.Cells[k,col+1].Value:=gSJGOrderAluDet.Columns[j].Footer.Value;
           end;
       end;

  exele.visible:=true;
  Screen.Cursor:=crDefault;
end; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值