[ABAP] ABAP2XLSX: CSV-Datei schreiben

TRY.
* Daten holen
    SELECT * FROM sflight INTO TABLE @DATA(it_sflight).

* ABAP2XLSX-Objekt
    DATA: o_xl TYPE REF TO zcl_excel.

* Converter itab->ABAP2XLSX
    DATA(o_xl_conv) = NEW zcl_excel_converter( ).
    o_xl_conv->convert( EXPORTING it_table = it_sflight
                        CHANGING  co_excel = o_xl ).

* CSV-Writer-Objekt erzeugen
    DATA(o_csv) = NEW zcl_excel_writer_csv( ).
    o_csv->set_delimiter( ip_value = ';' ).
    o_csv->set_enclosure( ip_value = '''' ).
    o_csv->set_endofline( ip_value = cl_abap_char_utilities=>cr_lf ).
    o_csv->set_active_sheet_index( i_active_worksheet = 1 ).

* Excel-Writer-Objekt erzeugen
    DATA(o_writer) = CAST zif_excel_writer( o_csv ).
* Excel-Writer-Objekt->xstring
    DATA(lv_xstr) = o_writer->write_file( o_xl ).

* xstring -> solix
    DATA(it_solix_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xstr ).

* CSV-Daten lokal speichern (binär übertragen)
    cl_gui_frontend_services=>gui_download( EXPORTING filename     = 'Test.csv'
                                                      filetype     = 'BIN'
                                                      bin_filesize = xstrlen( lv_xstr )
                                            CHANGING  data_tab     = it_solix_data ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] ABAP2XLSX: XLSX-Datei schreiben

TRY.
* Daten holen
    SELECT * FROM sflight INTO TABLE @DATA(it_sflight).

* ABAP2XLSX-Objekt
    DATA: o_xl TYPE REF TO zcl_excel.

* Converter itab->ABAP2XLSX
    DATA(o_xl_conv) = NEW zcl_excel_converter( ).
    o_xl_conv->convert( EXPORTING it_table = it_sflight
                        CHANGING  co_excel = o_xl ).

* XLSX-Writer-Objekt erzeugen
    DATA(o_writer) = CAST zif_excel_writer( NEW zcl_excel_writer_huge_file( ) ).

* Excel-Writer-Objekt->xstring
    DATA(lv_xstr) = o_writer->write_file( o_xl ).

* xstring -> solix
    DATA(it_solix_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xstr ).

* CSV Daten lokal speichern (binär übertragen)
    cl_gui_frontend_services=>gui_download( EXPORTING filename     = 'Test.xlsx'
                                                      filetype     = 'BIN'
                                                      bin_filesize = xstrlen( lv_xstr )
                                            CHANGING  data_tab     = it_solix_data ).

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] DMS: Infos zu Dokumenteninfosätzen lesen

Variante 1 (JOIN)

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_type TYPE draw-dokar OBLIGATORY DEFAULT 'XYZ'.
* Dokumentnummer, mit Suchhilfe auf Tabelle DRAW (Dokumentinformationssatz)
PARAMETERS: p_num TYPE draw-doknr OBLIGATORY DEFAULT '10000000001' MATCHCODE OBJECT /plmb/dir_elm.
* Dokumentversion
PARAMETERS: p_ver TYPE draw-dokvr OBLIGATORY DEFAULT '00'.
* Teildokument
PARAMETERS: p_part TYPE draw-doktl OBLIGATORY DEFAULT '000'.

START-OF-SELECTION.

  SELECT FROM draw AS dw
    INNER JOIN drad AS dd ON ( dw~dokar EQ dd~dokar AND dw~doknr EQ dd~doknr AND dw~dokvr EQ dd~dokvr AND dw~doktl EQ dd~doktl )
    INNER JOIN drat AS dt ON ( dw~dokar EQ dt~dokar AND dw~doknr EQ dt~doknr AND dw~dokvr EQ dt~dokvr AND dw~doktl EQ dt~doktl )
    LEFT OUTER JOIN dms_doc2loio AS l ON ( dw~dokar EQ l~dokar AND dw~doknr EQ l~doknr AND dw~dokvr EQ l~dokvr AND dw~doktl EQ l~doktl )
    LEFT OUTER JOIN dms_doc_files AS f ON ( dw~dokar EQ f~dokar AND dw~doknr EQ f~doknr AND dw~dokvr EQ f~dokvr AND dw~doktl EQ f~doktl )
    LEFT OUTER JOIN dms_ph_cd1 AS ph ON ( ph~loio_id EQ l~lo_objid )
    LEFT OUTER JOIN dms_phf_cd1 AS pf ON ( pf~phio_id EQ ph~phio_id )
    LEFT OUTER JOIN dms_phio2file AS o ON ( o~file_id EQ ph~prop08 )
    FIELDS dw~dokar,                    " Dokumentart
           dw~doknr,                    " Dokumentnummer
           dw~dokvr,                    " Dokumentversion
           dw~doktl,                    " Teildokument
           dw~dwnam,                    " Name des zuständigen Sachbearbeiters
           dw~adatum,                   " Ausgabedatum des Dokuments
           dw~document_info_record_key, " Dokumentinfosatzschlüssel konkatenierter Form
           dd~dokob,                    " Verknüpftes SAP-Objekt
           dd~obzae,                    " Zähler Key
           dd~objky,                    " Objektkey Dokumentenverwaltung
           dt~dktxt,                    " Beschreibung
           l~lo_objid,                  " Log.Dokument
           f~dappl,                     " WS-Appl.
           f~filename,                  " Dateiname
           ph~phio_id,                  " Phys.Dokument
           ph~stor_cat,                 " Kategorie
           pf~mimetype,                 " MIME Typ
           pf~file_size,                " Größe
           pf~file_name,                " Komponentenname
           pf~file_type,                " Übertragungsart
           o~file_id,                   " DMS: GUID für Zuordnung PHIO - Dateiname
           o~filename AS file_path      " Original zu Dokument
    WHERE dw~dokar EQ @p_type
      AND dw~doknr EQ @p_num
      AND dw~dokvr EQ @p_ver
      AND dw~doktl EQ @p_part
    INTO TABLE @DATA(it_di).

  cl_demo_output=>write_data( it_di ).

* 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       = 'DMS'
                                        html_string = lv_html
                                        container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
  WRITE: space.

Variante 2 (CDS-View P_DOCUMENTINFORECORDATTACHMENT)

* Dokumentart (TA DC10, Tabelle TDWA)
PARAMETERS: p_type TYPE draw-dokar OBLIGATORY DEFAULT 'XYZ'.
* Dokumentnummer, mit Suchhilfe auf Tabelle DRAW (Dokumentinformationssatz)
PARAMETERS: p_num TYPE draw-doknr OBLIGATORY DEFAULT '10000000001' MATCHCODE OBJECT /plmb/dir_elm.
* Dokumentversion
PARAMETERS: p_ver TYPE draw-dokvr OBLIGATORY DEFAULT '00'.
* Teildokument
PARAMETERS: p_part TYPE draw-doktl OBLIGATORY DEFAULT '000'.

START-OF-SELECTION.

  SELECT FROM p_documentinforecordattachment
    FIELDS *
    WHERE documentinforecorddoctype EQ @p_type
      AND documentinforecorddocnumber EQ @p_num
      AND documentinforecorddocversion EQ @p_ver
      AND documentinforecorddocpart EQ @p_part
    INTO TABLE @DATA(it_di).

  TRY.
* SALV-Table
      DATA: o_salv TYPE REF TO cl_salv_table.

      cl_salv_table=>factory( IMPORTING r_salv_table = o_salv
                              CHANGING  t_table      = it_di ).

* Grundeinstellungen
      o_salv->get_functions( )->set_all( abap_true ).
      o_salv->get_columns( )->set_optimize( abap_true ).
      o_salv->get_display_settings( )->set_list_header( 'DMS' ).
      o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
      o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

* Spaltenüberschriften: technischer Name und Beschreibungstexte, Short Text und Medium Text leer lassen für Autosize
      LOOP AT o_salv->get_columns( )->get( ) ASSIGNING FIELD-SYMBOL(<c>).
        DATA(o_col) = <c>-r_column.
        o_col->set_short_text( || ).
        o_col->set_medium_text( || ).
        o_col->set_long_text( |{ o_col->get_long_text( ) }| ).
      ENDLOOP.

      o_salv->display( ).
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] Prüfen, ob ein bestimmer Festwert in einer Domäne vorhanden ist

* Überprüfung von Festwerten einer Domäne
CALL FUNCTION 'CHECK_DOMAIN_VALUES'
  EXPORTING
    domname       = 'CCCATEGORY'
    value         = 'P'
  EXCEPTIONS
    no_domname    = 1
    wrong_value   = 2
    dom_not_found = 3
    OTHERS        = 4.

IF sy-subrc = 0.
  WRITE: / 'Wert in Domäne vorhanden'.
ELSE.
  WRITE: / 'Wert in Domäne nicht vorhanden'.
ENDIF.

Links

[ABAP] Varianten eines Reports lesen

* Suchhilfe PROGNAME einbinden
PARAMETERS: p_report TYPE varid-report MATCHCODE OBJECT progname DEFAULT sy-repid.
PARAMETERS: p_langu TYPE t002-spras DEFAULT sy-langu.

START-OF-SELECTION.

* Varianten des Reports in den Tabellen VARID (Variantenkatalog) und VARIT (Variantentexte) suchen
* die gefundenen Tabelleninhalte werden in der Ausgabetabelle it_variants als getrennte
* Strukturen V und VT gespeichert
  SELECT FROM varid AS v
    INNER JOIN varit AS vt ON ( v~report EQ vt~report AND v~variant EQ vt~variant )
    FIELDS v~*, vt~*
    WHERE v~mlangu = @p_langu
      AND vt~langu = @p_langu
      AND v~report = @p_report
    INTO TABLE @DATA(it_variants).

  IF sy-subrc = 0.
* Hier Objekte für die Ausgabe hinzufügen
    cl_demo_output=>write_data( it_variants ).

* 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       = |Varianten des Reports { p_report }|
                                          html_string = lv_html
                                          container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
    WRITE: space.
  ELSE.
    WRITE: / |Keine Varianten zu Report { p_report } gefunden.|.
  ENDIF.

[ABAP] SE80: Tricks für den ABAP-Editor

Groß-/Kleinschreibung umschalten

  • Text markieren
  • toggle: STRG + K
  • upper case: STRG + U
  • lower case: STRG + L

Blockselektion mit Maus

  • Blockbeginn selektieren
  • Blockende mit ALT + Mausklick

Blockselektion mit Tasten

  • Horizontale Auswahl: SHIFT + CRSR
  • Vertikale Auswahl der darunter liegenden Zeilen: SHIFT + ALT + CRSR

Schnelle Typdefinition im Code

  • TYPES im ABAP-Editor eingeben
  • STRG + SPACE drücken
  • Doppelklick auf TYPES
  • Typnamen im Popup eingeben
  • Button „Preview TYPE“ klicken
  • gewünschte Felder wählen
  • Button “Weiter” klicken

ABAP-Code als Datei (*.abap) speichern

  • STRG + SHIFT + S

Links

[ABAP] SAP-Workdir ermitteln

Variante 1 (cl_gui_frontend_services)

PARAMETERS: p_path TYPE string OBLIGATORY LOWER CASE.

INITIALIZATION.

TRY.
  cl_gui_frontend_services=>get_sapgui_workdir( CHANGING sapworkdir = p_path ).

  cl_gui_cfw=>flush( ).
CATCH cx_root INTO DATA(e_txt).
  WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (IW_C_GET_SAPWORKDIR)

DATA: lvsapworkdir TYPE sdok_chtrd.
DATA: lverror_msg	TYPE iwerrormsg.

* SAP Workdir aus der Registry bestimmen (Temporär)
CALL FUNCTION 'IW_C_GET_SAPWORKDIR'
  IMPORTING
    sapworkdir = lvsapworkdir
    error_msg  = lverror_msg.

WRITE: / lvsapworkdir.
WRITE: / lverror_msg.

[ABAP] Alle SAP-Objekttypen + Texte auflisten

DATA: it_types_in TYPE STANDARD TABLE OF KO105 WITH DEFAULT KEY.
DATA: it_types_out TYPE STANDARD TABLE OF KO100 WITH DEFAULT KEY.

* Intern: Tabelle mit allen Objekttypen inklusive Texten
CALL FUNCTION 'TRINT_OBJECT_TABLE'
  EXPORTING
    iv_complete  = abap_true
  TABLES
    tt_types_in  = it_types_in
    tt_types_out = it_types_out.

cl_demo_output=>write_data( it_types_in ).
cl_demo_output=>write_data( it_types_out ).
* 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       = 'SAP-Objekttypen'
                                      html_string = lv_html
                                      container   = cl_gui_container=>default_screen ).

* cl_gui_container=>default_screen erzwingen
WRITE: space.