導出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; 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章