Preface – This post is part of the ABAP Programs series.
To perform ABAP excel upload using ABAP report, we need to call a FM ‘TEXT_CONVERT_XLS_TO_SAP’. This FM converts the excel data to ABAP data. Now we can store the same in our internal table and later in our database table. The only mandatory requirement is that the table fields should match the header of excel in a synchronous way, else wrong data will be saved.
Table of Contents
Many times we need to upload an excel data from our local storage on PC and insert those data into our ABAP tables. This can be easily achieved using ABAP Function Module ‘TEXT_CONVERT_XLS_TO_SAP’. In the given program we convert our excel data into ABAP data and insert those data in our ztable.
TYPE-POOLS truxs. PARAMETERS p_file TYPE rlgrap-filename. TYPES : BEGIN OF t_tab, filename TYPE char100sm, END OF t_tab. DATA : t_upload TYPE STANDARD TABLE OF t_tab, wa_upload TYPE t_tab, it_type TYPE truxs_t_text_data. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. CALL FUNCTION 'F4_FILENAME' EXPORTING field_name = 'p_file' IMPORTING file_name = p_file. START-OF-SELECTION. CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' EXPORTING i_tab_raw_data = it_type i_filename = p_file TABLES i_tab_converted_data = t_upload EXCEPTIONS conversion_failed = 1 OTHERS = 2. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 . ENDIF. END-OF-SELECTION. DATA : ls_cc_impl TYPE zBarry_upload, lt_cc_impl TYPE STANDARD TABLE OF zBarry_upload. LOOP AT t_upload INTO wa_upload. ls_cc_impl-mandt = sy-mandt. ls_cc_impl-filename = wa_upload-filename. APPEND ls_cc_impl TO lt_cc_impl. ENDLOOP. MODIFY zBarry_upload FROM TABLE lt_cc_impl.
Table and Excel
In the above program, we have implemented the following steps:
- Initially, we have defined TYPE-POOLS truxs. TYPE-POOLS have predefined methods that can be reused in multiple programs. Here we are using truxs, that is used later as truxs_t_text_data, a data type.
- Now, we have define parameters: p_file. This will be used to take the file location as input.
- Later, we have defined variables: t_tab which is a table type, t_upload, wa_upload and it_type.
- Now, we have called a function module “F4_FILENAME” to process the file upload.
- Then, we have called another function module ‘TEXT_CONVERT_XLS_TO_SAP’ to get the ABAP form of data into an internal table.
- Once we have our data in an internal table, then we can easily append it to our data base table using loop.