打开Excel97。
Try
        ExcelApplication1.Connect;
     Except
     End;
     ExcelApplication1.Visible[0]:=True;
增加一个Workbook。
ExcelWorkbook1.ConnectTo(ExcelApplication1.
Workbooks.Add(EmptyParam,0));添加一个Worksheet。
var
Temp_Worksheet: _WorkSheet;
begin
Try
Temp_Worksheet:=ExcelWorkbook1.
WorkSheets.Add(EmptyParam,
EmptyParam,EmptyParam,EmptyParam,0) 
as _WorkSheet;//(注意)
ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);
Except
ShowMessage('Failure');
             End;
        end;
关闭Excel.
Try
         ExcelApplication1.Quit;
ExcelWorksheet1.Disconnect;
         ExcelWorkbook1.Disconnect;
         ExcelApplication1.Disconnect;
      Except
      End;---- 4. 对Excel的一些操作: 
选择当前Workbook的某一Worksheet.
procedure TForm1.ComboBox1DropDown
(Sender: TObject);
var
   i: Integer;
begin
     ComboBox1.Clear;
     For i:=1 to ExcelWorkbook1.
Worksheets.Count do
         ComboBox1.Items.Add
((ExcelWorkbook1.Worksheets.Item[i] 
as _WorkSheet).Name);
end;procedure TForm1.ComboBox1Change
(Sender: TObject);
begin
     ExcelWorkSheet1.ConnectTo
(ExcelWorkbook1.Worksheets.Item
 [ComboBox1.ItemIndex+1] as _WorkSheet);
     ExcelWorkSheet1.Activate;
end;选择某一Workbook:
procedure TForm1.ComboBox2DropDown
(Sender: TObject);
var
   i: Integer;
begin
ComboBox2.Clear;
if ExcelApplication1.Workbooks.Count >0 then
For i:=1 to ExcelApplication1.Workbooks.Count do
Combobox2.Items.Add(ExcelApplication1.
Workbooks.Item[i].Name);
end;procedure TForm1.ComboBox2Change(Sender: TObject);
begin
ExcelWorkSheet1.Disconnect;
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks.
Item[Combobox2.ItemIndex+1]);
     ExcelWorkBook1.Activate;
     ExcelWorksheet1.ConnectTo(ExcelWorkBook1.
ActiveSheet as _WorkSheet);
     ExcelWorkSheet1.Activate;
end;对某一单元格进行赋值及取值。
procedure TForm1.Button5Click(Sender: TObject);
begin
     ExcelWorksheet1.Cells.Item[SpinEdit2.Value,
SpinEdit1.Value]:=Edit1.Text;
end;procedure TForm1.Button6Click(Sender: TObject);
begin
     Edit1.Text:=ExcelWorksheet1.Cells.Item[
SpinEdit2.Value,SpinEdit1.Value];
end;选择某一区域
ExcelWorkSheet1.Range['A1','C1'].Select;打开一个Excel文件。
     if OpenDialog1.Execute then
        Begin
             Try
ExcelWorkBook1.ConnectTo
(ExcelApplication1.Workbooks.Open
     (OpenDialog1.FileName,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,
     EmptyParam,EmptyParam,
EmptyParam,EmptyParam,0));
     ExcelWorkSheet1.ConnectTo
(ExcelWorkBook1.Activesheet
       as _Worksheet);
             Except;
             End;
        End;

解决方案 »

  1.   

    可以参考一下。function ExportCollectInfoToExcel(AQry: TADOQuery; AExcelFile: string) :integer ;
    var
      EclApp : Variant;
      iSheetID :integer ;
    Begin
      Result := -1 ;
      iSheetID := 1 ;
      try
        EclApp := CreateOleObject('Excel.Application');
      except
        Exit;
      end;  try
        try
          EclApp.WorkBooks.Add ;      //全部信息
          EclApp.WorkSheets[iSheetID].Activate;
          EclApp.WorkSheets[iSheetID].Name := '总表' ;
          EclApp.Cells.Font.Name := 'Arial' ;
          EclApp.Cells.Font.Color := clBlack ;
          EclApp.Cells.Font.Size := 9 ;
          EclApp.Cells.Font.Bold := false ;
          EclApp.Cells.Font.UnderLine := false ;
          EclApp.Visible := false ;      CollectInfoDataSetToSheet(EclApp.Activesheet, AQry) ;
          Inc(iSheetID) ;      //正常
          AQry.Filtered := false ;
          AQry.Filter := '[State] = '''' and [Reason] <> ''NC'' and [Reason] <> ''NV'' ' ;
          AQry.Filtered := true ;
          if not AQry.IsEmpty then
          begin
            EclApp.WorkSheets[iSheetID].Activate;
            EclApp.WorkSheets[iSheetID].Name := '正常' ;
            EclApp.Cells.Font.Name := 'Arial' ;
            EclApp.Cells.Font.Color := clBlack ;
            EclApp.Cells.Font.Size := 9 ;
            EclApp.Cells.Font.Bold := false ;
            EclApp.Cells.Font.UnderLine := false ;
            EclApp.Visible := false ;        CollectInfoDataSetToSheet(EclApp.Activesheet, AQry) ;
            Inc(iSheetID) ;
          end;      //加急费
          AQry.Filtered := false ;
          AQry.Filter := '[State] = ''E'' and [Reason] <> ''NC'' and [Reason] <> ''NV'' ' ;
          AQry.Filtered := true ;
          if not AQry.IsEmpty then
          begin
            EclApp.WorkSheets[iSheetID].Activate;
            EclApp.WorkSheets[iSheetID].Name := '加急费' ;
            EclApp.Cells.Font.Name := 'Arial' ;
            EclApp.Cells.Font.Color := clBlack ;
            EclApp.Cells.Font.Size := 9 ;
            EclApp.Cells.Font.Bold := false ;
            EclApp.Cells.Font.UnderLine := false ;
            EclApp.Visible := false ;        CollectInfoDataSetToSheet(EclApp.Activesheet, AQry) ;
            Inc(iSheetID) ;
          end;      AQry.Filtered := false ;      EclApp.ActiveWorkBook.Saved:=True;
          EclApp.ActiveWorkBook.SaveAs(AExcelFile);
          Result := 1 ;
        except
          Result := 0 ;
        end;
      finally
        EclApp.ActiveWorkBook.Saved:=True;
        EclApp.ActiveWorkBook.Close;
        eclApp.Quit; {释放VARIANT变量}
        eclApp:=Unassigned;
      end;
    end;
    procedure CollectInfoDataSetToSheet(ASheet :Variant; AQry :TADOQuery) ;
    var
      Row : Integer ;
      sStr :string ;
    begin
      ASheet.Rows.RowHeight := 15;
      ASheet.Columns.ColumnWidth := 11 ;  ASheet.Rows[1].Font.Name := 'Arial';
      ASheet.Rows[1].Font.Color := clBlack;
      ASheet.Rows[1].Font.Size := 9 ;
      ASheet.Rows[1].Font.Bold := True;
      ASheet.Rows[1].Font.UnderLine := false;  ASheet.Range['A1:AJ1'].Columns.Interior.Color := clYellow;  ASheet.Cells(1, 1) := 'Order No' ;
      ASheet.Cells(1, 2) := 'AWB' ;
      ASheet.Cells(1, 3) := 'Dst' ;  Row := 2 ;
      with AQry do
      begin
        First ;
        while not Eof do
        begin
          Application.ProcessMessages ;      if frmWaiting.pb.Progress mod 500 = 0 then
          begin
            RefreshQuery(AQry, true) ;
          end ;      ASheet.Cells(Row, 1) := FieldByName('ORDER_NO').asstring ;
          ASheet.Cells(Row, 2) := FieldByName('AWB').asstring ;
          ASheet.Cells(Row, 3) := FieldByName('DST').asstring ;      Next ;
          Inc(Row) ;
        end ;
      end;
      sStr := 'A1:AJ' + IntToStr(Row - 1) ;
      ASheet.Range[sStr].Borders.Color := clBlack ;  sStr := 'A2:A' + IntToStr(Row - 1) ;
      ASheet.Range[sStr].Columns.Interior.Color := $0095E916;
      
    end ;