Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[ABAP] Download internal table as TXT or CSV

01
02
03
04
05
06
07
08
09
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
" ty_output and tt_output are just dummy types
 
DATA: l_output_line    TYPE ty_output,
      l_output_lines   TYPE tt_output, 
      l_csv_output     TYPE truxs_t_text_data,
      l_txt_output     TYPE TABLE OF string,
 
PARAMETERS: p_alv   RADIOBUTTON GROUP rb1 DEFAULT 'X' USER-COMMAND radio,
            p_csv   RADIOBUTTON GROUP rb1,
            p_flcsv TYPE rlgrap-filename DEFAULT 'c:\temp\file.csv',
            p_txt   RADIOBUTTON GROUP rb1,
            p_fltxt TYPE rlgrap-filename DEFAULT 'c:\temp\file.txt'.
 
" fill table l_output_lines with your data you want to export as txt or csv
 
  IF p_csv = abap_true.
 
    CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
      TABLES
        i_tab_sap_data       = l_output_lines
      CHANGING
        i_tab_converted_data = l_csv_output.
 
    TRY.
        cl_gui_frontend_services=>gui_download( EXPORTING filename = CONV #( p_flcsv )
                                                          filetype = 'ASC'
                                                CHANGING  data_tab = l_csv_output ). 
      CATCH cx_root INTO DATA(e_text).
        MESSAGE e_text->get_text( ) TYPE 'I'.
    ENDTRY.
 
 
  ELSEIF p_txt = abap_true.
 
    LOOP AT l_output_lines INTO l_output_line.
      CALL FUNCTION 'SO_STRUCT_TO_CHAR'
        EXPORTING
          ip_struct = l_output_line
        IMPORTING
          ep_string = l_string.
      APPEND l_string TO l_txt_output.
    ENDLOOP.
 
    TRY.
        cl_gui_frontend_services=>gui_download( EXPORTING filename = CONV #( p_fltxt )
                                                          filetype = 'ASC'
                                                CHANGING  data_tab = l_txt_output ).
      CATCH cx_root INTO e_text.
        MESSAGE e_text->get_text( ) TYPE 'I'.
    ENDTRY.
 
  ENDIF.