当前位置:软件学习 > Excel >>

Delphi开发OLE自动化控制器操纵Excel

OLE自动化是Windows应用程序之间互相操纵的一种技巧。被操纵的一方称为自动化服务器(也称自动化对象),典型的自动化服务器有Microsoft Word、Excel和Powerpoint。操纵自动化服务器的一方称为自动化控制器。在开发数据库应有程序中,经常需要借助Microsoft Excel的强大报表功能,把数据库中的数据输出到Excel表格中。Delphi 5.0以前的版本虽然也可以编写自动化控制器和自动化服务器,但编写程序较为复杂,不易掌握。Delphi 5.0对于OLE提供了强大的支持,利用Delphi 5.0最新提供的Servers栏控件可以很容易开发OLE自动化控制器实现对OLE自动化服务器的调用,发挥Word、Excel、Powerpoint的强大功能。

下面给出一利用Delphi 5.0开发OLE自动化控制器操纵Excel的实例,希望对用Delphi开发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下调试通过。

让DELPHI与OFFICE联姻

由于微软的Office系列的完善的功能;与Windows和IE的紧密集成以及强大的扩展能力,它实际上已经成为事实上的Windows下办公软件的标准,我们知道在VB中可以建立各种Office对象(Word、Excel)并控制这些对象编辑、打印、保存文档以及控制执行Office中的很多操作。象这样在VB中建立并控制Office对象是十分有用的,例如你可以将一个或者一批数据库自动输入到Word或者Excel中并保存,再通过Outlook将文档分发给其它同仁。

过去,这只有通过VB才能实现的,但是现在Delphi5也提供了这样强大的对象组。利用Delphi也可以利用Office资源了。

打开Delphi,滚动Compent Palette到Servers页,就可以看到很多熟悉的控件图标,这些就是Office组件的控件。Delphi 5中对应Office的组件包括了Word、Excel、Access、PowerPoint和Outlook可以说是十分的全面。不过要使用这些组件首先要保证你的系统中安装了Office 97或以上的 版本。

下面首先来说以下Office组件的基本使用方法Delphi中对于Office中不同的组件,首先要建立一个Application对象,例如要控制Word,首先要建立TWordApplication对象,然后再将诸如TWordDocument等Word对象通过ConnectTo方法连接到TWordApplication对象上。对于其它的Excel、Outlook等也是如此。

下面我通过具体的范例来说明对Delphi中Office对象的控制,即如何将文字输出到Word中进行简单的排版并保存和打印输出。

首先建立一个新的工程,在Form1中加入三个TButton对象、一个TMemo对象、一个TWordApplication对象、一个TWordDocument对象、一个TWordFont对象。下面是Form1的代码:

  unit Unit1;
              interface
              uses
              Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
              StdCtrls, ExtCtrls, Word97, OleServer,Clipbrd;
              type
              TForm1 = class(TForm)
              Memo1: TMemo;
              Button1: TButton;
              WordApplication1: TWordApplication;
              WordDocument1: TWordDocument;
              WordFont1: TWordFont;
              Button2: TButton;
              Button3: TButton;
              procedure FormCreate(Sender: TObject);
              procedure Button1Click(Sender: TObject);
              procedure Button2Click(Sender: TObject);
              procedure Button3Click(Sender: TObject);
              private
              { Private declarations }
              public
              { Public declarations }
              end;
              var
              Form1: TForm1;
              implementation
              {$R *.DFM}						
		
	
补充:软件开发 , Delphi ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,