ALV-Grid und Excel

Dieses Coding demonstriert, wie man in die ALV-Funktionalität eingreifen kann. In diesem Fall wird der Konstruktor überschrieben, um beim Export der Tabelle den Inhalt der ALV-Tabelle manipulieren zu können.

CLASS lcl_alv_grid DEFINITION INHERITING FROM cl_gui_alv_grid.
  PUBLIC SECTION.
    METHODS
*      *—————————————————————*
*      *     Konstruktur                                               *
*      *  Wird überschrieben, da zusätzliche Funktionalität benötigt!  *
*      *—————————————————————*
       : constructor
              IMPORTING
                  i_shellstyle        TYPE i
                                      OPTIONAL
                  i_lifetime          TYPE i
                                      OPTIONAL
                  i_parent            TYPE REF TO cl_gui_container
                  i_appl_events       TYPE char01
                                      OPTIONAL
                  i_parentdbg         TYPE REF TO cl_gui_container
                                      OPTIONAL
                  i_applogparent      TYPE REF TO cl_gui_container
                                      OPTIONAL
                  i_graphicsparent    TYPE REF TO cl_gui_container
                                      OPTIONAL
                  i_use_variant_class TYPE c
                                      OPTIONAL
                  i_name              TYPE string
                                      OPTIONAL
               EXCEPTIONS
                  error_cntl_create
                  error_cntl_init
                  error_cntl_link
                  error_dp_create

*      *—————————————————————*
*      *  Erweiterung der Toolbar um einen Button “Export -> Excel”    *
*      *—————————————————————*

       , excel_toolbar        FOR EVENT toolbar
                              OF cl_gui_alv_grid
                              IMPORTING e_object
                                        e_interactive

*      *—————————————————————*
*      *  Auswerten des Funktionscodes bei Klick auf Button            *
*      *—————————————————————*

       , excel_user_command   FOR EVENT user_command
                              OF cl_gui_alv_grid
                              IMPORTING e_ucomm.

    TYPE-POOLS: kkblo.

  PRIVATE SECTION.
    METHODS
*      *—————————————————————*
*      *  Daten nach MS Excel exportieren                              *
*      *—————————————————————*

       : start_excel.
ENDCLASS.

*———————————————————————*
*       CLASS lcl_alv_grid
*———————————————————————*
CLASS lcl_alv_grid IMPLEMENTATION.

  METHOD constructor.
*   Konstruktor der Superklasse aufrufen
    CALL METHOD super->constructor
         EXPORTING
             i_shellstyle        = i_shellstyle
             i_lifetime          = i_lifetime
             i_parent            = i_parent
             i_appl_events       = i_appl_events
             i_parentdbg         = i_parentdbg
             i_applogparent      = i_applogparent
             i_graphicsparent    = i_graphicsparent
             i_use_variant_class = i_use_variant_class
             i_name              = i_name.

*   Event-Behandler setzen
    SET HANDLER me->excel_toolbar      FOR me.
    SET HANDLER me->excel_user_command FOR me.

  ENDMETHOD.

  METHOD excel_toolbar.
    DATA
    : ls_toolbar TYPE stb_button
    .

*…Seperator
    ls_toolbar-function  = ‘DUMMY’.
    ls_toolbar-butn_type = ‘3’.
    APPEND ls_toolbar TO e_object->mt_toolbar.

*… neuer Button “SORT_ASC”
    ls_toolbar-function  = ‘EXCEL’.
    ls_toolbar-icon      = ‘@J2@’.
    ls_toolbar-butn_type = ‘0’.
    ls_toolbar-disabled  = space.
    ls_toolbar-quickinfo = ‘Excel’.
    APPEND ls_toolbar TO e_object->mt_toolbar.

*…Seperator
    ls_toolbar-function  = ‘DUMMY’.
    ls_toolbar-butn_type = ‘3’.
    APPEND ls_toolbar TO e_object->mt_toolbar.

  ENDMETHOD.                    “toolbar

  METHOD excel_user_command.
    IF e_ucomm = ‘EXCEL’.
*     Button “Excel-Export” geklickt
      CALL METHOD start_excel.
    ENDIF.
  ENDMETHOD.                    “user_command

  METHOD start_excel.

    DATA
*         Feldkatalog/WA für ALV
        : lt_lv_fcat    TYPE lvc_t_fcat
        , ls_lv_fcat    TYPE lvc_s_fcat

*         Layout des ALV Grids
        , ls_lv_layout  TYPE lvc_s_layo

*         Name der Anzeigetabelle
        , l_kk_tabname  TYPE kkblo_tabname

*         Layout für Excel-Export
        , ls_kk_layo    TYPE kkblo_layout

*         Feldkatalog/WA für Excel-Export
        , lt_kk_fcat    TYPE kkblo_t_fieldcat
        , ls_kk_fcat    TYPE kkblo_fieldcat.

    FIELD-SYMBOLS
*         Zeiger auf die Datentabelle
        : <lt_outtab>   TYPE STANDARD TABLE.

*   CL_GUI_ALV_GRID=>MT_OUTTAB ist eine Datenreferenz und
*     muss zunächst dereferenziert werden.

    ASSIGN me->mt_outtab->* TO <lt_outtab>.

*   Aktuellen Feldkatalog von Frontend holen
    CALL METHOD me->get_frontend_fieldcatalog
         IMPORTING
            et_fieldcatalog = lt_lv_fcat.

*   Aktuelles Layout vom Frontend holen.
    CALL METHOD me->get_frontend_layout
         IMPORTING
            es_layout       = ls_lv_layout.

*   Layout umschieben
    MOVE-CORRESPONDING ls_lv_layout TO ls_kk_layo.

*   Feldkatalog umschieben
    LOOP AT lt_lv_fcat INTO ls_lv_fcat.
      MOVE-CORRESPONDING ls_lv_fcat TO ls_kk_fcat.
      APPEND ls_kk_fcat TO lt_kk_fcat.
    ENDLOOP.

*   Tabellenname setzen (obligatorischer Parameter des
*     Bausteins ALV_XXL_CALL
    l_kk_tabname = ls_lv_fcat-tabname.

*   Flag: wenn SY-MSGID nach dem Baustein-Aufruf gesetzt ist
*         haben wir eine vernünftige Nachricht.

    CLEAR sy-msgid.

*   Excel-Export starten
    CALL FUNCTION ‘ALV_XXL_CALL’
         EXPORTING
              i_tabname           = l_kk_tabname
              is_layout           = ls_kk_layo
              it_fieldcat         = lt_kk_fcat
         TABLES
              it_outtab           = <lt_outtab>
         EXCEPTIONS
              fatal_error         = 1
              no_display_possible = 2
              OTHERS              = 3.
    IF sy-subrc <> 0.
      IF NOT sy-msgid IS INITIAL.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ELSE.
      ENDIF.
    ENDIF.


  ENDMETHOD.

ENDCLASS.

Vielen Dank an Haubi!!

 

Enno Wulff
Letzte Artikel von Enno Wulff (Alle anzeigen)

Leave a Comment