unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComObj;
type
TForm1 =
class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
FExcelApp:Variant;
function GetApplication:Variant;
function GetSelection: Variant;
function GetActiveSheet: Variant;
public
property XlsApp:Variant
read GetApplication;
property ActiveSheet:Variant
read GetActiveSheet;
property Selection:Variant
read GetSelection;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function TForm1.GetApplication:Variant;
begin
if VarType(FExcelApp)=varEmpty
then
try
FExcelApp:=GetActiveOleObject('
Excel.Application');
FExcelApp.Visible:=True;
except
FExcelApp:= CreateOleObject('
Excel.Application');
FExcelApp.Visible:=True;
end;
Result:=FExcelApp;
end;
function TForm1.GetSelection: Variant;
begin
Result:=XlsApp.Selection;
end;
function TForm1.GetActiveSheet: Variant;
begin
Result:=XlsApp.ActiveSheet;
end;
procedure TForm1.Button1Click(Sender: TObject);
var Sel:Variant;
r,c,r1,c1,r2,c2:Integer;
s:
String;
begin
Sel:=Selection;
r1:=Sel.Row;
c1:=Sel.Column;
r2:=Sel.Rows.Count;
c2:=Sel.Columns.Count;
for r:=r1
to r1+r2-1
do
for c:=c1
to c1+c2-1
do
begin
s:=ActiveSheet.Cells[r,c].Value;
Memo1.Lines.Add('
Cell('+IntToStr(r)+'
,'+IntToStr(c)+'
).Value='+s);
end;
end;
end.