* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCLASS->READ_CSV_FILE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_DELIMITER TYPE CL_RSDA_CSV_CONVERTER=>CHAR (default =C_DEFAULT_DELIMITER)
* | [--->] IM_FIELD_SEPARATOR TYPE CL_RSDA_CSV_CONVERTER=>CHAR (default =C_DEFAULT_SEPARATOR)
* | [--->] IM_FILEPATH TYPE STRING
* | [<---] EX_TABLE TYPE ANY TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
DATA: lt_raw_data TYPE truxs_t_text_data,
lv_dataset_line TYPE string,
ref_wa TYPE REF TO data.
FIELD-SYMBOLS: <fs_itab> TYPE ANY TABLE,
<fs_wa> TYPE any.
DATA(lo_csv_converter) = cl_rsda_csv_converter=>create( i_delimiter = im_delimiter i_separator =
im_field_separator ).
"CREATE A DYNAMIC TABLE WITH THE SAME STRUCTURE AS TARGETED TABLE
ASSIGN ex_table TO <fs_itab>.
"CREATE A DYNAMIC STRUCTURE
CREATE DATA ref_wa LIKE LINE OF <fs_itab>.
ASSIGN ref_wa->* TO <fs_wa>.
"UPLOAD CSV FILE
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = im_filepath
filetype = 'ASC'
TABLES
data_tab = lt_raw_data.
"SEPARATE VALUES AND APPEND THEM INTO TARGET TABLE
LOOP AT lt_raw_data INTO DATA(ls_csv_line).
CALL METHOD lo_csv_converter->csv_to_structure
EXPORTING
i_data = ls_csv_line
IMPORTING
e_s_data = <fs_wa>.
INSERT <fs_wa> INTO TABLE ex_table.
ENDLOOP.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCLASS->SAVE_CSV_FILE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_FILEPATH TYPE STRING
* | [--->] IM_FIELD_SEPARATOR TYPE CL_RSDA_CSV_CONVERTER=>CHAR (default =C_DEFAULT_SEPARATOR)
* | [--->] IM_TABLE TYPE ANY TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
DATA: lt_csv TYPE TABLE OF string,
lv_row TYPE string,
lv_string TYPE string.
"CONSTRUCT THE TARGET TABLE FOR DOWNLOAD.SEPARATE VALUE WITH COMMAS
LOOP AT im_table ASSIGNING FIELD-SYMBOL(<fs_line>).
CLEAR xout.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_value>).
IF sy-subrc NE 0.
EXIT.
ENDIF.
IF sy-index EQ 1.
lv_row = <fs_value>.
ELSE.
lv_string = <fs_value>.
CONDENSE lv_string.
CONCATENATE lv_row lv_string INTO lv_row SEPARATED BY im_field_separator.
ENDIF.
ENDDO.
APPEND lv_row TO lt_csv.
ENDLOOP.
"DOWNLOAD THE TABLE INTO CSV FILE
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = im_filepath
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.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCLASS->READ_TSV_FILE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_FILEPATH TYPE STRING
* | [<---] EX_TABLE TYPE ANY TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD read_tsv_file.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = im_filepath
filetype = 'ASC'
has_field_separator = abap_true
CHANGING
data_tab = ex_table.
ENDMETHOD.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCLASS->SAVE_TSV_FILE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IM_TABLE TYPE ANY TABLE
* | [--->] IM_FILEPATH TYPE STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD save_tsv_file.
DATA: lt_tsv TYPE TABLE OF string,
lv_line TYPE string,
lv_value TYPE string.
LOOP AT im_table ASSIGNING FIELD-SYMBOL(<fs_line>).
CLEAR lv_line.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_value>).
IF sy-subrc NE 0.
EXIT.
ENDIF.
IF sy-index EQ 1.
lv_line = <fs_value>.
ELSE.
lv_value = <fs_value>.
CONDENSE lv_value.
CONCATENATE lv_line lv_value INTO lv_line SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
ENDIF.
ENDDO.
APPEND lv_line TO lt_tsv.
ENDLOOP.
cl_gui_frontend_services=>gui_download(
EXPORTING
filename = im_filepath
filetype = 'DAT'
CHANGING
data_tab = lt_tsv
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 ).
ENDMETHOD.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |