unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExcelXP, OleServer;
type
TForm1 =
class(TForm)
ExAP1: TExcelApplication;
ExWS1: TExcelWorksheet;
ExWB1: TExcelWorkbook;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const dat = '
C:\Book1.xls';
var OLEArray :
Array of oleVariant;
rueckgabe :
string;
procedure TForm1.Button1Click(Sender: TObject);
var i,z,s,zeilen,spalten :byte;
begin
ExAP1.Workbooks.Open(dat, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,0);
ExWB1.ConnectTo(ExAP1.ActiveWorkbook);
ExWS1.ConnectTo(ExWB1.Sheets.item['
Sheet1']
as _Worksheet);
// Excel.Worksheets['Tabelle1'].Activate; // Test-Bereich anlegen.
for i := 0
to 10
do
begin
ExWS1.cells.Item[i+5, 2]:= '
Zeile'+ inttostr(i);
ExWS1.cells.Item[i+5, 3]:= '
Zeile1'+ inttostr(i);
end;
// Excel.Worksheets['Sheet1'];
ExWS1.Range['
B5:C15',null].
Name := '
Test';
// Bereich Test einrichten
Zeilen:= ExWS1.range['
Test',null].rows.count;
// Anzahl Zeilen im Bereich
Spalten:= ExWS1.range['
Test',null].columns.count;
// Anzahl Spalten im Bereich
OleArray:=VarArrayCreate([Spalten,Zeilen],varVariant);
// dyn. Array
OleArray:= ExWs1.Range['
Test',null].cells;
// Range-Obj nach OleArray
// übertragen
for z := 1
to Zeilen
do // den Bereich anzeigen.
begin
for s := 1
to Spalten
do
begin
Rueckgabe:= OleArray.item[z,s].value;
ShowMessage('
Laß mal sehen: '+ Rueckgabe);
end;
end;
end;
end.