[ABAP] Verschlüsselung mit AES256

TRY.
* get sap codepage by external name
    DATA(lv_codepage_utf8) = cl_abap_codepage=>sap_codepage( 'utf-8' ).

* AES-256 Encryption Key
    DATA(lv_key_xstr) = cl_sec_sxml_writer=>generate_key( algorithm = cl_sec_sxml_writer=>co_aes256_algorithm ).

* Plaintext
    DATA(lv_plain_str) = |Testtext_123|.

* STRING --> XSTRING
    DATA(lv_plain_xstr) = cl_bcs_convert=>string_to_xstring( iv_codepage = CONV #( lv_codepage_utf8 )
                                                             iv_string   = lv_plain_str ).

* 16 Bit-Initialisierungsvector für CBC-(Cypher Block Chaining)-Modes der AES Encryption
    DATA(lv_iv_xstr) = CONV xstring( '00000000000000000000000000000000' ).

* Encrypt data with Initialization Vector

    DATA: lv_encrypted_xstr TYPE xstring.

    cl_sec_sxml_writer=>encrypt_iv( EXPORTING plaintext  = lv_plain_xstr
                                              key        = lv_key_xstr
                                              iv         = lv_iv_xstr
                                              algorithm  = cl_sec_sxml_writer=>co_aes256_algorithm_pem
                                    IMPORTING ciphertext = lv_encrypted_xstr ).


* Decrypt data
    DATA: lv_decrypted_xstr TYPE xstring.

    cl_sec_sxml_writer=>decrypt( EXPORTING ciphertext = lv_encrypted_xstr
                                           key        = lv_key_xstr
                                           algorithm  = cl_sec_sxml_writer=>co_aes256_algorithm_pem
                                 IMPORTING plaintext  = lv_decrypted_xstr ).

* XSTRING --> STRING
    DATA(lv_decrypted_str) = cl_bcs_convert=>xstring_to_string( iv_cp   = lv_codepage_utf8
                                                                iv_xstr = lv_decrypted_xstr ).

* Ausgabe
    WRITE: / 'Plain:', lv_plain_str.
    WRITE: / 'Encrypted xstring:', lv_encrypted_xstr.
    WRITE: / 'Decrypted xstring:', lv_decrypted_xstr.
    WRITE: / 'Decrypted:', lv_decrypted_str.

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

[ABAP] Datei-Download

Variante 1 (cl_gui_frontend_services)

TRY.
* beliebiger Stream
    DATA: lv_xdata TYPE xstring.
    
    ...
    
* xstring -> solix
    DATA(it_solix_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_xdata ).

* Daten lokal speichern
    cl_gui_frontend_services=>gui_download( EXPORTING
                                              filename     = |Bild.png|
                                              filetype     = 'BIN'
                                              bin_filesize = xstrlen( lv_xdata )
                                            CHANGING
                                              data_tab     = it_solix_data ).
    ENDIF.
  ENDIF.
CATCH cx_root INTO DATA(e_txt).
  WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (cl_gui_frontend_services)

* Inhalt einer internen Tabelle als Datei (CSV) auf den lokalen Rechner speichern:
* http://help.sap.com/saphelp_nw70ehp2/helpdata/de/c7/5ab8ec178c44a8aacd1dcac3460db8/content.htm  

* CSV-Daten
DATA: itab TYPE STANDARD TABLE OF string.
* Dateipfad + Dateiname
DATA: lv_file TYPE string VALUE 'c:\temp\test.txt'.

...

TRY.
  cl_gui_frontend_services=>gui_download( EXPORTING
                                            filename = lv_file
                                            filetype = 'DAT'  " mögliche Filetypen: BIN, ASC, DAT, DBF, WK1, VSS, IBM
                                            codepage = '4110' " UTF-8 Encoding: 4110, UTF-16 Big Endian : 4102, UTF-16 Little Endian : 4103.
                                          CHANGING
                                            data_tab = itab ).
      
    WRITE: / |Datei erfolgreich unter { lv_file } gespeichert.|.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

Variante 3 (cl_secxml_helper)

TRY.
* Daten als Stream
    DATA: lv_xdata TYPE xstring.

    ...

* File lokal speichern auf dem Frontend
    cl_secxml_helper=>save_file( EXPORTING default_file_name = ''
                                           initial_directory = ''
                                           window_title      = 'Datei speichern'
                                           bindata           = lv_xdata ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] XLSX-Datei mit Klasse cl_ehfnd_xlsx einlesen und in SALV-Grid anzeigen

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_bin_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

**********************************************************************
* XLSX Handling: Kapselt cl_xlsx_document
**********************************************************************
        DATA(o_excel) = cl_ehfnd_xlsx=>get_instance( ).

* XLSX Workbook (XString --> XML)
        DATA(o_doc) = o_excel->load_doc( iv_file_data = lv_bin_data ).

* XLSX Sheets des Workbooks holen
        DATA(it_sheets) = o_doc->get_sheets( ).

        IF lines( it_sheets ) > 0.
* XLSX Sheet: erste Sheet holen
          DATA(o_sheet) = o_doc->get_sheet_by_id( iv_sheet_id = 1 ).
          DATA(lv_sheet_name) = it_sheets[ 1 ]-name.

* max. Zeilenzahl der Sheet
          DATA(lv_max_rows) = o_sheet->get_last_row_number( ).
          DATA(lv_max_cols) = o_sheet->get_last_column_number_in_row( 1 ).

          IF lv_max_rows > 0 AND lv_max_cols > 0.
* Komponenten (Spalten) der Tabelle --> generische Stringtable bauen
            DATA(it_components) = VALUE cl_abap_structdescr=>component_table( ).

* Überschriften (Header) für ALV-Grid
            DATA(it_colnames) = VALUE stringtab( ).

            DO lv_max_cols TIMES.
* Spaltenbezeichner aus 1. Zeile (Header) der Excel-Tabelle holen
              DATA(lv_col_header) = o_sheet->get_cell_content( iv_row    = 1
                                                               iv_column = sy-index ).

* alle Vorkommen, die nicht [a-zA-Z0-9_] entsprechen, durch '_' ersetzen
              REPLACE ALL OCCURRENCES OF REGEX '([^\w]|[äöüÄÖÜß])+' IN lv_col_header WITH '_'.

* Tabelle mit Überschriften für ALV-Grid füllen
              APPEND lv_col_header TO it_colnames.

* Spalte vom Typ String mit generischem Namen zur Komponententabelle hinzufügen
              APPEND VALUE #( name   = |COL{ sy-index }|
                              type   = cl_abap_elemdescr=>get_string( )
                            ) TO it_components.
            ENDDO.

**********************************************************************
* generische interne Tabelle mit hilfe dynamischer Objekte erzeugen
**********************************************************************
* Strukturdeskriptor für Komponententabelle erzeugen
            DATA(o_struct_desc) = cl_abap_structdescr=>create( it_components ).

* Tabellendeskriptor erzeugen
            DATA(o_table_desc) = cl_abap_tabledescr=>create( p_line_type = o_struct_desc ).

* dynamisches Tabellenobjekt anhand des Tabellendeskriptors erstellen
            DATA: o_table TYPE REF TO data.
            CREATE DATA o_table TYPE HANDLE o_table_desc.

* Feldsymbol auf das Tabellenobjekt vom Typ STANDARD TABLE anlegen
            FIELD-SYMBOLS <table> TYPE STANDARD TABLE.
            ASSIGN o_table->* TO <table>.

* Inhalt (ohne Header) aus XLSX in interne Tabelle schreiben
            DATA(lv_row) = 2.

            DO lv_max_rows - 1 TIMES.
              DATA(lv_col) = 1.

* neue Ausgabezeile anfügen
              APPEND INITIAL LINE TO <table>.

* neue Ausgabezeile holen und Feldsymbol zuweisen
              DATA(lv_lc) = lines( <table> ).
              ASSIGN <table>[ lv_lc ] TO FIELD-SYMBOL(<row>).

              IF <row> IS ASSIGNED.
* für alle Spalten der Tabelle
                DO lv_max_cols TIMES.
* n-te Spalte <col> der akt. Tabellenzeile <row> ermitteln
                  ASSIGN COMPONENT lv_col OF STRUCTURE <row> TO FIELD-SYMBOL(<cell>).
                  IF <cell> IS ASSIGNED.
* Inhalt der akt. Zelle in die Zelle der internen Tabelle schreiben
                    <cell> = o_sheet->get_cell_content( iv_row    = lv_row
                                                        iv_column = lv_col ).


                  ENDIF.

                  lv_col = lv_col + 1.
                ENDDO.
              ENDIF.

              lv_row = lv_row + 1.
            ENDDO.

**********************************************************************
* Anzeige der gefüllten generischen Tabelle in einem SALV-Grid
**********************************************************************
            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      = <table> ).

*             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( CONV #( lv_sheet_name ) ).
                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(lv_idx) = sy-tabix.

                  DATA(o_col) = <c>-r_column.
                  o_col->set_short_text( || ).
                  o_col->set_medium_text( || ).
                  o_col->set_long_text( CONV #( it_colnames[ lv_idx ] ) ).
                ENDLOOP.

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

        ENDIF.

      ENDIF.

    ENDIF.

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

[ABAP] MS-Excel-Datei mit Hilfe der Klasse cl_ehfnd_xlsx lesen

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_bin_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

* XLSX Handling: Kapselt cl_xlsx_document
        DATA(o_excel) = cl_ehfnd_xlsx=>get_instance( ).

* XLSX Workbook (XString --> XML)
        DATA(o_doc) = o_excel->load_doc( iv_file_data = lv_bin_data ).

* XLSX Sheets des Workbooks holen
        DATA(it_sheets) = o_doc->get_sheets( ).

        LOOP AT it_sheets ASSIGNING FIELD-SYMBOL(<sheet>).
          WRITE: / <sheet>-name, <sheet>-sheet_id, <sheet>-rid.
        ENDLOOP.

        ULINE.

* XLSX Sheet: erste Sheet holen
        DATA(o_sheet) = o_doc->get_sheet_by_id( iv_sheet_id = 1 ).
* max. Zeilenzahl der Sheet
        DATA(lv_maxrow) = o_sheet->get_last_row_number( ).

        DATA(lv_row) = 1.

        DO lv_maxrow TIMES.
* max. Spaltenzahl der akt. Zeile
          DATA(lv_cols_in_row) = o_sheet->get_last_column_number_in_row( lv_row ).
          DATA(lv_col) = 1.

          DO lv_cols_in_row TIMES.
* Inhalt der akt. Zelle
            DATA(lv_value) = o_sheet->get_cell_content( EXPORTING iv_row    = lv_row
                                                                  iv_column = lv_col ).

            WRITE: lv_value.

            lv_col = lv_col + 1.
          ENDDO.

          lv_row = lv_row + 1.

          WRITE: /.
        ENDDO.

      ENDIF.
    ENDIF.

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

[ABAP] Beispiel für Verwendung der Klasse cl_xlsx_document

* Workbook
PARAMETERS: p_wb RADIOBUTTON GROUP grp DEFAULT 'X'.
* Sharedstrings
PARAMETERS: p_str RADIOBUTTON GROUP grp.
* Sheetpart
PARAMETERS: p_sh RADIOBUTTON GROUP grp.
* Tablepart
PARAMETERS: p_tab RADIOBUTTON GROUP grp.

START-OF-SELECTION.

  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_bin_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

**********************************************************************
* Excel-Objekt
**********************************************************************
          DATA(o_excel) = cl_xlsx_document=>load_document( lv_bin_data ).

          DATA(o_workbook) = o_excel->get_workbookpart( ).
          DATA(lv_workbook_xstr) = o_workbook->get_data( ).

          DATA(o_sharedstrings) = o_workbook->get_sharedstringspart( ).
          DATA(lv_sharedstrings_xstr) = o_sharedstrings->get_data( ).

          DATA(o_sheets) = o_workbook->get_worksheetparts( ).
          IF o_sheets->get_count( ) > 0.
            DATA(o_sheet) = CAST cl_xlsx_worksheetpart( o_sheets->get_part( 0 ) ).

            DATA(lv_sheet_xstr) = o_sheet->get_data( ).

            DATA(o_tables) = o_sheet->get_tableparts( ).
            IF o_tables->get_count( ) > 0.
              DATA(o_table) = CAST cl_xlsx_tablepart( o_tables->get_part( 0 ) ).
              DATA(lv_table_xstr) = o_table->get_data( ).
            ENDIF.
          ENDIF.

* XML Doc
          DATA(o_doc) = NEW cl_xml_document( ).

* xstring --> xml
          CASE abap_true.
            WHEN p_wb.
              o_doc->parse_xstring( lv_workbook_xstr ).
            WHEN p_str.
              o_doc->parse_xstring( lv_sharedstrings_xstr ).
            WHEN p_sh.
              o_doc->parse_xstring( lv_sheet_xstr ).
            WHEN p_tab.
              o_doc->parse_xstring( lv_table_xstr ).
          ENDCASE.

* Inhalt des XML-Documents in string wandeln
          o_doc->render_2_string( EXPORTING pretty_print = abap_true
                                  IMPORTING retcode      = DATA(lv_rc2)
                                            size         = DATA(lv_size2)
                                            stream       = DATA(lv_xml) ).

          IF lv_rc2 = 0.
* XML-String anzeigen
            cl_soap_xml_helper=>xml_show( sdoc  = lv_xml
                                          html  = abap_true
                                          title = 'XML Anzeige' ).
          ENDIF.

        ENDIF.
      ENDIF.

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

[ABAP] Transportauftrag als ZIP-Datei speichern

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.

  lbl1 = 'Transport-Auftrag:'.

START-OF-SELECTION.
  TRY.
      DATA: lv_req TYPE trwbo_request.

* (Nach-)Lesen von Einzelbestandteilen eines Auftrags
      CALL FUNCTION 'TR_READ_REQUEST'
        EXPORTING
          iv_read_e070     = abap_true
          iv_read_e07t     = abap_true
          iv_trkorr        = p_trkorr
        CHANGING
          cs_request       = lv_req
        EXCEPTIONS
          error_occured    = 1
          no_authorization = 2
          OTHERS           = 3.

      IF sy-subrc = 0.
*D Änderbar
*L Änderbar, geschützt
*O Freigabe gestartet
*R Freigegeben
*N Freigegeben (Importschutz für reparierte Objekte aktiv)
        IF lv_req-h-trstatus = 'R'.

          DATA: lv_trans_cofile_dir TYPE stpa-file.
          DATA: lv_trans_data_dir TYPE stpa-file.

* intern: Pfade der Transportverzeichnisse
          CALL FUNCTION 'TRINT_FI_GET_DIRECTORY_NAMES'
            EXPORTING
              iv_dir_type          = 'T'
            IMPORTING
              ev_data_dir          = lv_trans_data_dir
              ev_cofiles_dir       = lv_trans_cofile_dir
            EXCEPTIONS
              get_dir_trans_failed = 1
              build_path_failed    = 2
              OTHERS               = 3.

          IF sy-subrc = 0.
* Name
            DATA(lv_syst) = |{ p_trkorr(3) }|.
            DATA(lv_num) = |{ p_trkorr+4 }|.
            DATA(lv_name) = |{ lv_num }.{ lv_syst }|.

* Cofile lesen
            DATA: lv_xstr_cofile TYPE xstring.
            DATA(lv_cofile) = |{ lv_trans_cofile_dir }/K{ lv_name }|.

            OPEN DATASET lv_cofile FOR INPUT IN BINARY MODE.
            READ DATASET lv_cofile INTO lv_xstr_cofile.
            CLOSE DATASET lv_cofile.

* Data lesen
            DATA: lv_xstr_data TYPE xstring.
            DATA(lv_data) = |{ lv_trans_data_dir }/R{ lv_name }|.

            OPEN DATASET lv_data FOR INPUT IN BINARY MODE.
            READ DATASET lv_data INTO lv_xstr_data.
            CLOSE DATASET lv_data.

* Zip
            DATA(o_zip) = NEW cl_abap_zip( ).

            o_zip->add( name    = |K{ lv_name }|
                        content = lv_xstr_cofile ).

            o_zip->add( name    = |R{ lv_name }|
                        content = lv_xstr_data ).

            DATA(lv_xstr_zip) = o_zip->save( ).

* SOLIX
            DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_xstr_zip ).

            DATA: lv_action TYPE i.
            DATA: lv_filename TYPE string.
            DATA: lv_fullpath TYPE string.
            DATA: lv_path TYPE string.

            DATA(lv_default) = |{ lv_req-h-as4text }_{ p_trkorr }|.

* alle Vorkommen, die nicht [a-zA-Z0-9_] entsprechen, durch '_' ersetzen
            REPLACE ALL OCCURRENCES OF REGEX '[^\w]+' IN lv_default WITH '_'.

* Save-Dialog
            cl_gui_frontend_services=>file_save_dialog( EXPORTING default_file_name = lv_default
                                                                  default_extension = 'zip'
                                                                  file_filter       = |{ cl_gui_frontend_services=>filetype_all }|
                                                        CHANGING  filename          = lv_filename
                                                                  path              = lv_path
                                                                  fullpath          = lv_fullpath
                                                                  user_action       = lv_action ).

            IF lv_action EQ cl_gui_frontend_services=>action_ok.

* Datei lokal speichern
              cl_gui_frontend_services=>gui_download( EXPORTING filename     = lv_fullpath
                                                                filetype     = 'BIN'
                                                                bin_filesize = xstrlen( lv_xstr_zip )
                                                      CHANGING  data_tab     = it_raw_data ).
            ENDIF.

          ELSE.
            WRITE: / |Systemverzeichnisse nicht gefunden.|.
          ENDIF.
        ELSE.
          WRITE: / |Auftrag { p_trkorr } nicht frei gegeben.|.
        ENDIF.

      ELSE.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO DATA(lv_msg).
        WRITE: / lv_msg.
      ENDIF.
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] Binärdaten (HEX) in eine lokale Datei speichern

* HEX-Daten (Beispiel)
DATA(it_solix) = VALUE solix_tab(
                   ( line = '7639BD' )
                   ( line = '67326F' )
                 ).

TRY.
    DATA: lv_action TYPE i.
    DATA: lv_filename TYPE string.
    DATA: lv_fullpath TYPE string.
    DATA: lv_path TYPE string.

* Save-Dialog
    cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                  default_file_name = 'Data.bin'
                                                  default_extension = 'bin'
                                                  file_filter       = |Binärdaten (*.bin)\|*.bin\|{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING
                                                  filename          = lv_filename
                                                  path              = lv_path
                                                  fullpath          = lv_fullpath
                                                  user_action       = lv_action ).

    IF lv_action EQ cl_gui_frontend_services=>action_ok.
* XSTRING für Längenbestimung erzeugen
      DATA(lv_xstring) = cl_bcs_convert=>solix_to_xstring( it_solix ).

* Datei lokal speichern
      cl_gui_frontend_services=>gui_download( EXPORTING
                                                filename     = lv_fullpath
                                                filetype     = 'BIN'
                                                bin_filesize = xstrlen( lv_xstring )
                                              CHANGING
                                                data_tab     = it_solix ).

    ENDIF.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

[ABAP] Komplexbeispiel: Binärdaten Screenshot mit GZIP komprimieren, in Base64-String wandeln und zurück (CL_ABAP_GZIP, SSFC_BASE64_ENCODE, SSFC_BASE64_DECODE)

TRY.
    DATA: lv_mime_type TYPE string.
    DATA: lv_image_bytes TYPE xstring.

* Ein paar Bytes holen: Screenshot aufnehmen
    cl_gui_frontend_services=>get_screenshot( IMPORTING
                                                mime_type_str = lv_mime_type
                                                image         = lv_image_bytes ).

    DATA: lv_xstr_gzip TYPE xstring.

* Binärdaten mit GZIP komprimieren
    cl_abap_gzip=>compress_binary( EXPORTING raw_in   = lv_image_bytes
                                   IMPORTING gzip_out = lv_xstr_gzip ).

    DATA(lv_base64_data) = ||.

* Base64 codieren
    CALL FUNCTION 'SSFC_BASE64_ENCODE'
      EXPORTING
        bindata                  = lv_xstr_gzip
      IMPORTING
        b64data                  = lv_base64_data
      EXCEPTIONS
        ssf_krn_error            = 1
        ssf_krn_noop             = 2
        ssf_krn_nomemory         = 3
        ssf_krn_opinv            = 4
        ssf_krn_input_data_error = 5
        ssf_krn_invalid_par      = 6
        ssf_krn_invalid_parlen   = 7
        OTHERS                   = 8.
    IF sy-subrc = 0.

* Base64-String in der Listenausgabe darstellen
      DATA: it_text80 TYPE STANDARD TABLE OF text80 WITH DEFAULT KEY.
* formatierte Ausgabe (80 Zeichen Breite)
      CALL FUNCTION 'SWA_STRING_TO_TABLE'
        EXPORTING
          character_string = lv_base64_data
        IMPORTING
          character_table  = it_text80.

      LOOP AT it_text80 ASSIGNING FIELD-SYMBOL(<t>).
        WRITE: / <t>.
      ENDLOOP.

      DATA: lv_xstr_dec TYPE xstring.

* Base64 decodieren
      CALL FUNCTION 'SSFC_BASE64_DECODE'
        EXPORTING
          b64data                  = lv_base64_data
        IMPORTING
          bindata                  = lv_xstr_dec
        EXCEPTIONS
          ssf_krn_error            = 1
          ssf_krn_noop             = 2
          ssf_krn_nomemory         = 3
          ssf_krn_opinv            = 4
          ssf_krn_input_data_error = 5
          ssf_krn_invalid_par      = 6
          ssf_krn_invalid_parlen   = 7
          OTHERS                   = 8.

      IF sy-subrc = 0.
        DATA: lv_pic_bytes TYPE xstring.

* Binärdaten mit GZIP dekomprimieren
        cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = lv_xstr_dec IMPORTING raw_out = lv_pic_bytes ).

* freier Custom-Container innerhalb der Listenansicht
        DATA(o_cnt) = NEW cl_gui_custom_container( container_name = ''
                                                   repid          = 'SAPMSSY0'
                                                   dynnr          = '0120' ).

* Position des Containers setzen
        o_cnt->set_top( 50 ).
        o_cnt->set_left( 50 ).
        o_cnt->set_width( 640 ).
        o_cnt->set_height( 240 ).

* Bild über die URL laden und anzeigen
        DATA(o_pic) = NEW cl_gui_picture( parent = o_cnt ).
        o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_stretch ).

* xstring -> solix
        DATA(it_bin_data) = cl_bcs_convert=>xstring_to_solix( lv_pic_bytes ).

        DATA: lv_url TYPE swk_url.

* temporäre URL für das Bild erzeugen
        CALL FUNCTION 'DP_CREATE_URL'
          EXPORTING
            type                 = 'image/jpeg' " https://wiki.selfhtml.org/wiki/Referenz:MIME-Typen
            subtype              = ''
          TABLES
            data                 = it_bin_data
          CHANGING
            url                  = lv_url
          EXCEPTIONS
            dp_invalid_parameter = 1
            dp_error_put_table   = 2
            dp_error_general     = 3
            OTHERS               = 4.

* Bild von URL laden
        o_pic->load_picture_from_url_async( lv_url ).

      ENDIF.
    ENDIF.

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

[ABAP] SAP Smart Forms: XSF Utilities – BDS Grafik (Formulargrafik) holen und anzeigen

TRY.
* Holt Formulargrafiken, die mit der SE78 eingerichtet wurden
    DATA(lv_xstr) = cl_ssf_xsf_utilities=>get_bds_graphic_as_bmp( p_object = 'GRAPHICS'
                                                                  p_name   = 'ENJOY'
                                                                  p_id     = 'BMAP'
                                                                  p_btype  = 'BCOL' ). " BMON	- Rasterbild schwarz-weiß, BCOL	- Rasterbild Farbe

* freier Custom-Container innerhalb der Listenansicht
    DATA(o_cnt) = NEW cl_gui_custom_container( container_name = ''
                                               repid          = 'SAPMSSY0'
                                               dynnr          = '0120' ).

* Position des Containers setzen
    o_cnt->set_top( 50 ).
    o_cnt->set_left( 50 ).
    o_cnt->set_width( 640 ).
    o_cnt->set_height( 240 ).

* Bild über die URL laden und anzeigen
    DATA(o_pic) = NEW cl_gui_picture( parent = o_cnt ).
    o_pic->set_display_mode( display_mode = cl_gui_picture=>display_mode_stretch ).

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

    DATA: lv_url TYPE swk_url.

* temporäre URL für das Bild erzeugen
    CALL FUNCTION 'DP_CREATE_URL'
      EXPORTING
        type                 = 'image/jpeg' " https://wiki.selfhtml.org/wiki/Referenz:MIME-Typen
        subtype              = ''
      TABLES
        data                 = it_bin_data
      CHANGING
        url                  = lv_url
      EXCEPTIONS
        dp_invalid_parameter = 1
        dp_error_put_table   = 2
        dp_error_general     = 3
        OTHERS               = 4.

* Bild von URL laden
    o_pic->load_picture_from_url_async( lv_url ).

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

[ABAP] Interne Tabelle als Excel-Datei (*.xlsx) speichern (cl_fdt_xl_spreadsheet)

* Mit Hilfe der Klasse cl_fdt_xl_spreadsheet können über XML-Transformationen
* Daten aus SAP in XLSX-Dateien geschrieben werden
TRY.
* Testdaten lesen
    SELECT * FROM t001 INTO TABLE @DATA(it_t001).

    IF sy-subrc = 0.
* Header erzeugen
      DATA: it_columns TYPE if_fdt_doc_spreadsheet=>t_column.
      DATA: lv_head TYPE t001.
      DATA(o_desc) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( lv_head ) ).

      LOOP AT o_desc->get_components( ) ASSIGNING FIELD-SYMBOL(<c>).
        IF <c> IS ASSIGNED.
          IF <c>-type->kind = cl_abap_typedescr=>kind_elem.
            APPEND VALUE #( id           = sy-tabix
                            name         = <c>-name
                            display_name = <c>-name
                            is_result    = abap_true
                            type         = <c>-type ) TO it_columns.
          ENDIF.
        ENDIF.
      ENDLOOP.

* itab + header -> XML -> xstring
* Achtung: Speicherintensiv und rel. langsam! Es sollten keine großen Datenmengen verarbeitet werden.
      DATA(lv_bin_data) = cl_fdt_xl_spreadsheet=>if_fdt_doc_spreadsheet~create_document( columns      = it_columns " optional
                                                                                         itab         = REF #( it_t001 )
                                                                                         iv_call_type = if_fdt_doc_spreadsheet=>gc_call_dec_table ).
      IF xstrlen( lv_bin_data ) > 0.
        DATA: lv_action TYPE i.
        DATA: lv_filename TYPE string.
        DATA: lv_fullpath TYPE string.
        DATA: lv_path TYPE string.

* Save-Dialog
        cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                      default_file_name = 'Excel.xlsx'
                                                      default_extension = 'xlsx'
                                                      file_filter       = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                    CHANGING
                                                      filename          = lv_filename
                                                      path              = lv_path
                                                      fullpath          = lv_fullpath
                                                      user_action       = lv_action ).

        IF lv_action EQ cl_gui_frontend_services=>action_ok.
* XSTRING -> SOLIX (RAW)
          DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_bin_data ).

* Datei lokal speichern
          cl_gui_frontend_services=>gui_download( EXPORTING
                                                    filename     = lv_fullpath
                                                    filetype     = 'BIN'
                                                    bin_filesize = xstrlen( lv_bin_data )
                                                  CHANGING
                                                    data_tab     = it_raw_data ).


        ENDIF.
      ENDIF.
    ENDIF.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.