[ABAP] Desktop-Office-Integration (DOI) (Listausgabe / MS Excel inplace)

Vorteile

  • Einfacher unkomplizierter Zugriff auf MS Excel-Dateien

Nachteile

  • Office muss auf dem Clientrechner installiert sein, nicht zur Ausführung per Job geeignet
  • Einbettung in SAP Document Container Control: Kapselung von ActiveX / OLE2, daher rel. langsam
  • Interface unterstützt nur 9999 Zeilen und Spalten, da row und column vom Typ C(4)
  • kann alles besser über OLE2-Objekte manuell abgebildet werden -> mehr Freiheitsgrade
* Demoprogramme:
*   SAPRDEMO_DOI_BDS
*   SAPRDEMO_FORM_INTERFACE
*   SAPRDEMO_SPREADSHEET_INTERFACE
*   SAPRDEMO_MAILMERGE_INTERFACE
* Interface:
*   I_OI_DOCUMENT_PROXY

DATA: lv_has_activex TYPE abap_bool.

CALL FUNCTION 'GUI_HAS_ACTIVEX'
  IMPORTING
    return = lv_has_activex.

IF lv_has_activex = abap_true.
  DATA: lv_error TYPE REF TO i_oi_error.
  DATA: lv_size TYPE i.
  DATA: it_xl_upload TYPE umb_bds_content.
  DATA: o_control TYPE REF TO i_oi_container_control.
  DATA: o_document TYPE REF TO i_oi_document_proxy.
  DATA: o_spreadsheet TYPE REF TO i_oi_spreadsheet.

  cl_gui_frontend_services=>gui_upload( EXPORTING
                                          filename     = 'c:\temp\Excel.xlsx'
                                          filetype     = 'BIN'
                                          read_by_line = space
                                        IMPORTING
                                          filelength   = lv_size
                                        CHANGING
                                          data_tab     = it_xl_upload ).

* Erzeugt eine Container-Control-Instanz
  c_oi_container_control_creator=>get_container_control( IMPORTING
                                                           control = o_control
                                                           error   = lv_error ).

  IF lv_error->has_succeeded = abap_true.
* erzeugt und initializiert das Control
    o_control->init_control( EXPORTING
                               no_flush                 = abap_true
                               r3_application_name      = 'DOI Beispiel'
                               parent                   = cl_gui_container=>default_screen
                               inplace_enabled          = abap_true
                               inplace_show_toolbars    = abap_false
                               inplace_scroll_documents = abap_true
                               register_on_close_event  = abap_true
                               register_on_custom_event = abap_true
                             IMPORTING
                               error                 = lv_error ).

    IF lv_error->has_succeeded = abap_true.
* erzeugt eine Instanz für die Verwaltung eines Dokuments
      o_control->get_document_proxy( EXPORTING
                                       document_type  = soi_doctype_excel_sheet
                                       no_flush       = abap_true
                                     IMPORTING
                                       document_proxy = o_document
                                       error          = lv_error ).

      IF lv_error->has_succeeded = abap_true.
* Öffnen eines bereits existierenden Dokuments
        o_document->open_document_from_table( EXPORTING
                                                 document_size    = lv_size
                                                 document_table   = it_xl_upload
                                                 document_title   = 'DOI Beispiel'
                                                 open_inplace     = abap_true " SAP Inplace anzeigen oder extern in MS Excel
                                                 protect_document = ' '
                                               IMPORTING
                                                 error            = lv_error ).

        IF lv_error->has_succeeded = abap_true.
* liefert eine Interface-Referenz zurück
          o_document->get_spreadsheet_interface( IMPORTING
                                                   sheet_interface = o_spreadsheet
                                                   error           = lv_error ).

          IF lv_error->has_succeeded = abap_true.
            DATA: it_cells TYPE soi_generic_table.
            DATA: it_ranges TYPE soi_range_list.
            DATA: lv_retcode TYPE soi_ret_string.

            it_ranges = VALUE #( ( name = 'MyRange' ) ).

* Einfügen eines Bereiches ausgehend von den Dimensionen
            o_spreadsheet->insert_range_dim( EXPORTING
                                               name = it_ranges[ 1 ]-name
                                               top = 1
                                               left = 1
                                               rows = 10
                                               columns = 10
                                               no_flush = abap_true ).

* Holen der Namen und der Daten der Bereiche
            o_spreadsheet->get_ranges_data( EXPORTING
                                              all      = abap_false
                                            IMPORTING
                                              contents = it_cells
                                              error    = lv_error
                                              retcode  = lv_retcode
                                            CHANGING
                                              ranges   = it_ranges ).

            LOOP AT it_cells ASSIGNING FIELD-SYMBOL(<fs_cells>) GROUP BY <fs_cells>-row.
              DATA(lv_line) = ||.

              LOOP AT GROUP <fs_cells> ASSIGNING FIELD-SYMBOL(<fs_col>).
                IF lv_line IS INITIAL.
                  lv_line = |{ <fs_col>-value WIDTH = 20 }|.
                ELSE.
                  lv_line = |{ lv_line }{ <fs_col>-value WIDTH = 20 }|.
                ENDIF.
              ENDLOOP.

              WRITE: / <fs_cells>-row, lv_line.
            ENDLOOP.

* Schließen eines Dokuments in der Office-Anwendung
            o_document->close_document( IMPORTING error = lv_error ).
            IF lv_error->has_succeeded = abap_true.

* Schließen eines Visual-Basic-Dokuments
              o_document->close_activex_document( IMPORTING error = lv_error ).
              IF lv_error->has_succeeded = abap_true.

* Freigabe des für das Dokument reservierten Speichers
                o_document->release_document( IMPORTING error = lv_error ).

                IF lv_error->has_succeeded = abap_true.
                  FREE: o_document.
                ENDIF.
              ENDIF.
            ENDIF.

* baut das Control ab
            o_control->destroy_control( IMPORTING error = lv_error ).

            IF lv_error->has_succeeded = abap_true.
              FREE: o_control.
            ENDIF.

          ENDIF.
        ENDIF.
      ENDIF.
    ENDIF.
  ENDIF.
ENDIF.

Links