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.
About the author