You can use FM SAP_CONVERT_TO_CSV_FORMAT :
DATA: lt_csvdata TYPE truxs_t_text_data.
* Convert table to CSV (separator semicolon in France)
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
EXPORTING
i_field_seperator = ';'
TABLES
i_tab_sap_data = lt_table
CHANGING
i_tab_converted_data = lt_csvdata
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
Notice that input parameter i_line_header is not implemented. FM provides table content without Header line.
To get technical fieldnames, you can use method cl_abap_typedescr=>describe_by_data( ).
Example of implementation to get CSV table with header line :
* <SIGNATURE>-------------------------------------------------------------------+
* | Static Public Method ZCL_BC_TOOLS=>CONVERT_TABLE_TO_CSV
* +-----------------------------------------------------------------------------+
* | [--->] IT_TABLE TYPE STANDARD TABLE
* | [--->] IV_LINE_HEADER TYPE ABAP_BOOL (default ='X')
* | [--->] IV_SEPARATOR TYPE CHAR1 (default =';')
* | [<-()] RT_CSV TYPE TRUXS_T_TEXT_DATA
* +------------------------------------------------------------------</SIGNATURE>
METHOD convert_table_to_csv.
" -------------------------------------------------------------------
" Convert internal table into CSV with technical names as header line
" -------------------------------------------------------------------
DATA : lo_struct_descriptor TYPE REF TO cl_abap_structdescr.
CHECK it_table IS NOT INITIAL.
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
EXPORTING
i_field_seperator = iv_separator
TABLES
i_tab_sap_data = it_table
CHANGING
i_tab_converted_data = rt_csv
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
RETURN.
ENDIF.
IF iv_line_header IS NOT INITIAL.
TRY.
lo_struct_descriptor ?= cl_abap_typedescr=>describe_by_data( it_table[ 1 ] ).
INSERT INITIAL LINE INTO rt_csv ASSIGNING FIELD-SYMBOL(<fs_line_header>)
INDEX 1.
LOOP AT lo_struct_descriptor->components
ASSIGNING FIELD-SYMBOL(<fs_component>).
AT FIRST.
<fs_line_header> = <fs_component>-name.
CONTINUE.
ENDAT.
<fs_line_header> = <fs_line_header> &&
iv_separator && <fs_component>-name.
ENDLOOP.
CATCH cx_sy_move_cast_error.
RETURN.
ENDTRY.
ENDIF.
ENDMETHOD.
Code example to save CSV file
DATA : lv_filename TYPE string.
IF gt_table_with_data IS INITIAL.
MESSAGE i001(00) WITH 'No data to export.'(021).
RETURN.
ENDIF.
" Convertions en CSV (with the method described above)
DATA(lt_csv) = zcl_bc_tools=>convert_table_to_csv( gt_table_with_data ).
" Do not display empty dates :
LOOP AT lt_csv ASSIGNING FIELD-SYMBOL().
REPLACE ALL OCCURRENCES OF '00.00.0000' IN WITH ''.
ENDLOOP.
" Open the popup window to choose the diretory to save the file
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename = |{ lv_default_filename }.csv|
mode = 'S'
title = 'Export CSV file'
IMPORTING
filename = lv_filename
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
MESSAGE s001(00) WITH 'CSV Export canncelled bu user.' DISPLAY LIKE 'W'.
RETURN.
ENDIF.
" Download file
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = lv_filename
TABLES
data_tab = lt_csv
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.
About the author