*---------------------------------------------------------------------- * Upload data from a CSV file into the database table Z_MYTABLE. Input * file must be in CSV format, with the fields in the following order... * 1. Sales Organisation * 2. Sales Office *---------------------------------------------------------------------- REPORT Z_UPLOAD_CSV_TO_DATABASE message-id ZBC_O NO STANDARD PAGE HEADING. *** INTERNAL TABLES *** * Uploaded data from the CSV file TYPES: BEGIN OF t_file_data, line(600) TYPE c, END OF t_file_data. DATA: v_file_data TYPE t_file_data, i_file_data TYPE STANDARD TABLE OF t_file_data. * A line for the database DATA: v_db_table TYPE Z_MYTABLE. *---------------------------------------------------------------------* * SELECTION-SCREEN *---------------------------------------------------------------------* PARAMETERS: p_file TYPE rlgrap-filename OBLIGATORY DEFAULT 'c:/table.csv'. *---------------------------------------------------------------------* * AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file *---------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. PERFORM ask_for_filename CHANGING p_file. *---------------------------------------------------------------------* * START-OF-SELECTION *---------------------------------------------------------------------* START-OF-SELECTION. * upload the file PERFORM upload_file_to_table. * fill the database table PERFORM fill_db_table. *---------------------------------------------------------------------* * FORM upload_file_to_table *---------------------------------------------------------------------* * [+] Uploads the data of a local file to a table *---------------------------------------------------------------------* FORM upload_file_to_table. CALL FUNCTION 'UPLOAD' EXPORTING filename = p_file filetype = 'ASC' TABLES data_tab = i_file_data EXCEPTIONS OTHERS = 99. ENDFORM. "upload_file_to_table *----------------------------------------------------------------------* * FORM fill_db_table *----------------------------------------------------------------------* * [+] Splits the lines of input data * [+] Inserts the new data into the database table *----------------------------------------------------------------------* FORM fill_db_table. * Determine whether there is a header line in the file. * If so, we want to delete it. LOOP AT i_file_data INTO v_file_data. IF v_file_data+0(4) = 'SOrg'. DELETE i_file_data INDEX 1. ENDIF. EXIT. ENDLOOP. LOOP AT i_file_data INTO v_file_data. * Split the input line into the separate variables SPLIT v_file_data AT ',' INTO v_db_table-vkorg v_db_table-vkbur. * Insert the data into the table INSERT into Z_MYTABLE values v_db_table. * Output a status message for the row IF sy-subrc = 0. WRITE: / 'Added :', v_db_table-vkorg, v_db_table-vkbur. ELSE. WRITE: / 'Not Added:', v_db_table-vkorg, v_db_table-vkbur. ENDIF. ENDLOOP. ENDFORM. "fill_db_table *---------------------------------------------------------------------* * FORM ask_for_filename *---------------------------------------------------------------------* * [+] Asks the user to choose the output filename/directory via a * file chooser popup *---------------------------------------------------------------------* FORM ask_for_filename CHANGING p_fullpath TYPE rlgrap-filename. DATA: lv_title TYPE string, lv_filter TYPE string, lv_ext TYPE string, lv_rc TYPE i, lv_selfiles TYPE file_table, li_selfiles TYPE STANDARD TABLE OF file_table, lv_action TYPE i. * popup properties lv_title = 'Open File'. lv_filter = 'Comma-Separated (CSV) file (*.csv)|*.csv|'. lv_ext = 'csv'. * Ask the user for the filename CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = lv_title file_filter = lv_filter default_extension = lv_ext CHANGING file_table = li_selfiles user_action = lv_action rc = lv_rc EXCEPTIONS OTHERS = 99. IF sy-subrc = 0 AND lv_action = cl_gui_frontend_services=>action_ok. * set the value if the user clicked OK and there were no errors LOOP AT li_selfiles INTO lv_selfiles. p_fullpath = lv_selfiles-filename. EXIT. ENDLOOP. ENDIF. ENDFORM. "ask_for_filename