Save internal table as CSV file delimited by Tab
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.
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:\tmp\test.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.
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:\tmp\test.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 in my infodepot:
Full list of examples in my infodepot
If you have a question, have found an error or just want to contact me, please use this form.
If you have a question, have found an error or just want to contact me, please use this form.
Copyright (C) 2013 http://www.kerum.pl/infodepot/
Disclaimer: I am not affiliated or related to any division or subsidiary of SAP AG.
Trademarks or registered trademarks of any products or companies referred to on this site belong to those companies.
Anyone using the given solutions, is doing it under his/her own responsibility and at own risk.