From 333d505a1f5777610b5f904b884dcc80be6afa1b Mon Sep 17 00:00:00 2001 From: Ivan Femia Date: Wed, 15 Nov 2017 18:25:33 +0100 Subject: [PATCH] fix #505 --- src/zcl_excel_worksheet.clas.abap | 287 +++++++++++++++--------------- src/zdemo_excel43.prog.abap | 59 ++++++ src/zdemo_excel43.prog.xml | 23 +++ 3 files changed, 226 insertions(+), 143 deletions(-) create mode 100644 src/zdemo_excel43.prog.abap create mode 100644 src/zdemo_excel43.prog.xml diff --git a/src/zcl_excel_worksheet.clas.abap b/src/zcl_excel_worksheet.clas.abap index cf0dffb..afbeb03 100644 --- a/src/zcl_excel_worksheet.clas.abap +++ b/src/zcl_excel_worksheet.clas.abap @@ -474,7 +474,7 @@ public section. et_table TYPE STANDARD TABLE RAISING zcx_excel. - + *"* protected components of class ZCL_EXCEL_WORKSHEET *"* do not include other source files here!!! *"* protected components of class ZCL_EXCEL_WORKSHEET @@ -4164,6 +4164,149 @@ method GET_TABCOLOR. endmethod. + METHOD get_table. +*--------------------------------------------------------------------* +* Comment D. Rauchenstein +* With this method, we get a fully functional Excel Upload, which solves +* a few issues of the other excel upload tools +* ZBCABA_ALSM_EXCEL_UPLOAD_EXT: Reads only up to 50 signs per Cell, Limit +* in row-Numbers. Other have Limitations of Lines, or you are not able +* to ignore filters or choosing the right tab. +* +* To get a fully functional XLSX Upload, you can use it e.g. with method +* CL_EXCEL_READER_2007->ZIF_EXCEL_READER~LOAD_FILE() +*--------------------------------------------------------------------* + + FIELD-SYMBOLS: TYPE data. + FIELD-SYMBOLS: TYPE data. + + DATA lv_actual_row TYPE int4. + DATA lv_actual_col TYPE int4. + DATA lv_errormessage TYPE string. + DATA lv_max_col TYPE zexcel_cell_column. + DATA lv_max_row TYPE int4. + DATA lv_delta_col TYPE int4. + DATA lv_value TYPE zexcel_cell_value. + DATA lv_rc TYPE sysubrc. + + + lv_max_col = me->get_highest_column( ). + lv_max_row = me->get_highest_row( ). + +*--------------------------------------------------------------------* +* The row counter begins with 1 and should be corrected with the skips +*--------------------------------------------------------------------* + lv_actual_row = iv_skipped_rows + 1. + lv_actual_col = iv_skipped_cols + 1. + + + TRY. +*--------------------------------------------------------------------* +* Check if we the basic features are possible with given "any table" +*--------------------------------------------------------------------* + APPEND INITIAL LINE TO et_table ASSIGNING . + IF sy-subrc <> 0 OR IS NOT ASSIGNED. + + lv_errormessage = 'Error at inserting new Line to internal Table'(002). + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + + ELSE. + lv_delta_col = lv_max_col - iv_skipped_cols. + ASSIGN COMPONENT lv_delta_col OF STRUCTURE TO . + IF sy-subrc <> 0 OR IS NOT ASSIGNED. + lv_errormessage = 'Internal table has less columns than excel'(003). + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + ELSE. +*--------------------------------------------------------------------* +*now we are ready for handle the table data +*--------------------------------------------------------------------* + REFRESH et_table. +*--------------------------------------------------------------------* +* Handle each Row until end on right side +*--------------------------------------------------------------------* + WHILE lv_actual_row <= lv_max_row . + +*--------------------------------------------------------------------* +* Handle each Column until end on bottom +* First step is to step back on first column +*--------------------------------------------------------------------* + lv_actual_col = iv_skipped_cols + 1. + + UNASSIGN . + APPEND INITIAL LINE TO et_table ASSIGNING . + IF sy-subrc <> 0 OR IS NOT ASSIGNED. + lv_errormessage = 'Error at inserting new Line to internal Table'(002). + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + ENDIF. + WHILE lv_actual_col <= lv_max_col. + + lv_delta_col = lv_actual_col - iv_skipped_cols. + ASSIGN COMPONENT lv_delta_col OF STRUCTURE TO . + IF sy-subrc <> 0. + lv_errormessage = |{ 'Error at assigning field (Col:'(004) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + ENDIF. + + me->get_cell( + EXPORTING + ip_column = lv_actual_col " Cell Column + ip_row = lv_actual_row " Cell Row + IMPORTING + ep_value = lv_value " Cell Value + ep_rc = lv_rc " Return Value of ABAP Statements + ). + IF lv_rc <> 0 + AND lv_rc <> 4. "No found error means, zero/no value in cell + lv_errormessage = |{ 'Error at reading field value (Col:'(007) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + ENDIF. + + = lv_value. +* CATCH zcx_excel. " + ADD 1 TO lv_actual_col. + ENDWHILE. + ADD 1 TO lv_actual_row. + ENDWHILE. + ENDIF. + + + ENDIF. + + CATCH cx_sy_assign_cast_illegal_cast. + lv_errormessage = |{ 'Error at assigning field (Col:'(004) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + CATCH cx_sy_assign_cast_unknown_type. + lv_errormessage = |{ 'Error at assigning field (Col:'(004) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + CATCH cx_sy_assign_out_of_range. + lv_errormessage = 'Internal table has less columns than excel'(003). + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + CATCH cx_sy_conversion_error. + lv_errormessage = |{ 'Error at converting field value (Col:'(006) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. + RAISE EXCEPTION TYPE zcx_excel + EXPORTING + error = lv_errormessage. + + ENDTRY. + ENDMETHOD. + + method GET_TABLES_ITERATOR. eo_iterator = tables->if_object_collection~get_iterator( ). endmethod. @@ -5242,146 +5385,4 @@ method ZIF_EXCEL_SHEET_VBA_PROJECT~SET_CODENAME. method ZIF_EXCEL_SHEET_VBA_PROJECT~SET_CODENAME_PR. me->zif_excel_sheet_vba_project~codename_pr = ip_codename_pr. endmethod. - - METHOD get_table. -*--------------------------------------------------------------------* -* Comment D. Rauchenstein -* With this method, we get a fully functional Excel Upload, which solves -* a few issues of the other excel upload tools -* ZBCABA_ALSM_EXCEL_UPLOAD_EXT: Reads only up to 50 signs per Cell, Limit -* in row-Numbers. Other have Limitations of Lines, or you are not able -* to ignore filters or choosing the right tab. -* -* To get a fully functional XLSX Upload, you can use it e.g. with method -* CL_EXCEL_READER_2007->ZIF_EXCEL_READER~LOAD_FILE() -*--------------------------------------------------------------------* - - FIELD-SYMBOLS: TYPE data. - FIELD-SYMBOLS: TYPE data. - - DATA lv_actual_row TYPE int4. - DATA lv_actual_col TYPE int4. - DATA lv_errormessage TYPE string. - - - -*try - DATA(lv_max_col) = me->get_highest_column( ). - DATA(lv_max_row) = me->get_highest_row( ). - -*--------------------------------------------------------------------* -* The row counter begins with 1 and should be corrected with the skips -*--------------------------------------------------------------------* - lv_actual_row = iv_skipped_rows + 1. - lv_actual_col = iv_skipped_cols + 1. - - - TRY. -*--------------------------------------------------------------------* -* Check if we the basic features are possible with given "any table" -*--------------------------------------------------------------------* - APPEND INITIAL LINE TO et_table ASSIGNING . - IF sy-subrc <> 0 OR IS NOT ASSIGNED. - - lv_errormessage = 'Error at inserting new Line to internal Table'(002). - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - - ELSE. - - ASSIGN COMPONENT ( lv_max_col - iv_skipped_cols ) OF STRUCTURE TO . - IF sy-subrc <> 0 OR IS NOT ASSIGNED. - lv_errormessage = 'Internal table has less columns than excel'(003). - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - - ELSE. -*--------------------------------------------------------------------* -*now we are ready for handle the table data -*--------------------------------------------------------------------* - - REFRESH et_table. -*--------------------------------------------------------------------* -* Handle each Row until end on right side -*--------------------------------------------------------------------* - WHILE lv_actual_row <= lv_max_row . - -*--------------------------------------------------------------------* -* Handle each Column until end on bottom -* First step is to step back on first column -*--------------------------------------------------------------------* - lv_actual_col = iv_skipped_cols + 1. - - UNASSIGN . - APPEND INITIAL LINE TO et_table ASSIGNING . - IF sy-subrc <> 0 OR IS NOT ASSIGNED. - lv_errormessage = 'Error at inserting new Line to internal Table'(002). - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - ENDIF. - WHILE lv_actual_col <= lv_max_col. - - - ASSIGN COMPONENT ( lv_actual_col - iv_skipped_cols ) OF STRUCTURE TO . - IF sy-subrc <> 0. - lv_errormessage = |{ 'Error at assigning field (Col:'(004) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - ENDIF. - - me->get_cell( - EXPORTING - ip_column = lv_actual_col " Cell Column - ip_row = lv_actual_row " Cell Row - IMPORTING - ep_value = DATA(lv_value) " Cell Value - ep_rc = DATA(lv_rc) " Return Value of ABAP Statements - ). - IF lv_rc <> 0 - and lv_rc <> 4. "No found error means, zero/no value in cell - lv_errormessage = |{ 'Error at reading field value (Col:'(007) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - ENDIF. - - = lv_value. -* CATCH zcx_excel. " - ADD 1 TO lv_actual_col. - ENDWHILE. - ADD 1 TO lv_actual_row. - ENDWHILE. - ENDIF. - - - ENDIF. - - CATCH cx_sy_assign_cast_illegal_cast. - lv_errormessage = |{ 'Error at assigning field (Col:'(004) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - CATCH cx_sy_assign_cast_unknown_type. - lv_errormessage = |{ 'Error at assigning field (Col:'(004) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - CATCH cx_sy_assign_out_of_range. - lv_errormessage = 'Internal table has less columns than excel'(003). - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - CATCH cx_sy_conversion_error. - lv_errormessage = |{ 'Error at converting field value (Col:'(006) } { lv_actual_col } { ' Row:'(005) } { lv_actual_row }|. - RAISE EXCEPTION TYPE zcx_excel - EXPORTING - error = lv_errormessage. - - ENDTRY. - ENDMETHOD. - ENDCLASS. diff --git a/src/zdemo_excel43.prog.abap b/src/zdemo_excel43.prog.abap new file mode 100644 index 0000000..b608374 --- /dev/null +++ b/src/zdemo_excel43.prog.abap @@ -0,0 +1,59 @@ +*&---------------------------------------------------------------------* +*& Report ZDEMO_EXCEL43 +*& +*&---------------------------------------------------------------------* +*& +*& +*&---------------------------------------------------------------------* + +REPORT zdemo_excel43. + +" +"Locally created Structure, which should be equal to the excels structure +" +TYPES: BEGIN OF lty_excel_s, + dummy TYPE dummy. +TYPES: END OF lty_excel_s. + +DATA lt_tab TYPE TABLE OF lty_excel_s. +DATA: lt_filetable TYPE filetable, + ls_filetable TYPE file_table. +DATA lv_subrc TYPE i. +DATA: lo_excel TYPE REF TO zcl_excel, + lo_reader TYPE REF TO zif_excel_reader, + lo_worksheet TYPE REF TO zcl_excel_worksheet, + lo_salv TYPE REF TO cl_salv_table. + +" +"Ask User to choose a path +" +cl_gui_frontend_services=>file_open_dialog( EXPORTING window_title = 'Excel selection' + file_filter = '*.xlsx' + multiselection = abap_false + CHANGING file_table = lt_filetable " Tabelle, die selektierte Dateien enthält + rc = lv_subrc + EXCEPTIONS file_open_dialog_failed = 1 + cntl_error = 2 + error_no_gui = 3 + not_supported_by_gui = 4 + OTHERS = 5 ). +IF sy-subrc <> 0. + MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno + WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. +ELSE. + CREATE OBJECT lo_reader TYPE zcl_excel_reader_2007. + TRY. + LOOP AT lt_filetable INTO ls_filetable. + lo_excel = lo_reader->load_file( ls_filetable-filename ). + lo_worksheet = lo_excel->get_worksheet_by_index( iv_index = 1 ). + lo_worksheet->get_table( IMPORTING et_table = lt_tab ). + ENDLOOP. + ENDTRY. +ENDIF. +" +"Do the presentation stuff +" + +cl_salv_table=>factory( IMPORTING r_salv_table = lo_salv + CHANGING t_table = lt_tab ). +lo_salv->display( ). diff --git a/src/zdemo_excel43.prog.xml b/src/zdemo_excel43.prog.xml new file mode 100644 index 0000000..93619a3 --- /dev/null +++ b/src/zdemo_excel43.prog.xml @@ -0,0 +1,23 @@ + + + + + + ZDEMO_EXCEL43 + A + X + 1 + E + X + X + + + + R + Demo 43 GET_TABLE + 17 + + + + +