Главная » Шпаргалка » DELPHI INTERBASE FireBird » Delphi и OLE Automation с Excel

Delphi и OLE Automation с Excel

unit sheet;

interface

uses

EXCEL_TLB, windows, sysutils;

//-------------------------------------------------------------------------

type

tExcel = class
private
xla: _Application;
xlw: _Workbook;
LCID: integer;
procedure fSetVisible(Visible: boolean);
function fGetVisible: boolean;
procedure fSetCell(Cell: string; Value: OLEVariant);
function fGetCell(Cell: string): OleVariant;
public
constructor create;
destructor destroy; override;
procedure AddWorkBook(Template: OleVariant);
procedure SaveAs(filename: string);
property Visible: boolean
read fGetVisible write fSetVisible;
property Cell[Cell: string]: OleVariant
read fGetCell write fSetCell;
end;

//-------------------------------------------------------------------------

procedure CreateSpreadsheet(filename: string);

//-------------------------------------------------------------------------

implementation

//-------------------------------------------------------------------------

constructor tExcel.create;
begin

LCID := GetUserDefaultLCID;
xla := CoApplication.Create;
end;

//-------------------------------------------------------------------------

destructor tExcel.destroy;
begin

xla.Quit;
inherited;
end;

//-------------------------------------------------------------------------

procedure tExcel.AddWorkBook(Template: OleVariant);
begin

xlw := xla.Workbooks.Add(Template, LCID);
end;

//-------------------------------------------------------------------------

procedure tExcel.fSetVisible(Visible: boolean);
begin

xla.visible[lcid] := Visible;
end;

//-------------------------------------------------------------------------

function tExcel.fGetVisible: boolean;
begin

result := xla.visible[lcid];
end;

//-------------------------------------------------------------------------

procedure tExcel.fSetCell(Cell: string; Value: OLEVariant);
begin

xla.Range['A1', 'A1'].Value := value;
end;

//-------------------------------------------------------------------------

function tExcel.fGetCell(Cell: string): OleVariant;
begin

result := xla.Range['A1', 'A1'].Value;
end;

//-------------------------------------------------------------------------

procedure tExcel.SaveAs(filename: string);
begin

xlw.SaveAs(
filename,
xlWorkbookNormal,
'',
'',
False,
False,
xlNoChange,
xlLocalSessionChanges,
true,
0,
0,
LCID);
end;


Нижеприведенный пример использует данный класс для создания электронной таблицы.

 

procedure CreateSpreadsheet(filename: string);
var
xl: tExcel;
begin
xl := tExcel.create;
try
xl.AddWorkBook('c:\graham\excel\sample2\ssddemo.xlt');
xl.visible := true;
xl.cell['a1'] := 'тест';
xl.SaveAs(filename);
finally
xl.free;
end;
Pages:  Prev  1 | 2 | 3 | 4