2

Got a little problem by using the function 'GUI_DOWNLOAD'. Tried to append a header line at the top of my .txt file i created. My solution is:

 CALL FUNCTION 'GUI_DOWNLOAD'
   EXPORTING
     filename              = lv_file
     filetype              = 'ASC'
     write_field_separator = 'X'
   TABLES
     data_tab              = it_outh. "internal table just with the header line

 CALL FUNCTION 'GUI_DOWNLOAD'
   EXPORTING
     filename              = lv_file
     filetype              = 'ASC'
     append                = 'X'
     write_field_separator = 'X'
   TABLES
     data_tab              = it_output. "internal table with selected data

The code works. But the formatting is crap, cause of the different length (header line and data)

formatting of my file

Is the only way to fix that problem, using shorter descriptions in it_outh? Or does anyone of you have a better idea for me?

Have a nice day.

Regards, Dennis

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
creative235
  • 21
  • 1
  • 3

1 Answers1

1

Here is a quick-n-dirty workaround how to make an aligned output of itab with header into text:

FIELD-SYMBOLS: <fs_table> TYPE STANDARD TABLE.

DATA: lref_struct TYPE REF TO cl_abap_structdescr,
      o_table     TYPE REF TO data.

lref_struct ?= cl_abap_structdescr=>describe_by_name( 'CRCO' ).
DATA(components) = lref_struct->get_components( ).
DATA(fields) = VALUE ddfields( FOR line IN lref_struct->get_ddic_field_list( ) ( line ) ).

" making all types as char by replacing with existing PRZ char field
MODIFY components FROM VALUE abap_componentdescr( type = components[ name = 'PRZ' ]-type ) TRANSPORTING type WHERE name <> ''.

lref_struct = cl_abap_structdescr=>create( components ).
DATA(o_ref_table) = cl_abap_tabledescr=>create( p_line_type = lref_struct p_table_kind = cl_abap_tabledescr=>tablekind_std ).

CHECK o_ref_table IS BOUND.

CREATE DATA o_table TYPE HANDLE o_ref_table.
ASSIGN o_table->* TO <fs_table>.
APPEND INITIAL LINE TO <fs_table>. " reserving line for headers

SELECT
  CAST( mandt      AS CHAR( 12 ) ) AS mandt,
  CAST( objty      AS CHAR( 2 ) ) AS objty,
  CAST( objid      AS CHAR( 8 ) ) AS objid,
  CAST( laset      AS CHAR( 6 ) ) AS laset,
  CAST( endda      AS CHAR( 8 ) ) AS endda,
  CAST( lanum      AS CHAR( 4 ) ) AS lanum,
  CAST( begda      AS CHAR( 8 ) ) AS begda,
  CAST( aedat_kost AS CHAR( 8 ) ) AS aedat_kost,
  CAST( aenam_kost AS CHAR( 12 ) ) AS aenam_kost,
  CAST( kokrs      AS CHAR( 10 ) ) AS kokrs,
  CAST( kostl      AS CHAR( 6 ) ) AS kostl,
  CAST( lstar      AS CHAR( 12 ) ) AS lstar,
  CAST( lstar_ref  AS CHAR( 12 ) ) AS lstar_ref,
  CAST( forml      AS CHAR( 12 ) ) AS forml,
  CAST( prz        AS CHAR( 12 ) ) AS prz,
  CAST( actxy      AS CHAR( 12 ) ) AS actxy,
  CAST( actxk      AS CHAR( 12 ) ) AS actxk,
  CAST( leinh      AS CHAR( 12 ) ) AS leinh,
  CAST( bde        AS CHAR( 12 ) ) AS bde,
  CAST( sakl       AS CHAR( 1 ) ) AS sakl
  UP TO 10 ROWS
  FROM crco
  APPENDING CORRESPONDING FIELDS OF TABLE @<fs_table>.

" writing headers
ASSIGN <fs_table>[ 1 ] TO FIELD-SYMBOL(<empty>).
LOOP AT fields ASSIGNING FIELD-SYMBOL(<field>).
  ASSIGN COMPONENT <field>-fieldname OF STRUCTURE <empty> TO FIELD-SYMBOL(<heading>).
  CHECK sy-subrc = 0.
  <heading> = <field>-scrtext_m.
ENDLOOP.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename     = 'C:\tab.txt'
    filetype       = 'ASC'
    write_field_separator = 'X'
  TABLES
    data_tab     = <fs_table>.

Though, it is not that simple, but it definitely does the job

enter image description here

The trick that is used in the above snippet: target internal table is created dynamically making all the fields char unlike real DB table, then the dummy line is added at the top of the table and all the headings are put there.

This approach requires additional preliminary work (like explicit casting of all DB fields) but I see no other way to make formatted TXT output.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90