The easiest way to save internal table as CSV file with columns separated by Tab as delimiter-character
is to make a use of parameter WRITE_FIELD_SEPARATOR in method GUI_DOWNLOAD of CL_GUI_FRONTEND_SERVICES.
Remark #1: If you need semicolon instead if tabulator, then use function SAP_CONVERT_TO_CSV_FORMAT
to convert the output.
Remark #2: Pay special attention for columns with numbers and amounts,
they will be written out converted with effective settings for thousand and decimal separator.
See an example below.
REPORT ztest.
TYPES:
BEGIN OF ts_itab,
field1(3) TYPE n,
field2(10) TYPE c,
END OF ts_itab.
DATA:
ls_itab TYPE ts_itab,
lt_itab TYPE TABLE OF ts_itab,
lv_path TYPE string VALUE 'c:tmptest.txt'.
ls_itab-field1 = '111'. ls_itab-field2 = 'AAA'. APPEND ls_itab TO lt_itab.
ls_itab-field1 = '222'. ls_itab-field2 = 'BBB'. APPEND ls_itab TO lt_itab.
ls_itab-field1 = '333'. ls_itab-field2 = 'CCC'. APPEND ls_itab TO lt_itab.
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = lv_path
filetype = 'ASC'
write_field_separator = 'X' " Separate Columns by Tabs in Case of ASCII Download
CHANGING
data_tab = lt_itab
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
not_supported_by_gui = 22
error_no_gui = 23
OTHERS = 24.
IF sy-subrc <> 0.
WRITE: / 'Error, sy-subrc =', sy-subrc.
ELSE.
WRITE: / 'OK'.
ENDIF.
See other related notes on my website: