- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, ComObj;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- Button2: TButton;
- Button3: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- private
- { Private declarations }
- ExcelApp: Variant;
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.DFM}
- procedure TForm1.Button1Click(Sender: TObject);
- var
- Path: string;
- begin
- Path := ExtractFilePath(Application.exeName);
- if copy(Path, length(Path) ,1) <> '/' then
- Path := Path + '/';
- ExcelApp := CreateOleObject('Excel.Application');
- ExcelApp.WorkBooks.Open(Path+'Book2.xlt');
- Button1.Enabled := False;
- Button2.Enabled := True;
- Button3.Enabled := True;
- end;
- procedure TForm1.Button2Click(Sender: TObject);
- var
- Path: string;
- begin
- try
- Path := ExtractFilePath(Application.exeName);
- if copy(Path, length(Path) ,1) <> '/' then
- Path := Path + '/';
- ExcelApp.WOrkBooks[1].SaveAs(Path+'test.xls');
- finally
- Button1.Enabled := True;
- Button2.Enabled := False;
- Button3.Enabled := False;
- ExcelApp.WOrkBooks.Close;
- ExcelApp.Quit;
- end;
- end;
- procedure TForm1.Button3Click(Sender: TObject);
- var
- Book: Variant;
- Sheet: Variant;
- AfterSheet: Variant;
- begin
- Book := ExcelApp.WorkBooks[1];
- AfterSheet := Book.WorkSheets[3];
- Sheet := Book.WorkSheets.Add(EmptyParam, AfterSheet, 1);
- //Sheet.move(Book.WorkSheets[4],
- //Sheet := Book.WorkSheets[1];
- Sheet.Activate;
- ExcelApp.Cells[3,1] := 'TEST';
- end;
- end.
DELPHI中控制EXCEL的一个例子
最新推荐文章于 2024-04-18 08:42:51 发布