It is very easy to Automate Excell:
Like this using Variants . It is also possible using Interfaces. Transfering data to excel using same simple loops. See bellow
procedure TTumOgr.SpeedButton1Click(Sender: TObject);
var XApp:Variant;
sheet:Variant;
r,c:Integer;
row,col:Integer;
filName:Integer;
q:Integer;
begin
XApp:=CreateOleObject('Excel.Application');
XApp.Visible:=true;
XApp.WorkBooks.Add(-4167);
XApp.WorkBooks[1].WorkSheets[1].Name:='Sheet1';
sheet:=XApp.WorkBooks[1].WorkSheets['Sheet1'];
for filName:=0 to DMod.OgrSiralaQuery.FieldCount-1 do
begin
q:=filName+1;
sheet.Cells[1,q]:=DMod.OgrSiralaQuery.Fields[filName].FieldName;
end;
for r:=0 to DMod.OgrSiralaQuery.RecordCount-1 do
begin
for c:=0 to DMod.OgrSiralaQuery.FieldCount-1 do
begin
row:=r+2;
col:=c+1;
sheet.Cells[row,col]:=DMod.OgrSiralaQuery.Fields[c].AsString;
end;
DMod.OgrSiralaQuery.Next;
end;
XApp.WorkSheets['Sheet1'].Range['A1:AA1'].Font.Bold:=True;
XApp.WorkSheets['Sheet1'].Range['A1:K1'].Borders.LineStyle :=13;
XApp.WorkSheets['Sheet1'].Range['A2:K'+inttostr(DMod.OgrSiralaQuery.RecordCount-1)].Borders.LineStyle :=1;
XApp.WorkSheets['Sheet1'].Columns[1].ColumnWidth:=16;
XApp.WorkSheets['Sheet1'].Columns[2].ColumnWidth:=7;
XApp.WorkSheets['Sheet1'].Columns[3].ColumnWidth:=19;
XApp.WorkSheets['Sheet1'].Columns[4].ColumnWidth:=9;
XApp.WorkSheets['Sheet1'].Columns[5].ColumnWidth:=9;
XApp.WorkSheets['Sheet1'].Columns[6].ColumnWidth:=9;
XApp.WorkSheets['Sheet1'].Columns[7].ColumnWidth:=46;
XApp.WorkSheets['Sheet1'].Columns[8].ColumnWidth:=9;
XApp.WorkSheets['Sheet1'].Columns[9].ColumnWidth:=7;
XApp.WorkSheets['Sheet1'].Columns[10].ColumnWidth:=6;
XApp.WorkSheets['Sheet1'].Columns[11].ColumnWidth:=13;
end;
I hope it will help you, Do not forget to realise XApp which allocated!