Class ZCL_CONVERT_EXCEL_TO_ITAB
class ZCL_CONVERT_EXCEL_TO_ITAB definition
public
final
create public .
public section.
class-methods GET_ITAB
importing
!FILENAME type STRING
!WORKSHEET type STRING optional
!BEGIN_COL type I default 1
!BEGIN_ROW type I default 1
changing
!ITAB type ANY TABLE
exceptions
FILE_OPEN_ERROR
FILE_READ_ERROR
NO_BATCH
GUI_REFUSE_FILETRANSFER
INVALID_TYPE
NO_AUTHORITY
UNKNOWN_ERROR
BAD_DATA_FORMAT
HEADER_NOT_ALLOWED
SEPARATOR_NOT_ALLOWED
HEADER_TOO_LONG
UNKNOWN_DP_ERROR
ACCESS_DENIED
DP_OUT_OF_MEMORY
DISK_FULL
DP_TIMEOUT
OTHERS_EXC
CONV_BIN_TO_XSTRING_FAILED
SHEET_NOT_FOUND
LOAD_DOCUMENT_FAILED .
class-methods GET_FILENAME
returning
value(FILENAME) type STRING .
class-methods GET_FILE_TEXT
returning
value(FILENAME) type STRING .
class-methods GET_FILE_EXCEL
returning
value(FILENAME) type STRING .
private section.
types:
t_worksheet_names TYPE STANDARD TABLE OF string .
class-methods GET_ACTIVE_SHEET
importing
!XDOCUMENT type XSTRING
!WORKSHEETS type T_WORKSHEET_NAMES
returning
value(ACTIVESHEET) type STRING
exceptions
LOAD_DOCUMENT_FAILED .
GET_ITAB Static Method Public Transfer data from Excel worksheet to Internal Table GET_FILENAME Static Method Public GET_FILE_TEXT Static Method Public GET_FILE_EXCEL Static Method Public GET_ACTIVE_SHEET Static Method Private Get activesheet (opening sheet) |
GET_ITAB
FILENAME Importing Type STRING WORKSHEET Importing Type STRING BEGIN_COL Importing Type I 1 BEGIN_ROW Importing Type I 1 ITAB Changing Type ANY TABLE |
FILE_OPEN_ERROR FILE_READ_ERROR NO_BATCH GUI_REFUSE_FILETRANSFER INVALID_TYPE NO_AUTHORITY UNKNOWN_ERROR BAD_DATA_FORMAT HEADER_NOT_ALLOWED SEPARATOR_NOT_ALLOWED HEADER_TOO_LONG UNKNOWN_DP_ERROR ACCESS_DENIED DP_OUT_OF_MEMORY DISK_FULL DP_TIMEOUT OTHERS_EXC CONV_BIN_TO_XSTRING_FAILED SHEET_NOT_FOUND LOAD_DOCUMENT_FAILED |
METHOD get_itab. FIELD-SYMBOLS: lw_filename = filename. CALL FUNCTION ‘TRINT_FILE_GET_EXTENSION’ IF lw_ext = ‘XLS’. CALL FUNCTION ‘GUI_UPLOAD’ “Convert binary data to xstring lo_excel_ref = NEW cl_fdt_xl_spreadsheet( “Get List of Worksheets IF NOT lt_worksheets IS INITIAL. IF lw_woksheetname IS NOT INITIAL. CREATE DATA lo_itab LIKE LINE OF itab. “Transfer data from Excel work sheet to ITAB LOOP AT <ft_data> ASSIGNING FIELD–SYMBOL(<fs_data>) FROM begin_row. DO lw_num_of_col TIMES. ASSIGN COMPONENT sy–index OF STRUCTURE <fs_data> TO FIELD–SYMBOL(<fw_data>). UNASSIGN: ADD 1 TO lw_index_itab. INSERT <fs_itab> INTO TABLE itab. |
GET_FILENAME
METHOD get_filename. DATA __t_filetab TYPE filetable. DATA __rc TYPE i. CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = ‘Select File’ “default_filename = file_filter = ‘(*.xlsx)|*.xlsx|(*.txt)|*.txt’ multiselection = ‘ ‘ CHANGING file_table = __t_filetab rc = __rc EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy–subrc NE 0. MESSAGE ID sy–msgid TYPE ‘E’ NUMBER sy–msgno DISPLAY LIKE ‘S’. ENDIF.READ TABLE __t_filetab INTO DATA(__file) INDEX 1. IF sy–subrc EQ 0. filename = __file–filename. ENDIF. ENDMETHOD. |
GET_FILE_TEXT
METHOD get_file_text. DATA __t_filetab TYPE filetable. DATA __rc TYPE i. CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = ‘Select File’ “default_filename = file_filter = ‘(*.txt)|*.txt’ multiselection = ‘ ‘ CHANGING file_table = __t_filetab rc = __rc EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy–subrc NE 0. MESSAGE ID sy–msgid TYPE ‘E’ NUMBER sy–msgno DISPLAY LIKE ‘S’. ENDIF.READ TABLE __t_filetab INTO DATA(__file) INDEX 1. IF sy–subrc EQ 0. filename = __file–filename. ENDIF. ENDMETHOD. |
GET_FILE_EXCEL
method GET_FILE_EXCEL. DATA __t_filetab TYPE filetable. DATA __rc TYPE i. CALL METHOD cl_gui_frontend_services=>file_open_dialog EXPORTING window_title = ‘Select File’ “default_filename = file_filter = ‘(*.xlsx)|*.xlsx’ multiselection = ‘ ‘ CHANGING file_table = __t_filetab rc = __rc EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy–subrc NE 0. MESSAGE ID sy–msgid TYPE ‘E’ NUMBER sy–msgno DISPLAY LIKE ‘S’. ENDIF.READ TABLE __t_filetab INTO DATA(__file) INDEX 1. IF sy–subrc EQ 0. filename = __file–filename. ENDIF. endmethod. |
GET_ACTIVE_SHEET
METHOD get_active_sheet. DATA: lo_xlsx_document TYPE REF TO cl_xlsx_document.TRY. lo_xlsx_document = cl_xlsx_document=>load_document( iv_data = xdocument ). CATCH cx_root INTO DATA(msg). MESSAGE msg->get_text( ) TYPE ‘E’ RAISING load_document_failed. ENDTRY.DATA(lo_zip) = NEW cl_abap_zip( ). lo_zip->load( xdocument ).DATA(ls_files) = VALUE #( lo_zip->files[ name = ‘xl/workbook.xml’ ] OPTIONAL ). IF ls_files IS NOT INITIAL. lo_zip->get( EXPORTING name = ls_files–name IMPORTING content = DATA(ls_workbook_xml) ).DATA(lo_xml_document) = NEW cl_xml_document( ). lo_xml_document->parse_xstring( stream = ls_workbook_xml ). DATA(lw_workbookview) = lo_xml_document->find_node( ‘workbookView’ ). DATA(lw_activetab) = lo_xml_document->get_node_attribute( node = lw_workbookview name = ‘activeTab’ ).IF lw_activetab IS INITIAL. activesheet = VALUE #( worksheets[ 1 ] OPTIONAL ). ELSE. ADD 1 TO lw_activetab. activesheet = VALUE #( worksheets[ lw_activetab ] OPTIONAL ). ENDIF. ENDIF. ENDMETHOD. |
Usage:
DATA: BEGIN OF gt_excel_data OCCURS 0, col01 TYPE char100, col02 TYPE char100, col03 TYPE char100, col04 TYPE char100, col05 TYPE char100, col06 TYPE char100, col07 TYPE char100, col08 TYPE char100, col09 TYPE char100, col10 TYPE char100, col11 TYPE char100, col12 TYPE char100, col13 TYPE char100, col14 TYPE char100, col15 TYPE char100, col16 TYPE char100, col17 TYPE char100, col18 TYPE char100, col19 TYPE char100, col20 TYPE char100, col21 TYPE char100, col22 TYPE char100, col23 TYPE char100, col24 TYPE char100, col25 TYPE char100, col26 TYPE char100, col27 TYPE char100, col28 TYPE char100, col29 TYPE char100, col30 TYPE char100, col31 TYPE char100, col32 TYPE char100, col33 TYPE char100, col34 TYPE char100, col35 TYPE char100, col36 TYPE char100, col37 TYPE char100, col38 TYPE char100, col39 TYPE char100, col40 TYPE char100, col41 TYPE char100, col42 TYPE char100, col43 TYPE char100, col44 TYPE char100, col45 TYPE char100, col46 TYPE char100, col47 TYPE char100, col48 TYPE char100, col49 TYPE char100, col50 TYPE char100, col51 TYPE char100, col52 TYPE char100, col53 TYPE char100, col54 TYPE char100, col55 TYPE char100, col56 TYPE char100, col57 TYPE char100, col58 TYPE char100, col59 TYPE char100, col60 TYPE char100, col61 TYPE char100, col62 TYPE char100, col63 TYPE char100, col64 TYPE char100, col65 TYPE char100, col66 TYPE char100, col67 TYPE char100, col68 TYPE char100, col69 TYPE char100, col70 TYPE char100, END OF gt_excel_data,TYPES: gty_excel_data LIKE gt_excel_data. DATA gt_excel_data_tmp TYPE STANDARD TABLE OF gty_excel_data.DATA: gv_sheet_name TYPE soi_field_name.CLEAR: gv_sheet_name.CASE ‘X’. WHEN p_store. gv_sheet_name = ‘Plant’. WHEN p_char. gv_sheet_name = ‘Characteristic’. WHEN OTHERS. ENDCASE.CONCATENATE ‘FILE://’ p_file INTO gw_document_url. DATA: lv_inplace TYPE c VALUE ‘X’, lv_readonly, lv_file TYPE string, lv_sheet TYPE string. lv_file = p_file. |