This commit is contained in:
Ivan Femia 2017-11-15 18:25:33 +01:00
parent 90523b9853
commit 333d505a1f
3 changed files with 226 additions and 143 deletions

View File

@ -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: <ls_line> TYPE data.
FIELD-SYMBOLS: <lv_value> 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 <ls_line>.
IF sy-subrc <> 0 OR <ls_line> 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 <ls_line> TO <lv_value>.
IF sy-subrc <> 0 OR <lv_value> 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 <ls_line>.
APPEND INITIAL LINE TO et_table ASSIGNING <ls_line>.
IF sy-subrc <> 0 OR <ls_line> 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 <ls_line> TO <lv_value>.
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> = 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: <ls_line> TYPE data.
FIELD-SYMBOLS: <lv_value> 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 <ls_line>.
IF sy-subrc <> 0 OR <ls_line> 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 <ls_line> TO <lv_value>.
IF sy-subrc <> 0 OR <lv_value> 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 <ls_line>.
APPEND INITIAL LINE TO et_table ASSIGNING <ls_line>.
IF sy-subrc <> 0 OR <ls_line> 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 <ls_line> TO <lv_value>.
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> = 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.

View File

@ -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( ).

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<abapGit version="v1.0.0" serializer="LCL_OBJECT_PROG" serializer_version="v1.0.0">
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<PROGDIR>
<NAME>ZDEMO_EXCEL43</NAME>
<STATE>A</STATE>
<VARCL>X</VARCL>
<SUBC>1</SUBC>
<RLOAD>E</RLOAD>
<FIXPT>X</FIXPT>
<UCCHECK>X</UCCHECK>
</PROGDIR>
<TPOOL>
<item>
<ID>R</ID>
<ENTRY>Demo 43 GET_TABLE</ENTRY>
<LENGTH>17</LENGTH>
</item>
</TPOOL>
</asx:values>
</asx:abap>
</abapGit>