Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSV_Upload_long_process
FORM convert_file_to_loading_data USING p_file_data TYPE zfile_data_tty " input file data table type
CHANGING c_loading_data TYPE z_data_tty. " Target data table type
DATA: lt_csv_table_struct TYPE REF TO data,
lo_csv_table_desc TYPE REF TO cl_abap_tabledescr,
lo_csv_table_desc_struc TYPE REF TO cl_abap_structdescr,
lt_csv_table_flds TYPE abap_compdescr_tab,
lw_csv_table_flds TYPE abap_compdescr,
lw_file_rawline TYPE LINE OF textline_t,
lt_file_columns TYPE table_of_strings,
lw_file_columns TYPE LINE OF table_of_strings,
lw_fieldcatalog TYPE lvc_s_fcat,
lt_fieldcatalog TYPE lvc_t_fcat,
lt_dynamictable TYPE REF TO data,
lw_dynamicline TYPE REF TO data,
lw_file_csv_struct TYPE zpp_manfactr_ticket_data_str,
lv_loop_count TYPE i,
lv_line_count TYPE i,
lv_fieldname TYPE char30,
lv_csv_esc TYPE char1.
CONSTANTS: lc_csv_sep TYPE char1 VALUE ',',
lc_csv_num_cols TYPE i VALUE 11.
FIELD-SYMBOLS: <fs_csv_table_struct> TYPE STANDARD TABLE,
<fs_dynamic_table> TYPE STANDARD TABLE,
<fs_dynamic_lines> TYPE any,
<fs_dynamic_field> TYPE any.
*Get structure of destination table
GET REFERENCE OF c_loading_data INTO lt_csv_table_struct.
ASSIGN lt_csv_table_struct->* TO <fs_csv_table_struct>.
lo_csv_table_desc ?= cl_abap_structdescr=>describe_by_data_ref( lt_csv_table_struct ).
lo_csv_table_desc_struc ?= lo_csv_table_desc->get_table_line_type( ).
lt_csv_table_flds = lo_csv_table_desc_struc->components.
* Create field catalog
LOOP AT lt_csv_table_flds INTO lw_csv_table_flds.
lw_fieldcatalog-fieldname = lw_csv_table_flds-name.
lw_fieldcatalog-inttype = lw_csv_table_flds-type_kind.
lw_fieldcatalog-intlen = lw_csv_table_flds-length.
lw_fieldcatalog-decimals = lw_csv_table_flds-decimals.
APPEND lw_fieldcatalog TO lt_fieldcatalog.
ENDLOOP.
* Create a dynamic table for field cat values
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = lt_fieldcatalog
IMPORTING
ep_table = lt_dynamictable
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
"create dynamic table lines
ASSIGN lt_dynamictable->* TO <fs_dynamic_table>.
CREATE DATA lw_dynamicline LIKE LINE OF <fs_dynamic_table>.
ASSIGN lw_dynamicline->* TO <fs_dynamic_lines>.
" process the CSV file raw data
LOOP AT p_file_data INTO lw_file_rawline.
CALL FUNCTION 'RSDS_CONVERT_CSV'
EXPORTING
i_data_sep = lc_csv_sep
i_esc_char = lv_csv_esc
i_record = lw_file_rawline
i_field_count = lc_csv_num_cols
IMPORTING
e_t_data = lt_file_columns
EXCEPTIONS
escape_no_close = 1
escape_improper = 2
conversion_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" store CSV fields into dynamic table
lv_loop_count = 1.
DESCRIBE TABLE lt_file_columns LINES lv_line_count.
LOOP AT lt_csv_table_flds INTO lw_csv_table_flds.
MOVE lw_csv_table_flds-name TO lv_fieldname.
ASSIGN COMPONENT lv_fieldname
OF STRUCTURE <fs_dynamic_lines>
TO <fs_dynamic_field>.
READ TABLE lt_file_columns INDEX lv_loop_count INTO lw_file_columns.
IF lv_loop_count <= lv_line_count.
<fs_dynamic_field> = lw_file_columns.
ELSE.
<fs_dynamic_field> = ''.
ENDIF.
lv_loop_count = lv_loop_count + 1.
ENDLOOP.
MOVE-CORRESPONDING <fs_dynamic_lines> TO lw_file_csv_struct.
APPEND lw_file_csv_struct TO c_loading_data.
ENDLOOP.
ENDFORM. " CONVERT_FILE_TO_LOADING_DATA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment