Précédent   Forum des professionnels en informatique > Logiciels > Solutions d'entreprise > ERP > SAP
SAP Forum d'entraide sur SAP et sur la programmation avec le langage ABAP
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 04/07/2007, 09h52   #1
Rédactrice
 
Avatar de Fleur-Anne.Blain
 
Inscription : juillet 2006
Messages : 2 662
Détails du profil
Informations forums :
Inscription : juillet 2006
Messages : 2 662
Points : 5 590
Points : 5 590
Par défaut [Source ABAP] Export de tables en "dynamique"

Bonjour,

Ceci n'est pas une question, je partage juste un JOB ABAP que voici dont le but est d'exporter des tables SAP quelque soit sa structure (limitée à 100 colonnes) en fichier plat.

Les tables à exporter en fichiers plats sont passées en paramètre par une variante.

En espérant que cela soit utile

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
REPORT  nomreport   .
TABLES: table.
* data declaration
DATA : s_table TYPE REF TO DATA,
       s_table_line TYPE REF TO DATA,
       filename1 type string,
       filename2 type string,
       s_file LIKE rlgrap-filename,
       t_file LIKE rlgrap-filename.
data : t_filename type table of string.
constants: c_directory(20) value 'le chemin destination'.
FIELD-SYMBOLS : <fs> TYPE standard table.
field-symbols : <fs_line> type any.
field-symbols : <fs_field> type any.
data: v_line(500).

select-options : table_select for table-TABNAME.
*             p_dfile LIKE rlgrap-filename .

start-of-selection.

  loop at table_select.

* Create dynamic structure
    CREATE DATA s_table TYPE table of (table_select-low).
    CREATE DATA s_table_line type (table_select-low).

    ASSIGN s_table->* TO  <fs>.
    assign s_table_line->* to <fs_line>.

* Select data into internal table
    SELECT * FROM (table_select-low) INTO table <fs>.


* export into excel file
    move c_directory to  filename1.
    concatenate filename1 table_select-low '.xls' into filename2.

    OPEN DATASET filename2 FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

    loop at <fs> into <fs_line>.

      clear: v_line.
      do 100 times.
        ASSIGN COMPONENT SY-INDEX OF
                   STRUCTURE <fs_line> TO <FS_field>.
        if sy-subrc = 0.
          concatenate v_line  '|' <FS_field> into v_line.
        else.
          exit.
        endif.
      enddo.
      transfer v_line to filename2.
    endloop.

    Close DATASET filename2.
  endloop.
__________________
la culture c'est comme la confiture moins on en a plus on l'étale.

Vous souhaitez contribuer aux rubriques Solutions d'entreprises ou BI, contactez-moi

Mes tutos
Fleur-Anne.Blain est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 04/07/2007, 10h56   #2
Membre habitué
 
Inscription : juin 2003
Messages : 146
Détails du profil
Informations personnelles :
Âge : 31
Localisation : France, Vienne (Poitou Charente)

Informations forums :
Inscription : juin 2003
Messages : 146
Points : 135
Points : 135
Envoyer un message via MSN à Sh@m@n
Merci à toi
Sh@m@n est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 04/07/2007, 20h01   #3
Membre à l'essai
 
Inscription : août 2006
Messages : 40
Détails du profil
Informations forums :
Inscription : août 2006
Messages : 40
Points : 24
Points : 24
Eh ! Merci bien pour ce petit utilitaire
Frooty est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/03/2009, 23h29   #4
Invité régulier
 
Inscription : novembre 2003
Messages : 149
Détails du profil
Informations forums :
Inscription : novembre 2003
Messages : 149
Points : 9
Points : 9
Par défaut tables dynamiques

Bonjour,
quelqu'un peut m'expliquer ce code

Merci

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

REPORT  nomreport   .
TABLES: table.
* data declaration
DATA : s_table TYPE REF TO DATA,
       s_table_line TYPE REF TO DATA,
       filename1 type string,
       filename2 type string,
       s_file LIKE rlgrap-filename,
       t_file LIKE rlgrap-filename.
data : t_filename type table of string.
constants: c_directory(20) value 'le chemin destination'.
FIELD-SYMBOLS : <fs> TYPE standard table.
field-symbols : <fs_line> type any.
field-symbols : <fs_field> type any.
data: v_line(500).

select-options : table_select for table-TABNAME.
*             p_dfile LIKE rlgrap-filename .

start-of-selection.

  loop at table_select.

* Create dynamic structure
    CREATE DATA s_table TYPE table of (table_select-low).
    CREATE DATA s_table_line type (table_select-low).

    ASSIGN s_table->* TO  <fs>.
    assign s_table_line->* to <fs_line>.

* Select data into internal table
    SELECT * FROM (table_select-low) INTO table <fs>.


* export into excel file
    move c_directory to  filename1.
    concatenate filename1 table_select-low '.xls' into filename2.

    OPEN DATASET filename2 FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

    loop at <fs> into <fs_line>.

      clear: v_line.
      do 100 times.
        ASSIGN COMPONENT SY-INDEX OF
                   STRUCTURE <fs_line> TO <FS_field>.
        if sy-subrc = 0.
          concatenate v_line  '|' <FS_field> into v_line.
        else.
          exit.
        endif.
      enddo.
      transfer v_line to filename2.
    endloop.

    Close DATASET filename2.
  endloop.
melmouj est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2009, 14h16   #5
Invité régulier
 
Inscription : avril 2009
Messages : 22
Détails du profil
Informations forums :
Inscription : avril 2009
Messages : 22
Points : 8
Points : 8
Bonjour,

Sous quelle version marche ce code ?

Je suis en 4.6, et cela ne fonctionne pas.

Merci
Drben est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2009, 15h20   #6
Rédacteur/Modérateur
 
Avatar de cladsam
 
Morgan Bourgeois
Inscription : août 2003
Messages : 1 730
Détails du profil
Informations personnelles :
Nom : Morgan Bourgeois
Âge : 32
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations forums :
Inscription : août 2003
Messages : 1 730
Points : 1 862
Points : 1 862
Citation:
Envoyé par Drben Voir le message
Bonjour,

Sous quelle version marche ce code ?

Je suis en 4.6, et cela ne fonctionne pas.

Merci
Bonjour,

pourrais-tu être plus précise dans "Ca ne marche pas".
Ça ne compile pas, ça compile mais tuas un problème à l'exécution ? Quel message d'erreur rencontres-tu etc. ?
__________________
----------------------------------------------------
Consultant technico-fonctionnel SAP logistique -
Mon site sur developpez
---------------------------------------------------
Anakin Skywalker turn to the Dark Side after his failed attempt to upgrade R/2-D2 to R/3-D2.
cladsam est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 27/04/2009, 15h34   #7
Invité régulier
 
Inscription : avril 2009
Messages : 22
Détails du profil
Informations forums :
Inscription : avril 2009
Messages : 22
Points : 8
Points : 8
Bonjour,

Voici les éléments que j'ai modifié afin d'essayer de faire fonctionner le programme.

TABLES: table. --> TABLES: dd03l. Pour récupérer une liste de tablename

SELECT-OPTIONS : table_select <-- Can not be up to 8 characters
Donc renommé en 'table_se'

J'ai remplacé 'le chemin de destination' par le chemin que je souhaite.

Au final, j'ai l'érreur suivante sur la ligne :
create data s_table type table of (table_se-low).

The type specification of 'TABLE' is incomplete.

J'ai essayer de corriger, mais sans succès.
Drben est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/04/2009, 20h12   #8
Invité de passage
 
Inscription : mars 2004
Messages : 9
Détails du profil
Informations forums :
Inscription : mars 2004
Messages : 9
Points : 4
Points : 4
j'essayerais "type standard table", qui est la forme explicite de "type table" :

create data s_table type STANDARD table of (table_se-low).
Benedetto est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 28/04/2009, 21h04   #9
Membre expérimenté
 
Avatar de Celdrøn
 
Homme Celdrøn Valdersen
Consultant SAP
Inscription : juillet 2007
Messages : 438
Détails du profil
Informations personnelles :
Nom : Homme Celdrøn Valdersen
Âge : 26
Localisation : France

Informations professionnelles :
Activité : Consultant SAP

Informations forums :
Inscription : juillet 2007
Messages : 438
Points : 579
Points : 579
Envoyer un message via MSN à Celdrøn
Salut,

Pour moi, il faut faire un traitement par table dynamique et donc ça passe par l'utilisation de field-symbol.

Pour moi :
Data : t_table TYPE STANDARD TABLE OF (table_se-low).

Ne fonctionnera pas.

J'ai un exemple de code je crois, je regarderai du bureau demain et je vous tiens au courant.

@++.
__________________
Boaf...signature <= ça suffira ça ??
Celdrøn est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 05/05/2009, 10h34   #10
Invité régulier
 
Inscription : avril 2009
Messages : 22
Détails du profil
Informations forums :
Inscription : avril 2009
Messages : 22
Points : 8
Points : 8
Bonjour,

J'ai toujours le probleme avec le code.

J'ai retourné le probleme dans tous les sens, aucun succès.

Si quelqu'un a pu faire marcher ce code, j'ai besoin d'un petit coup de main
Drben est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 08/02/2010, 15h45   #11
Invité de passage
 
Inscription : février 2010
Messages : 1
Détails du profil
Informations forums :
Inscription : février 2010
Messages : 1
Points : 3
Points : 3
Par défaut Export du contenu d'une table dans fichier plat

Chez moi ça marche.

Le code ci-dessous permet en + :
- de préciser les conditions de sélection de la clause "where" du SELECT
- de sélectionner les colonnes à exporter
- de diriger l'export vers le PC ou le serveur au choix.

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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
*&---------------------------------------------------------------------*
*& Report  Z_EXPORT_TABLE                                            *
*&---------------------------------------------------------------------*
*& Exporting a transparent table content into a flat file              *
*&---------------------------------------------------------------------*

REPORT  z_export_table.
TABLES: dd02l
       ,dd03l
       .
* data declaration
DATA : gs_table      TYPE REF TO data
      ,gs_table_line TYPE REF TO data
      ,gs_filename     TYPE string
      ,gs_filenamec(128) TYPE c
      ,gv_nb_col    TYPE dd03l-position
      .
DATA:  gt_where(100) OCCURS 0 WITH HEADER LINE
      .
DATA:
      go_descr TYPE REF TO cl_abap_structdescr
     ,gs_comp  TYPE abap_compdescr
     ,gt_tabfield TYPE TABLE OF string
     ,gs_tabfield TYPE string
     .
FIELD-SYMBOLS : <fs_table_line> TYPE ANY
               ,<fs> TYPE STANDARD TABLE
               ,<fs_line> TYPE ANY
               ,<fs_field> TYPE ANY
               .
DATA: gs_line         TYPE string
     ,gt_filout       TYPE TABLE OF string
     .

DATA: v_field(500) TYPE c.

DATA: gv_progname     TYPE sy-repid
     ,gv_dynnum       TYPE sy-dynnr
     ,gt_dynpfields   LIKE dynpread OCCURS 0 WITH HEADER LINE
     ,gv_tabnam       TYPE dd02l-tabname
     .
TYPES: BEGIN OF s_dd03l
      ,fieldname TYPE dd03l-fieldname
      ,END OF s_dd03l
      .

DATA: gt_dd03l TYPE TABLE OF s_dd03l.

***************************************
PARAMETERS: p_tabnam TYPE dd02l-tabname
           ,p_where1(100) TYPE c
           ,p_where2(100) TYPE c
           ,p_where3(100) TYPE c
           ,p_where4(100) TYPE c
           ,p_where5(100) TYPE c
           .
SELECTION-SCREEN SKIP 1.

SELECT-OPTIONS s_field FOR dd03l.

SELECTION-SCREEN SKIP 1.
PARAMETERS: p_loc RADIOBUTTON GROUP radf
           ,p_serv RADIOBUTTON GROUP radf
           .
PARAMETERS: p_path(128) TYPE c
           ,p_file(128) TYPE c
           .
PARAMETERS: p_separ(1) TYPE c DEFAULT '|'.

************************************************************************
INITIALIZATION.
  gv_progname = sy-repid.
  gv_dynnum   = sy-dynnr.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_field-low.

* retrieval of the table containing
* the enter values of the selection screen
  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname     = gv_progname
      dynumb     = gv_dynnum
      request    = 'A'
    TABLES
      dynpfields = gt_dynpfields
    EXCEPTIONS
      OTHERS     = 9.
* retrieval of the value entered for the p_tabnam field
  LOOP AT gt_dynpfields WHERE fieldname = 'P_TABNAM'.
    gv_tabnam = gt_dynpfields-fieldvalue.
  ENDLOOP.

  CLEAR gt_dd03l[].
  SELECT fieldname INTO CORRESPONDING FIELDS OF TABLE gt_dd03l
  FROM dd03l
  WHERE tabname = gv_tabnam
    AND comptype = 'E'
  .
  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield    = 'FIELDNAME'
      dynpprog    = gv_progname
      dynpnr      = gv_dynnum
      dynprofield = 'S_FIELD-LOW'
      value_org   = 'S'
    TABLES
      value_tab   = gt_dd03l.
************************************************************************
AT SELECTION-SCREEN.

  IF p_tabnam IS INITIAL.
    SET CURSOR FIELD 'P_TABNAM'.
    MESSAGE 'Enter a Table Name !' TYPE 'E'.
  ENDIF.

  IF p_path IS INITIAL.
    SET CURSOR FIELD 'P_PATH'.
    MESSAGE 'Enter Path name !' TYPE 'E'.
  ENDIF.

  IF p_file IS INITIAL.
    SET CURSOR FIELD 'P_FILE'.
    MESSAGE 'Enter a File name !' TYPE 'E'.
  ENDIF.

  IF p_separ IS INITIAL.
    SET CURSOR FIELD 'P_SEPAR'.
    MESSAGE 'Enter a separator character !' TYPE 'E'.
  ENDIF.

  IF sy-ucomm = 'ONLI'.
    CONCATENATE p_path '/' p_file INTO gs_filename.
    WRITE gs_filename TO gs_filenamec.
    IF p_serv = 'X'.
      DELETE DATASET gs_filename.
      OPEN DATASET gs_filename
        FOR OUTPUT IN TEXT MODE
                   ENCODING DEFAULT
                   WITH SMART LINEFEED.
      IF sy-subrc NE 0.
        SET CURSOR FIELD 'P_PATH'.
        MESSAGE 'Outpt File cannot be opened !!!' TYPE 'E' .
      ENDIF.
    ELSE.
      CLEAR gt_filout[].
    ENDIF.
  ENDIF.

************************************************************************
START-OF-SELECTION.

* Create dynamic structure
  CREATE DATA gs_table TYPE TABLE OF (p_tabnam).
  ASSIGN gs_table->* TO  <fs>.
* select data into internal table
  CLEAR: gt_where[].
  IF p_where1 IS NOT INITIAL.
    APPEND p_where1 TO gt_where.
  ENDIF.
  IF p_where2 IS NOT INITIAL.
    APPEND p_where2 TO gt_where.
  ENDIF.
  IF p_where3 IS NOT INITIAL.
    APPEND p_where3 TO gt_where.
  ENDIF.
  IF p_where4 IS NOT INITIAL.
    APPEND p_where4 TO gt_where.
  ENDIF.
  IF p_where5 IS NOT INITIAL.
    APPEND p_where5 TO gt_where.
  ENDIF.

  IF p_where1 IS INITIAL.
    SELECT *
      FROM (p_tabnam) INTO TABLE <fs>
    .
  ELSE.
    SELECT *
      FROM (p_tabnam) INTO TABLE <fs>
      WHERE (gt_where)
    .
  ENDIF.

  IF sy-subrc NE 0.
    MESSAGE 'Select Found no record!' TYPE 'I'.
  ENDIF.

  IF p_file IS INITIAL.
    p_file = p_tabnam.
  ELSE.
  ENDIF.

******************
* export into file

* getting the column names of the table
  CREATE DATA gs_table_line TYPE (p_tabnam).
  ASSIGN gs_table_line->* TO  <fs_table_line>.
  go_descr ?= cl_abap_typedescr=>describe_by_data( <fs_table_line> ).
  CLEAR gt_tabfield[].
  LOOP AT go_descr->components INTO gs_comp.
    gs_tabfield = gs_comp-name.
    APPEND gs_tabfield TO gt_tabfield.
  ENDLOOP.
* getting the number of columns
  DESCRIBE TABLE gt_tabfield LINES gv_nb_col.

  LOOP AT <fs> ASSIGNING <fs_line>.

    CLEAR: gs_line.
*   process of each column
    DO gv_nb_col TIMES.

*     controling if the processed column is selected to be exported
      READ TABLE gt_tabfield INDEX sy-index
           INTO  gs_tabfield.
      CHECK gs_tabfield IN s_field.
*     getting the processed column value
      ASSIGN COMPONENT sy-index OF
                 STRUCTURE <fs_line> TO <fs_field>.
      WRITE <fs_field> TO v_field LEFT-JUSTIFIED.
      IF gs_line IS INITIAL.
        MOVE v_field TO gs_line.
      ELSE.
        CONCATENATE gs_line  p_separ v_field INTO gs_line.
      ENDIF.

    ENDDO.

    IF p_serv = 'X'.
      TRANSFER gs_line TO gs_filename.
    ELSE.
      APPEND gs_line TO gt_filout.
    ENDIF.
  ENDLOOP.

  IF p_serv = 'X'.
    CLOSE DATASET gs_filename.
    MESSAGE 'Output file successfully writen' TYPE 'I'.
  ELSE.
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                = gs_filename
      TABLES
        data_tab                = gt_filout
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        OTHERS                  = 22.
    IF sy-subrc <> 0.
      MESSAGE 'Error when writing in output file' TYPE 'E'.
    ELSE.
      MESSAGE 'Output file successfully writen' TYPE 'I'.
    ENDIF.
  ENDIF.
jackopet est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 09/02/2010, 10h59   #12
Membre confirmé
 
Inscription : août 2009
Messages : 226
Détails du profil
Informations forums :
Inscription : août 2009
Messages : 226
Points : 239
Points : 239
le problème de Drben (déjà c'est bien vieux, en + la 4.6 n'est presque plus maintenue maintenant, mais bon puisque tu relances je vais compléter), c'est que ça ne marche pas en 4.6 (4.6C j'imagine). Ton code ne marche pas non plus en 4.6C.

@Drben: je crois qu'en 4.6C la création dynamique comme présentée ci-dessus ne marche pas sur table interne, il faut passer par la création dynamique d'un source qui définit la table interne et une routine qui renvoie cette table interne. Bref, tu peux utiliser CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE. Tu trouveras de la doc dans des forums.

Juste pour l'explication, ça faisait qqch de ce genre:

Code :
1
2
3
4
5
6
7
8
TYPES : BEGIn OF struc,
... zones
END OF struc.
DATA itab TYPE STANDARD TABLE OF struc...
FORM xxx CHANGING e_ref_itab TYPE REF TO DATA.
  GET REFERENCE OF itab INTO e_ref_itab.
ENDFORM.
et dans ton programme tu fais:
remplir d'abord la table interne itab_source_abap avec le source ci-dessus (faut compléter ce qui manque)
Code :
1
2
3
4
5
6
7
8
9
GENERATE SUBROUTINE POOL itab_source_abap NAME progname.
DATA l_ref_itab TYPE REF TO DATA.
PERFORM xxx IN PROGRAM progname CHANGING l_ref_itab.
FIELD-SYMBOLS <itab> TYPE STANDARD TABLE.
FIELD-SYMBOLS <struc> TYPE ANY.
ASSIGN l_ref_itab->* TO <itab>.
LOOP AT <itab> ASSIGNING <struc>.
ENDLOOP.
sandraros est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/03/2011, 11h24   #13
Nouveau Membre du Club
 
Renaud Caussin
SAP
Inscription : février 2009
Messages : 52
Détails du profil
Informations personnelles :
Nom : Renaud Caussin
Âge : 24
Localisation : Belgique

Informations professionnelles :
Activité : SAP
Secteur : Industrie

Informations forums :
Inscription : février 2009
Messages : 52
Points : 33
Points : 33
Ces codes me seront bientôt très utiles également...

Merci beaucoup...
thedevilmaycry est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 21h18.


 
 
 
 
Partenaires

Hébergement Web