"-Begin-----------------------------------------------------------------
REPORT z_import_csv.
DATA:
it_raw_data TYPE truxs_t_text_data,
lv_useraction TYPE i,
lv_filetable TYPE filetable,
lv_rc TYPE sy-subrc,
lv_file TYPE string,
lr_data TYPE REF TO data,
lv_headline TYPE abap_bool,
lv_system TYPE cccategory,
lv_cnt TYPE i.
FIELD-SYMBOLS:
<lt_data> TYPE STANDARD TABLE.
TYPE-POOLS:truxs.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(40) cm_tname FOR FIELD pa_tname.
PARAMETERS pa_tname TYPE table_name.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(40) cm_file FOR FIELD pa_file.
PARAMETERS pa_file TYPE sapb-sappfad LOWER CASE.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(40) cm_sep FOR FIELD pa_sep.
PARAMETERS pa_sep(1) TYPE c LOWER CASE DEFAULT ';'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN SKIP 1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(40) cm_del FOR FIELD pa_del.
PARAMETERS pa_del As CheckBox Default Space.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(40) cm_head FOR FIELD pa_head.
PARAMETERS pa_head As CheckBox Default Space.
SELECTION-SCREEN END OF LINE.
INITIALIZATION.
cm_tname = 'Tabellenname'.
cm_file = 'Dateiname'.
cm_sep = 'Feldtrenner'.
cm_del = 'Tabellendaten löschen'.
cm_head = 'Kopfzeile vorhanden'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_file.
CLEAR pa_file.
CLEAR lv_filetable.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'CSV-Datei zum Upload in eine Tabelle'
multiselection = abap_false
default_extension = 'CSV'
file_filter = '(*.csv)|*.csv|(*.*)|*.*|'
CHANGING
file_table = lv_filetable
rc = lv_rc
user_action = lv_useraction
EXCEPTIONS
others = 1.
IF lv_useraction = 0 AND sy-subrc = 0 AND lv_rc = 1.
READ TABLE lv_filetable INTO pa_file INDEX 1.
ENDIF.
AT SELECTION-SCREEN.
CALL FUNCTION 'TR_SYS_PARAMS'
IMPORTING
SYSTEM_CLIENT_ROLE = lv_system
EXCEPTIONS
NO_SYSTEMNAME = 1
NO_SYSTEMTYPE = 2
OTHERS = 3.
IF sy-subrc <> 0 Or lv_system = 'P'.
MESSAGE 'Keine Berechtigung zur Nutzung dieser Funktionen' Type 'I'.
EXIT.
ENDIF.
SELECT COUNT( * ) INTO lv_cnt FROM tadir
WHERE pgmid = 'R3TR' AND object = 'TABL' AND obj_name = pa_tname.
IF lv_cnt <> 1.
MESSAGE 'Tabelle nicht vorhanden' Type 'I'.
EXIT.
ENDIF.
CLEAR lv_file.
lv_file = pa_file.
CHECK lv_file IS NOT INITIAL AND pa_tname IS NOT INITIAL.
IF pa_head IS INITIAL.
lv_headline = abap_false.
ELSE.
lv_headline = abap_true.
ENDIF.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_file
filetype = 'ASC'
TABLES
data_tab = it_raw_data
EXCEPTIONS
others = 1.
IF sy-subrc <> 0.
Message 'Upload konnte nicht durchgeführt werden' Type 'E'.
EXIT.
ENDIF.
CREATE DATA lr_data TYPE STANDARD TABLE OF (pa_tname).
ASSIGN lr_data->* TO <lt_data>.
CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP'
EXPORTING
i_field_seperator = pa_sep
i_line_header = lv_headline
i_tab_raw_data = it_raw_data
TABLES
i_tab_converted_data = <lt_data>
EXCEPTIONS
conversion_failed = 1
others = 2.
IF sy-subrc <> 0.
MESSAGE 'CSV-Konvertierung gescheitert' Type 'E'.
EXIT.
ENDIF.
IF pa_del IS NOT INITIAL.
DELETE FROM (pa_tname).
IF sy-subrc = 0.
INSERT (pa_tname) FROM TABLE <lt_data>.
IF sy-subrc = 0.
MESSAGE 'Daten wurden in Tabelle übernommen' Type 'S'.
ENDIF.
ELSE.
MESSAGE 'Daten konnten nicht gelöscht werden' Type 'E'.
ENDIF.
ELSE.
INSERT (pa_tname) FROM TABLE <lt_data>.
IF sy-subrc = 0.
MESSAGE 'Daten wurden in Tabelle übernommen' Type 'S'.
ENDIF.
ENDIF.
"-End-------------------------------------------------------------------