IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Contribuez Delphi Discussion :

export dbgrid (Excel, CSV, Clipboard)


Sujet :

Contribuez Delphi

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Décembre 2006
    Messages
    63
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2006
    Messages : 63
    Points : 40
    Points
    40
    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 : Sélectionner tout - Visualiser dans une fenêtre à part
    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;

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    279
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Septembre 2006
    Messages : 279
    Points : 99
    Points
    99
    Par défaut
    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

  3. #3
    Rédacteur
    Avatar de Pedro
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    5 411
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 5 411
    Points : 8 078
    Points
    8 078
    Par défaut
    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
    Aéroclub Bastia Saint-Exupéry

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 12
    Points : 7
    Points
    7
    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

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2002
    Messages : 12
    Points : 7
    Points
    7
    Par défaut
    C'est bon j'ai trouvé
    Merci

Discussions similaires

  1. [CSV] Format des données exportées dans Excel
    Par magsmile dans le forum Langage
    Réponses: 6
    Dernier message: 07/09/2007, 17h46
  2. Exporter fichier Excel en .csv
    Par pierre.coudert dans le forum Windows
    Réponses: 7
    Dernier message: 27/02/2007, 13h45
  3. [VBA Excel] export/sauvegarde d'une feuille excel .csv
    Par Paloma dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 05/12/2006, 12h07
  4. [CSV] Export de donnees vers excel (.csv)
    Par vivian dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 10/07/2006, 14h22
  5. Exporter des données d'un requete SQL vers excel (csv)
    Par PrinceMaster77 dans le forum ASP
    Réponses: 9
    Dernier message: 08/10/2005, 22h28

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo