Code: Alles auswählen.
DATA(lt_data) = VALUE string_table( ).
Code: Alles auswählen.
ASSIGN COMPONENT sy-index + 11 OF STRUCTURE ls_table TO FIELD-SYMBOL(<fs_comp>).
IF sy-index > ( lines( lt_columns ) ).
EXIT.
ENDIF.
Code: Alles auswählen.
* Überschriften vorhanden
PARAMETERS: p_head AS CHECKBOX DEFAULT ''.
* Separator / Trennzeichen
PARAMETERS: p_sep TYPE char1 DEFAULT ','.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (50) TEXT-001.
SELECTION-SCREEN END OF LINE.
PERFORM import_csv_to_table.
*&---------------------------------------------------------------------*
*& Form IMPORT_CSV_TO_DATAENGINE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM import_csv_to_table.
TRY.
TABLES: ztc_table.
DATA: lt_files TYPE filetable,
lv_rc TYPE i,
lv_action TYPE i.
cl_gui_frontend_services=>file_open_dialog( EXPORTING
file_filter = |csv (*.csv)\|*.csv\|{ cl_gui_frontend_services=>filetype_all }|
multiselection = abap_true
CHANGING
file_table = lt_files
rc = lv_rc
user_action = lv_action ).
IF lv_action = cl_gui_frontend_services=>action_ok AND
lines( lt_files ) = 1.
DATA(lt_data) = VALUE string_table( ).
cl_gui_frontend_services=>gui_upload( EXPORTING
filename = CONV #( lt_files[ 1 ]-filename )
filetype = 'DAT'
CHANGING
data_tab = lt_data ).
DATA(lv_startzeile) = COND i( WHEN p_head = abap_true THEN 2 ELSE 1 ).
LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<zeile>).
DATA(ls_table) = VALUE ztc_table( ).
* <zeile> = |{ sy-tabix }{ p_sep }{ <zeile> }|.
SPLIT <zeile> AT p_sep INTO TABLE DATA(lt_columns).
DO.
"bei 12 wird nur 1 ausgegeben. Unten das Problem wegen out of bounce lösen
ASSIGN COMPONENT sy-index + 11 OF STRUCTURE ls_table TO FIELD-SYMBOL(<fs_comp>).
IF sy-index > ( lines( lt_columns ) ).
EXIT.
ENDIF.
<fs_comp> = lt_columns[ sy-index ].
ENDDO.
INSERT ztc_table FROM ls_table.
ENDLOOP.
ENDIF.
CATCH cx_root INTO DATA(e_text).
MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.
ENDFORM.
Folgende Benutzer bedankten sich beim Autor DeathAndPain für den Beitrag:
tekko