Delphi開發(fā)OLE自動化控制器操縱Excel

字號:

OLE自動化是Windows應用程序之間互相操縱的一種技巧。被操縱的一方稱為自動化服務器(也稱自動化對象),典型的自動化服務器有Microsoft Word、Excel和PowERPoint。操縱自動化服務器的一方稱為自動化控制器。在開發(fā)數(shù)據(jù)庫應有程序中,經(jīng)常需要借助Microsoft Excel的強大報表功能,把數(shù)據(jù)庫中的數(shù)據(jù)輸出到Excel表格中。Delphi 5.0以前的版本雖然也可以編寫自動化控制器和自動化服務器,但編寫程序較為復雜,不易掌握。Delphi 5.0對于OLE提供了強大的支持,利用Delphi 5.0最新提供的Servers欄控件可以很容易開發(fā)OLE自動化控制器實現(xiàn)對OLE自動化服務器的調(diào)用,發(fā)揮Word、Excel、Powerpoint的強大功能。
    下面給出一利用Delphi 5.0開發(fā)OLE自動化控制器操縱Excel的實例,希望對用Delphi開發(fā)OLE應用程序的編程人員有所幫助。
    首先新建一Application, 然后在Form1上放置
     Servers欄控件ExcelApplication1、 ExcelWorkbook1、
     ExcelWorksheet1, 再放置控件Table1、 Datasource1、
     Dbgrid1、 Button1、 Button2、 Button3、 Button4, 并設置
     Table1.databasename:=dbdemos,
     Table1.TableName:=Country.db,
     Table1.active:=True, Button1.Caption:='
     SaveToExcel',
     Button2.caption:='PrintPreview',
     Button3.caption:='Print',
     Button4.caption:='CloseExcel'。
    代碼如下:
    unit Unit1;
    interface
    uses
    Windows, Messages, SysUtils, Classes,
    Graphics, Controls,
    Forms, Dialogs, Excel97, OleServer, Db, DBTables,
    Grids, DBGrids, StdCtrls;
    type
    TForm1 = class(TForm)
    ExcelApplication1: TExcelApplication;
    ExcelWorkbook1: TExcelWorkbook;
    ExcelWorksheet1: TExcelWorksheet;
    Table1: TTable;
    Table1Name: TStringField;
    Table1Capital: TStringField;
    Table1Continent: TStringField;
    Table1Area: TFloatField;
    Table1Population: TFloatField;
    button1: TButton;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    var
    Form1: TForm1;
    implementation
    {$R *.DFM}
    procedure TForm1.button1Click(Sender: TObject);
    var
    i,row,column:integer;
    begin
    Try
    ExcelApplication1.Connect;
    Except
    MessageDlg('Excel may not be installed',
    mtError, [mbOk], 0);
    Abort;
    End;
    ExcelApplication1.Visible[0]:=True;
    ExcelApplication1.Caption:='Excel Application';
    ExcelApplication1.Workbooks.Add(Null,0);
    ExcelWorkbook1.ConnectTo
    (ExcelApplication1.Workbooks[1]);
    ExcelWorksheet1.ConnectTo
    (ExcelWorkbook1.Worksheets[1] as _Worksheet);
    Table1.Open;
    row:=1;
    While Not(Table1.Eof) do
    begin
    column:=1;
    for i:=1 to Table1.FieldCount do
    begin
    ExcelWorksheet1.Cells.Item[row,column]:
    =Table1.fields[i-1].AsString;
    column:=column+1;
    end;
    Table1.Next;
    row:=row+1;
    end;
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
    ExcelWorksheet1.PrintPreview;
    end;
    procedure TForm1.Button3Click(Sender: TObject);
    begin
    ExcelWorksheet1.PrintOut;
    end;
    procedure TForm1.Button4Click(Sender: TObject);
    begin
    ExcelApplication1.Disconnect;
    ExcelApplication1.Quit;
    end;
    end.
    本程序在Delphi 5.0下調(diào)試通過。