At some point anyone can have a necessity of downloading table data and if they have more than 10 tables to download then they can use this simple program to download the data instead of manually downloading it one by one in SE16 / SE11. this program just downloads the content without header.
TABLES : dd02l.
SELECTION-SCREEN : BEGIN OF BLOCK b01 WITH FRAME TITLE text-001.
SELECT-OPTIONS : s_tname FOR dd02l-tabname.
PARAMETERS : p_file TYPE string.
SELECTION-SCREEN : END OF BLOCK b01.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL METHOD cl_gui_frontend_services=>directory_browse
CHANGING
selected_folder = p_file
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
START-OF-SELECTION.
PERFORM get_download_data.
*&---------------------------------------------------------------------*
*& Form GET_DOWNLOAD_DATA
*&---------------------------------------------------------------------*
* Get and download table data
*----------------------------------------------------------------------*
FORM get_download_data .
DATA : lt_dd02l TYPE STANDARD TABLE OF dd02l,
ls_dd02l TYPE dd02l,
lt_dd03l TYPE STANDARD TABLE OF dd03l,
ls_dd03l TYPE dd03l,
* lt_comp TYPE abap_compdescr_tab,
* ls_comp TYPE LINE OF abap_compdescr_tab,
* ls_fc TYPE lvc_s_fcat,
* lt_fc TYPE lvc_t_fcat,
lt_tbl_data TYPE REF TO data,
ls_str_data TYPE REF TO data,
lo_abap_structdescr TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS : <fs_dyn_table> TYPE STANDARD TABLE,
<fs_dyn_wa> TYPE ANY,
<fs_field> TYPE ANY.
SELECT * FROM dd02l INTO TABLE lt_dd02l WHERE tabname IN s_tname
AND as4local EQ 'A'
AND tabclass EQ 'TRANSP'.
IF sy-subrc IS INITIAL.
LOOP AT lt_dd02l INTO ls_dd02l.
CREATE DATA lt_tbl_data TYPE TABLE OF (ls_dd02l-tabname).
ASSIGN lt_tbl_data->* TO <fs_dyn_table>.
IF sy-subrc IS INITIAL AND <fs_dyn_table> IS ASSIGNED.
SELECT * INTO CORRESPONDING FIELDS OF TABLE <fs_dyn_table> FROM (ls_dd02l-tabname).
IF sy-subrc IS INITIAL.
PERFORM download_table_data USING <fs_dyn_table>
ls_dd02l-tabname.
CLEAR <fs_dyn_table>.
ENDIF.
ENDIF.
CLEAR : ls_dd02l, lt_dd03l, lt_tbl_data, ls_str_data.
UNASSIGN: <fs_dyn_wa>, <fs_dyn_table>.
ENDLOOP.
ENDIF.
ENDFORM. " GET_DOWNLOAD_DATA
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_TABLE_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_<FS_DYN_TABLE> text
* -->P_LS_DD02L_TABNAME text
*----------------------------------------------------------------------*
FORM download_table_data USING it_table TYPE ANY TABLE
iv_table_name TYPE dd02l-tabname.
DATA : lv_filename TYPE string.
CONCATENATE p_file iv_table_name INTO lv_filename SEPARATED BY '/'.
CONCATENATE lv_filename '.XLS' INTO lv_filename.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = lv_filename
filetype = 'ASC'
write_field_separator = 'X'
CHANGING
data_tab = it_table.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " DOWNLOAD_TABLE_DATA
If we need to download the data with header then we will have to make all the columns as string to cater for scenarios where we have numeric and currency fields and then we can get the column names from DD03L and get column names and insert the header as first row after the data is fetched. we can use code something like below to change the type and the below code might not be 100% working but we can use the code as reference:
lo_abap_structdescr ?= cl_abap_typedescr=>describe_by_name( ls_dd02l-tabname ).
lt_comp[] = lo_abap_structdescr->components[].
LOOP AT lt_comp INTO ls_comp.
ls_fc-fieldname = ls_comp-name .
ls_fc-datatype = 'STRG'.
APPEND ls_fc TO lt_fc.
CLEAR ls_fc.
ENDLOOP.
* Create dynamic internal table and assign to FS
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fc
i_length_in_byte = 'X'
IMPORTING
ep_table = lt_tbl_data.
Hope this information would be helpful.
Thanks,
Naveen