Précédent   Forum du club des développeurs et IT Pro > Environnements de développement > Delphi > Contribuez
Contribuez Proposez vos articles, cours, tutoriels, FAQ, sources et autres ressources pour la rubrique Delphi.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 26/12/2008, 02h32   #1
The Jos
Candidat au titre de Membre du Club
 
Inscription : décembre 2006
Messages : 63
Détails du profil
Informations forums :
Inscription : décembre 2006
Messages : 63
Points : 10
Points : 10
Par défaut export dbgrid (Excel, CSV, Clipboard)

Bien,

après avoir allègrement poster sur ce forum, il est temps pour moi de rendre la pièce

Voila, si cela peut interesser quelqu'un, 3 petites routines pour exporter un dbgrid.

1 Le dbgrid peut etre filtré l'export se fait sur le résultat du filtre
2 les routine teint compte de la selection s'il en existe une
3 elles tiennent compte du déplacement des colonnes
4 elles ne prennent en considération que les colonnes visibles.

PS1: Pour l'export vers le presse papier il se fait au format collable sous excel. Pour l'export vers excel j'ai appliqué un format de tableau automatique.

PS2 : l'amorce du code est piqué ici http://www.fobec.com/protec/trucs2/e...=479&rub=CBase, mais bon les routine ont complètement été revisitées.

PS3 : soyez indulgent les db c pas ma spécialité

Code :
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
 
uses dbgrids, db, Excel97, clipbrd;
 
Procedure ExportToClip(aGrid : TDBGrid);
Var
  I, J : Integer;
  SavePlace : TBookmark;
  Table : TStrings;
  ClipB : TClipboard;
  HeadTable : String;
  LineTable : String;
  First : Boolean;
Begin
 
  HeadTable := '';
  LineTable := '';
  Table := TStringList.Create;
  ClipB := TClipboard.Create;
  First := True;
  Try
    // En tête tableau
    For I := 0 To aGrid.Columns.Count - 1 Do
      If aGrid.Columns[I].Visible Then
        If First Then
        Begin
          HeadTable := HeadTable + aGrid.Columns[I].FieldName;
          First := False;
        End
        Else
          HeadTable := HeadTable + #09 + aGrid.Columns[I].FieldName;
 
    Table.Add(HeadTable);
    First := True;
 
    // Si il y a une selection
    If aGrid.SelectedRows.Count > 0 Then
    Begin
      For i := 0 To aGrid.SelectedRows.Count - 1 Do
      Begin
        aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
        For j := 0 To aGrid.Columns.Count - 1 Do
          If aGrid.Columns[J].Visible Then
            If First Then
            Begin
              lineTable := lineTable + aGrid.Fields[J].AsString;
              First := False;
            End
            Else
              lineTable := lineTable + #09 + aGrid.Fields[J].AsString;
        Table.Add(LineTable);
        LineTable := '';
        First := True;
      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
          For I := 0 To aGrid.Columns.Count - 1 Do
            If aGrid.Columns[I].Visible Then
              If First Then
              Begin
                lineTable := lineTable + aGrid.Fields[I].AsString;
                First := False;
              End
              Else
                lineTable := lineTable + #09 + aGrid.Fields[I].AsString;
 
          Table.Add(LineTable);
          LineTable := '';
          aGrid.DataSource.Dataset.Next;
          First := True;
        End;
 
        aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
 
        //Table.SaveToFile(FileName);
      Finally
        aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
      End;
    End;
    ClipB.Open;
    ClipB.SetTextBuf(Table.GetText);
    ClipB.Close;
  Finally
    ClipB.Free;
    Table.Free;
  End;
End;
 
//------------------------------------------------------------------------------
 
Procedure ExportToCSV(aGrid : TDBGrid;FileName : String);
Var
  I, J : Integer;
  SavePlace : TBookmark;
  Table : TStrings;
  HeadTable : String;
  LineTable : String;
  First : Boolean;
Begin
 
  HeadTable := '';
  LineTable := '';
  Table := TStringList.Create;
  First := True;
  Try
    // En tête tableau
    For I := 0 To aGrid.Columns.Count - 1 Do
      If aGrid.Columns[I].Visible Then
        If First Then
        Begin
          HeadTable := HeadTable + aGrid.Columns[I].FieldName;
          First := False;
        End
        Else
          HeadTable := HeadTable + ';' + aGrid.Columns[I].FieldName;
    Table.Add(HeadTable);
    First := True;
 
    // Si il y a une selection
    If aGrid.SelectedRows.Count > 0 Then
    Begin
      For i := 0 To aGrid.SelectedRows.Count - 1 Do
      Begin
        aGrid.DataSource.Dataset.GotoBookmark(pointer(aGrid.SelectedRows.Items[i]));
        For j := 0 To aGrid.Columns.Count - 1 Do
          If aGrid.Columns[J].Visible Then
            If First Then
            Begin
              lineTable := lineTable + aGrid.Fields[J].AsString;
              First := False;
            End
            Else
              lineTable := lineTable + ';' + aGrid.Fields[J].AsString;
        Table.Add(LineTable);
        LineTable := '';
        First := True;
      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
          For I := 0 To aGrid.Columns.Count - 1 Do
            If aGrid.Columns[I].Visible Then
              If First Then
              Begin
                lineTable := lineTable + aGrid.Fields[I].AsString;
                First := False;
              End
              Else
                lineTable := lineTable + ';' + aGrid.Fields[I].AsString;
          Table.Add(LineTable);
          LineTable := '';
          aGrid.DataSource.Dataset.Next;
          First := True;
        End;
 
        aGrid.DataSource.Dataset.GotoBookmark(SavePlace);
      Finally
        aGrid.DataSource.Dataset.FreeBookmark(SavePlace);
      End;
    End;
    Table.SaveToFile(FileName);
  Finally
    Table.Free;
  End;
End;
 
//------------------------------------------------------------------------------
 
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;
The Jos est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/03/2009, 16h19   #2
zanoubya
Membre du Club
 
Inscription : septembre 2006
Messages : 225
Détails du profil
Informations personnelles :
Localisation : Maroc

Informations forums :
Inscription : septembre 2006
Messages : 225
Points : 59
Points : 59
C'est bon j'ai pu détecter l'erreur et ca marche très bien avec l'exemple que j'ai déjà donné
Merci
zanoubya est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/03/2009, 17h36   #3
Pedro
Rédacteur
 
Avatar de Pedro
 
Inscription : octobre 2003
Messages : 5 406
Détails du profil
Informations forums :
Inscription : octobre 2003
Messages : 5 406
Points : 6 771
Points : 6 771
Merci pour ta participation
__________________
Pedro
Aucune réponse aux sollicitations techniques par MP

Faut pas attendre d'en avoir besoin pour s'en servir... (Lucien Stéphane)

Les pages Source C'est bon. Mangez-en!
Le défi Delphi
Règles du forum - FAQ Delphi - Pensez au chtit

Le site de mon frangin
Pedro est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2011, 11h05   #4
guy jeuniaux
Invité de passage
 
Inscription : novembre 2002
Messages : 2
Détails du profil
Informations forums :
Inscription : novembre 2002
Messages : 2
Points : 2
Points : 2
Par défaut Un renseignement à propos de ces codes

Bonjour,
Cet export fonctionne parfaitement.
Par contre est ce qu'il est possible d'indiquer un nom de classeur par défaut de façon à éviter d'utiliser "Enregister sous" sur excel

Merci
guy jeuniaux est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2011, 11h53   #5
guy jeuniaux
Invité de passage
 
Inscription : novembre 2002
Messages : 2
Détails du profil
Informations forums :
Inscription : novembre 2002
Messages : 2
Points : 2
Points : 2
C'est bon j'ai trouvé
Merci
guy jeuniaux est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 05h29.


 
 
 
 
Partenaires

Hébergement Web