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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
var
ExcelApplication1: TExcelApplication;
MonClasseur : _workbook;
MaFeuille :_Worksheet;
i,j,LCID:integer;
ligne:AnsiString;//contiendra toutes les données
destination,liens:Olevariant;//pour la methode 'paste'
begin
try
Screen.Cursor :=crHourGlass;
lcid := GetUserDefaultLCID;
ExcelApplication1 := TExcelApplication.create(self);
ExcelApplication1.Connect;
MonClasseur := ExcelApplication1.Workbooks.Open(fichierIni.fichierMemIni.ReadString(nomSection,'DEST',''),False,False,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,0);
ExcelApplication1.visible[LCID]:=false;
MaFeuille:=MonClasseur.Worksheets[1] as _worksheet;
dbResultat.DataSource.DataSet.DisableControls;
dbResultat.DataSource.DataSet.First;
//copie des entetes
for i := 0 to dbResultat.FieldCount - 1 do
begin
if dbResultat.Fields[i].Visible then
begin
ligne := ligne + dbResultat.Fields[i].DisplayLabel;
if i <> dbResultat.FieldCount - 1 then
begin
ligne := ligne + #9;
end;
end;
end;
ligne := ligne + #13;
j:=0;
//copie du contenu
while not dbResultat.DataSource.DataSet.eof do
begin
inc(j);
for i:=0 to dbResultat.FieldCount-1 do
begin
ligne:= ligne + dbResultat.Fields[i].Text;
if i <> dbResultat.FieldCount - 1 then
begin
ligne := ligne+ #9;//tab
end;
end;
ligne := ligne+ #13;//retour chariot
dbResultat.DataSource.DataSet.next;//ligne suivante
end;
Clipboard.SetTextBuf(PChar(ligne));//copie ds la cache
MaFeuille.Cells.Item[1, 1].Select;
//pour la methode paste
//mesLettres est une TstringList qui contient les lettres de l'alphabet
destination:= MaFeuille.Range['A1',mesLettres[dbResultat.FieldCount-1] + inttostr(j)];
liens :=false;
MaFeuille.Paste(destination,liens,LCID);
//on vide le cache
Clipboard.Clear;
dbResultat.DataSource.DataSet.EnableControls;
dbResultat.DataSource.DataSet.First;
MonClasseur.Close(True, emptyparam,emptyparam,0);
finally
ExcelApplication1.Disconnect;
Screen.Cursor :=crDefault;
end;
ShowMessage('Exportation terminé');
end; |
Partager