⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oleexcel.pas

📁 Delphi7高级应用开发随书源码
💻 PAS
字号:
unit oleexcel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, DB, DBTables, Grids, DBGrids, Excel2000,
  OleServer;

type
  TForm1 = class(TForm)
    ExcelApplication1: TExcelApplication;
    ExcelWorksheet1: TExcelWorksheet;
    ExcelWorkbook1: TExcelWorkbook;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Table1: TTable;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn4: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(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.BitBtn2Click(Sender: TObject);
begin
        ExcelApplication1.Disconnect;
        ExcelApplication1.Quit;
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
        ExcelWorksheet1.PrintPreview;
end;

procedure TForm1.BitBtn4Click(Sender: TObject);
begin
        ExcelWorksheet1.PrintOut;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -