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 72 73 74 75
|
Procedure ExportToExcel(aGrid : TDBGrid);
Var
PreviewToExcel : TExcelApplication;
RangeE : Excel97.Range;
I, J, Col, Row : Integer;
SavePlace : TBookmark;
Begin
PreviewToExcel := TExcelApplication.Create(Application.MainForm);
PreviewToExcel.Connect;
PreviewToExcel.Workbooks.Add(Null, 0);
RangeE := PreviewToExcel.ActiveCell;
Col := 0;
Row := 2;
// En tête tableau excel
For I := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[I].Visible Then
Begin
RangeE.Value := aGrid.Columns[I].FieldName;
RangeE := RangeE.Next;
Inc(Col);
End;
// Si il y a une selection
If aGrid.SelectedRows.Count > 0 Then
Begin
For i := 0 To aGrid.SelectedRows.Count - 1 Do
Begin
RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)];
aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
For j := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[J].Visible Then
Begin
RangeE.Value := aGrid.DataSource.Dataset.Fields[j].AsString;
RangeE := RangeE.Next;
End;
Inc(Row);
End;
end
Else
//Si il n'y a pas de sélection
Begin
SavePlace := aGrid.DataSource.Dataset.GetBookmark;
aGrid.DataSource.Dataset.First;
Try
While Not aGrid.DataSource.Dataset.Eof Do
Begin
RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)];
For I := 0 To aGrid.Columns.Count - 1 Do
If aGrid.Columns[I].Visible Then
Begin
RangeE.Value := aGrid.Fields[I].AsString;
RangeE := RangeE.Next;
End;
Inc(Row);
aGrid.DataSource.Dataset.Next;
End;
aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
Finally
aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
End;
End;
RangeE := PreviewToExcel.Range['A1', Chr(64 + Col) + IntToStr(Row - 1)];
RangeE.AutoFormat(3, Null, Null, Null, Null, Null, Null);
PreviewToExcel.Visible[0] := True;
PreviewToExcel.Disconnect;
End; |
Partager