[ABAP] Excel-Daten mit XCO API (xco_cp_xlsx) lesen

TYPES: BEGIN OF ty_xl_sheet,
         col1 TYPE string,
         col2 TYPE string,
         col3 TYPE string,
       END OF ty_xl_sheet.

TYPES: ty_it_users TYPE STANDARD TABLE OF ty_xl_sheet WITH DEFAULT KEY.

TRY.

    DATA: lv_rc TYPE i.
    DATA: it_files TYPE filetable.
    DATA: lv_action TYPE i.

* FileOpen-Dialog aufrufen
    cl_gui_frontend_services=>file_open_dialog( EXPORTING file_filter = |xlsx (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING  file_table  = it_files
                                                          rc          = lv_rc
                                                          user_action = lv_action ).

    IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
      IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
        DATA: lv_filesize TYPE w3param-cont_len.
        DATA: lv_filetype TYPE w3param-cont_type.
        DATA: it_bin_data TYPE w3mimetabtype.

* Excel-Datei auf Appl. Server hochladen (binary)
        cl_gui_frontend_services=>gui_upload( EXPORTING filename   = |{ it_files[ 1 ]-filename }|
                                                        filetype   = 'BIN'
                                              IMPORTING filelength = lv_filesize
                                              CHANGING  data_tab   = it_bin_data ).

* solix -> xstring
        DATA(lv_xsts_xlsx) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

* Excel-Dokument
        DATA(o_xl) = xco_cp_xlsx=>document->for_file_content( lv_xsts_xlsx )->read_access( ).

* Sheet
        DATA(o_sheet) = o_xl->get_workbook( )->worksheet->at_position( 1 ).

        IF abap_true = o_sheet->exists( ).

* komplette Sheet auswählen
          DATA(o_sel_pattern) = xco_cp_xlsx_selection=>pattern_builder->simple_from_to( )->get_pattern( ).

          DATA: it_sheet_data TYPE ty_it_users.

* Daten aus Sheet als Strings in interne Tabelle schreiben
          DATA(o_result) = o_sheet->select( o_sel_pattern
                                          )->row_stream(
                                          )->operation->write_to( REF #( it_sheet_data )
                                          )->set_value_transformation( xco_cp_xlsx_read_access=>value_transformation->string_value
                                          )->execute( ).

          IF abap_true = o_result->succeeded.
* Datenausgabe
            cl_demo_output=>write_data( it_sheet_data ).

*             HTML-Code vom Demo-Output holen
            DATA(lv_html) = cl_demo_output=>get( ).

*             Daten im Inline-Browser im SAP-Fenster anzeigen
            cl_abap_browser=>show_html( EXPORTING title       = 'Excel'
                                                  html_string = lv_html
                                                  container   = cl_gui_container=>default_screen ).

*             cl_gui_container=>default_screen erzwingen
            WRITE: space.
          ENDIF.

        ELSE.

* Fehlerbehandlung
          LOOP AT o_result->messages INTO DATA(o_msg).
            WRITE: / o_msg->get_text( ).
          ENDLOOP.

        ENDIF.
      ENDIF.
    ENDIF.

  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.

Links