uses
ComObj, ActiveX;
procedure TFormOff.BtnExcelClick(Sender: TObject);
var
RangeE: Excel97.Range;
I, Row: Integer;
Bookmark: TBookmarkStr;
begin
// create and show
ExcelApplication1.Visible [0] := True;
ExcelApplication1.Workbooks.Add (NULL, 0);
// fill is the first row with field titles
RangeE := ExcelApplication1.ActiveCell;
for I := 0 to Table1.Fields.Count - 1 do
begin
RangeE.Value := Table1.Fields [I].DisplayLabel;
RangeE := RangeE.Next;
end;
// add field data in following rows
Table1.DisableControls;
try
Bookmark := Table1.Bookmark;
try
Table1.First;
Row := 2;
while not Table1.EOF do
begin
RangeE := ExcelApplication1.Range ['A' + IntToStr (Row),
'A' + IntToStr (Row)];
for I := 0 to Table1.Fields.Count - 1 do
begin
RangeE.Value := Table1.Fields [I].AsString;
RangeE := RangeE.Next;
end;
Table1.Next;
Inc (Row);
end;
finally
Table1.Bookmark := Bookmark;
end;
finally
Table1.EnableControls;
end;