Great video I'm doing this at the moment but mine needs to accept any csv file so I need to put the code into a dynamic internal table Any ideas on how to do that?
Do you select your file from your local computer or from the application server? I am not sure yet what exactly your requirement is. Do you want to upload the CSV without manual confirmation? 🤔
Best way is to write it 😉 it is helpful for the understanding! 😊 but here it is: ----- Code Begin ----- " declare variables, types and structures TYPES: BEGIN OF ty_s_csv, mandt TYPE string, carrid TYPE string, connid TYPE string, text TYPE string, text2 TYPE string, END OF ty_s_csv. TYPES: ty_it_csv TYPE STANDARD TABLE OF ty_s_csv WITH DEFAULT KEY. " checkbox has the file a header PARAMETERS: p_header AS CHECKBOX DEFAULT 'X'. TRY. " call file open dialog DATA: lv_rc TYPE i, it_files TYPE filetable, 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_false CHANGING file_table = it_files rc = lv_rc user_action = lv_action ). IF lv_action = cl_gui_frontend_services=>action_ok. IF lines( it_files ) = 1. DATA(it_csv_strings) = VALUE string_table( ). " read csv file to internal csv string tab cl_gui_frontend_services=>gui_upload( EXPORTING filename = CONV #( it_files[ 1 ]-filename ) filetype = 'ASC' CHANGING data_tab = it_csv_strings ). cl_demo_output=>write_data( it_csv_strings ). " check if header exists DATA(lv_start_line) = COND i( WHEN p_header = abap_true THEN 2 ELSE 1 ). " check if we have some processable entries IF ( lines( it_csv_strings ) > lv_start_line - 1 ). " create itab csv processing DATA(it_csv) = VALUE ty_it_csv( ). LOOP AT it_csv_strings ASSIGNING FIELD-SYMBOL() FROM lv_start_line. DATA(ls_csv_line) = VALUE ty_s_csv( ). SPLIT AT ';' INTO TABLE DATA(it_columns). IF lines( it_columns ) = 5. ls_csv_line-mandt = it_columns[ 1 ]. ls_csv_line-carrid = it_columns[ 2 ]. ls_csv_line-connid = it_columns[ 3 ]. ls_csv_line-text = it_columns[ 4 ]. ls_csv_line-text2 = it_columns[ 5 ]. ENDIF. APPEND ls_csv_line TO it_csv. ENDLOOP. cl_demo_output=>write_data( it_csv ). DATA(lv_html) = cl_demo_output=>get( ). cl_abap_browser=>show_html( EXPORTING title = 'CSV Data' html_string = lv_html container = cl_gui_container=>default_screen ). WRITE: space. ENDIF. ENDIF. ENDIF. CATCH cx_root INTO DATA(e_text). MESSAGE e_text->get_text( ) TYPE 'I'. ENDTRY.
It's really helped a lot!! Thanks for the great video!!! I like your video so much!
Thank you! 😃 Glad to hear that! 🤗
Great video
I'm doing this at the moment but mine needs to accept any csv file so I need to put the code into a dynamic internal table
Any ideas on how to do that?
Do you select your file from your local computer or from the application server? I am not sure yet what exactly your requirement is. Do you want to upload the CSV without manual confirmation? 🤔
How do we import file from application server and access it in the program? Can you help me on this requirement?
I already did a video where I fetch some filled from the application server. If you have done this you can access the data in the file 🤗
could you please copy paste the code in comment section, or share some text file is having this? I want to try similar case
Best way is to write it 😉 it is helpful for the understanding! 😊 but here it is:
----- Code Begin -----
" declare variables, types and structures
TYPES: BEGIN OF ty_s_csv,
mandt TYPE string,
carrid TYPE string,
connid TYPE string,
text TYPE string,
text2 TYPE string,
END OF ty_s_csv.
TYPES: ty_it_csv TYPE STANDARD TABLE OF ty_s_csv WITH DEFAULT KEY.
" checkbox has the file a header
PARAMETERS: p_header AS CHECKBOX DEFAULT 'X'.
TRY.
" call file open dialog
DATA: lv_rc TYPE i,
it_files TYPE filetable,
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_false
CHANGING
file_table = it_files
rc = lv_rc
user_action = lv_action ).
IF lv_action = cl_gui_frontend_services=>action_ok.
IF lines( it_files ) = 1.
DATA(it_csv_strings) = VALUE string_table( ).
" read csv file to internal csv string tab
cl_gui_frontend_services=>gui_upload( EXPORTING
filename = CONV #( it_files[ 1 ]-filename )
filetype = 'ASC'
CHANGING
data_tab = it_csv_strings ).
cl_demo_output=>write_data( it_csv_strings ).
" check if header exists
DATA(lv_start_line) = COND i( WHEN p_header = abap_true THEN 2 ELSE 1 ).
" check if we have some processable entries
IF ( lines( it_csv_strings ) > lv_start_line - 1 ).
" create itab csv processing
DATA(it_csv) = VALUE ty_it_csv( ).
LOOP AT it_csv_strings ASSIGNING FIELD-SYMBOL() FROM lv_start_line.
DATA(ls_csv_line) = VALUE ty_s_csv( ).
SPLIT AT ';' INTO TABLE DATA(it_columns).
IF lines( it_columns ) = 5.
ls_csv_line-mandt = it_columns[ 1 ].
ls_csv_line-carrid = it_columns[ 2 ].
ls_csv_line-connid = it_columns[ 3 ].
ls_csv_line-text = it_columns[ 4 ].
ls_csv_line-text2 = it_columns[ 5 ].
ENDIF.
APPEND ls_csv_line TO it_csv.
ENDLOOP.
cl_demo_output=>write_data( it_csv ).
DATA(lv_html) = cl_demo_output=>get( ).
cl_abap_browser=>show_html( EXPORTING
title = 'CSV Data'
html_string = lv_html
container = cl_gui_container=>default_screen ).
WRITE: space.
ENDIF.
ENDIF.
ENDIF.
CATCH cx_root INTO DATA(e_text).
MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.