Sap Gui Upload to Upload to Al11 Dialog
ABAP code to upload XLSX file to SAP using ABAP. The method shown in the blog volition piece of work in SAP GUI and Webgui (program launched from Fiori Launchpad). This approach will also work with OData.
Lawmaking is based on form CL_FDT_XL_SPREADSHEET which can be instantiated using the file name and excel file in XSTRING variable. This processing of file every bit XSTRING is particularly useful in case of Fiori Apps, OData CREATE_STREAM method pass uploaded file already in XSTRING which tin be simply passed on to form CL_FDT_XL_SPREADSHEET constructor.
After instantiating the class get list of worksheets in the file using IF_FDT_DOC_SPREADSHEET~GET_WORKSHEET_NAMES. Method GET_WORKSHEET_NAMES returns internal table. In nigh of the case, you would read the first worksheet proper name and call method IF_FDT_DOC_SPREADSHEET~GET_ITAB_FROM_WORKSHEET with worksheet proper name to get information in a dynamic internal table. Yet, if y'all take data over multiple worksheets then y'all would have to call method GET_ITAB_FROM_WORKSHEET in the loop for each worksheet.
There are some role modules (TEXT_CONVERT_XLS_TO_SAP) available in SAP which can assistance y'all read data from XLS/XLSX file but these are based on Office Integration and requires MS Office installed on your PC. Also, this Part Integration solution (forth with a range of other features) is not supported in WebGUI. In other words, these FMs will only piece of work when yous are running your plan in SAP WebGUI.
I have structured the logic in standalone SE38 program so you can re-create-paste and have it ready for testing in your system. Manifestly, you would have to suit the code to use it in your awarding but you lot get the thought of how it works.
*---------------------------------------------------------------------* * Study ZPW_EXCELUPLOAD *---------------------------------------------------------------------* Written report zpw_excelupload. FIELD-SYMBOLS : <gt_data> TYPE STANDARD Tabular array . Pick-SCREEN BEGIN OF Cake b1 WITH FRAME . PARAMETERS : p_file TYPE ibipparms-path OBLIGATORY, p_ncol Type i OBLIGATORY DEFAULT 10. Selection-SCREEN END OF Cake b1 . *--------------------------------------------------------------------* * at pick screen *--------------------------------------------------------------------* AT Pick-SCREEN ON VALUE-REQUEST FOR p_file. Information: lv_rc Blazon i. DATA: lt_file_table Type filetable, ls_file_table TYPE file_table. CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = 'Select a file' CHANGING file_table = lt_file_table rc = lv_rc. IF sy-subrc = 0. READ Tabular array lt_file_table INTO ls_file_table Alphabetize ane. p_file = ls_file_table-filename. ENDIF. Kickoff-OF-SELECTION . PERFORM read_file . PERFORM process_file. *---------------------------------------------------------------------* * Grade READ_FILE *---------------------------------------------------------------------* FORM read_file . Information : lv_filename Blazon string, lt_records TYPE solix_tab, lv_headerxstring TYPE xstring, lv_filelength Type i. lv_filename = p_file. CALL Part 'GUI_UPLOAD' EXPORTING filename = lv_filename filetype = 'BIN' IMPORTING filelength = lv_filelength header = lv_headerxstring TABLES data_tab = lt_records EXCEPTIONS file_open_error = ane file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = iv invalid_type = five no_authority = 6 unknown_error = seven bad_data_format = 8 header_not_allowed = 9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 OTHERS = 17. "convert binary data to xstring "if y'all are using cl_fdt_xl_spreadsheet in odata then skips this step "every bit excel file will already be in xstring CALL Part 'SCMS_BINARY_TO_XSTRING' EXPORTING input_length = lv_filelength IMPORTING buffer = lv_headerxstring TABLES binary_tab = lt_records EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. "Implement suitable error handling hither ENDIF. Data : lo_excel_ref Type REF TO cl_fdt_xl_spreadsheet . Endeavour . lo_excel_ref = NEW cl_fdt_xl_spreadsheet( document_name = lv_filename xdocument = lv_headerxstring ) . Grab cx_fdt_excel_core. "Implement suitable mistake treatment here ENDTRY . "Get List of Worksheets lo_excel_ref->if_fdt_doc_spreadsheet~get_worksheet_names( IMPORTING worksheet_names = DATA(lt_worksheets) ). IF NOT lt_worksheets IS INITIAL. READ Table lt_worksheets INTO Information(lv_woksheetname) INDEX 1. DATA(lo_data_ref) = lo_excel_ref->if_fdt_doc_spreadsheet~get_itab_from_worksheet( lv_woksheetname ). "now you accept excel piece of work sail data in dyanmic internal table ASSIGN lo_data_ref->* TO <gt_data>. ENDIF. ENDFORM. *---------------------------------------------------------------------* * Form PROCESS_FILE *---------------------------------------------------------------------* FORM process_file . DATA : lv_numberofcolumns Blazon i, lv_date_string TYPE string, lv_target_date_field TYPE datum. FIELD-SYMBOLS : <ls_data> Blazon whatsoever, <lv_field> Type any. "yous could notice out number of columns dynamically from table <gt_data> lv_numberofcolumns = p_ncol . LOOP AT <gt_data> ASSIGNING <ls_data> FROM 2 . "processing columns Practice lv_numberofcolumns TIMES. ASSIGN COMPONENT sy-index OF STRUCTURE <ls_data> TO <lv_field> . IF sy-subrc = 0 . Example sy-index . * when one . * when 2 . WHEN 10 . lv_date_string = <lv_field> . PERFORM date_convert USING lv_date_string Irresolute lv_target_date_field . WRITE lv_target_date_field . WHEN OTHERS. WRITE : <lv_field> . ENDCASE . ENDIF. ENDDO . NEW-LINE . ENDLOOP . ENDFORM. *---------------------------------------------------------------------* * Class DATE_CONVERT *---------------------------------------------------------------------* Grade date_convert USING iv_date_string TYPE string Irresolute cv_date Blazon datum . Data: lv_convert_date(10) Type c. lv_convert_date = iv_date_string . "engagement format YYYY/MM/DD Discover REGEX '^\d{four}[/|-]\d{1,2}[/|-]\d{ane,2}$' IN lv_convert_date. IF sy-subrc = 0. CALL FUNCTION '/SAPDMC/LSM_DATE_CONVERT' EXPORTING date_in = lv_convert_date date_format_in = 'DYMD' to_output_format = ' ' to_internal_format = 'X' IMPORTING date_out = lv_convert_date EXCEPTIONS illegal_date = ane illegal_date_format = two no_user_date_format = 3 OTHERS = four. ELSE. " engagement format DD/MM/YYYY Find REGEX '^\d{1,two}[/|-]\d{i,2}[/|-]\d{4}$' IN lv_convert_date. IF sy-subrc = 0. CALL FUNCTION '/SAPDMC/LSM_DATE_CONVERT' EXPORTING date_in = lv_convert_date date_format_in = 'DDMY' to_output_format = ' ' to_internal_format = 'Ten' IMPORTING date_out = lv_convert_date EXCEPTIONS illegal_date = i illegal_date_format = ii no_user_date_format = three OTHERS = four. ENDIF. ENDIF. IF sy-subrc = 0. cv_date = lv_convert_date . ENDIF. ENDFORM .
Uploaded this excel file.
Beneath results from SAP GUI and WebGUI.
Source: https://www.samplecodeabap.com/how-to-upload-excel-to-sapusing-abap/