1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| procedure TForm1.Button1Click(Sender: TObject);
var rangeE: variant;
i,row:integer;
bookmark:Tbookmarkstr;
table:tdataset;
begin
//créer et rendre visible excel
table:=dbgrid1.datasource.dataset;
ExcelApplication1.Visible[0]:=true;
excelapplication1.workbooks.add(NULL,0);
//remplir la première ligne avec les noms des champs
rangeE:=ExcelApplication1.ActiveCell;
for i:=0 to table.Fields.Count-1 do
begin
rangeE.Value:=table.Fields[i].DisplayLabel;
rangeE:=rangeE.Next;
end;
rangeE.AutoFormat(10,NULL,NULL,NULL,NULL,NULL,NULL);
//remplir les lignes suivantes avec les enregts.
table.DisableControls;
try
bookmark:=table.Bookmark;
try
table.First;
row:=2;//table.RecordCount+1;
while not table.Eof do
begin
rangeE:=ExcelApplication1.Range['A'+inttostr(Row), 'A'+inttostr(Row)];
for i:=0 to table.Fields.Count-1 do
begin
rangeE.Value:=table.Fields[i].AsString;
rangeE:=RangeE.Next;
end;
table.Next;
Inc(row);
end;
finally
table.Bookmark:=bookmark;
end;
finally
table.EnableControls;
end;
//formater le tableau pour que cela fasse joli
//rangeE:=ExcelApplication1.Range['A1','E'+inttostr(row-1)];
// rangeE.AutoFormat(3,NULL,NULL,NULL,NULL,NULL,NULL);
end;
end. |
Partager