[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] CSS-Code als ABAP-INCLUDE speichern und laden

Über die Nutzung von Includes ist es möglich beliebigen Code im SAP abzulegen und darauf zuzugreifen. Folgende Schritte sind notwendig:

  • SE80 -> neues INCLUDE anlegen (Bsp.: ZCSSINCLUDE)
  • CSS-Code im INCLUDE eintragen
  • Aktvieren! (Ja, das funktioniert.)
  • CSS-Code (INCLUDE) im ABAP-Code lesen
* Name des INCLUDEs mit dem CSS-Code
CONSTANTS: co_cssinclude TYPE char12 VALUE 'ZCSSINCLUDE'.
* String für CSS-Daten
DATA: lv_css TYPE string.
	
* Prüfen, ob INCLUDE im System (TADIR) aktiv vorhanden
SELECT SINGLE obj_name
  INTO @DATA(lv_obj_name)
  FROM tadir
  WHERE obj_name = @co_w3css
    AND pgmid    = 'R3TR'
    AND object   = 'PROG'.

IF sy-subrc = 0.

* Stringtab für Code
  DATA: it_incl_code TYPE stringtab.

* Code des INCLUDEs lesen
  READ REPORT co_w3css INTO it_incl_code.

  IF sy-subrc = 0.
* String-Tabelle mit CSS-Code in String wandeln, Trennzeichen ist CRLF
    lv_css = REDUCE string( INIT s = ||
                            FOR <s> IN it_incl_code
                            NEXT s = COND #( WHEN s IS INITIAL THEN |{ <s> }| ELSE |{ s }{ cl_abap_char_utilities=>cr_lf }{ <s> }| ) ).
  ENDIF.
ENDIF.

* HTML mit CSS-INCLUDE
DATA(lv_html) = '<!DOCTYPE html>' &&
                '<html>' &&
                '<title>Lagermaterialkatalog</title>' &&
                '<meta name="viewport" content="width=device-width, initial-scale=1">' &&
                '<style>' && lv_css && '</style>' &&
                '<body>' &&
                '</body>' &&
                '</html>'.

[ABAP] Daten aus der Zwischenablage (Clipboard) importieren

* Datentyp mit ausreichend Länge für Zeilen der Clipboard-Daten
TYPES: ty_char4096 TYPE c LENGTH 4096.

DATA: it_clipboarddata TYPE STANDARD TABLE OF ty_char4096.
DATA: it_splitelements TYPE STANDARD TABLE OF string.
DATA: lv_line_count TYPE i.

cl_gui_frontend_services=>clipboard_import( IMPORTING
                                              data                 = it_clipboarddata
                                              length               = lv_line_count
                                            EXCEPTIONS
                                              cntl_error           = 1
                                              error_no_gui         = 2
                                              not_supported_by_gui = 3
                                              OTHERS               = 4 ).

IF sy-subrc = 0.
  WRITE: / 'Zeilen:', lv_line_count.

  SKIP.

* Zeilen der Clipboarddaten durchloopen
  LOOP AT it_clipboarddata ASSIGNING FIELD-SYMBOL(<fs_line>).

* die Zeilen am TAB spaltenweise aufsplitten
    SPLIT <fs_line> AT cl_abap_char_utilities=>horizontal_tab INTO TABLE it_splitelements.

* Zeilendaten verarbeiten -> neu zusammenbauen
    DATA(lv_line) = ||.

    LOOP AT it_splitelements ASSIGNING FIELD-SYMBOL(<fs_elem>).
      IF lv_line IS INITIAL.
        lv_line = <fs_elem>.
      ELSE.
        lv_line = |{ lv_line }\|{ <fs_elem> }|.
      ENDIF.
    ENDLOOP.

* Ausgabe
    WRITE: / lv_line.
  ENDLOOP.
ENDIF.

[ABAP] OLE2: VBScript-Code über MSScriptControl ausführen

* http://www.thescarms.com/VBasic/Scripting.aspx
* https://de.wikibooks.org/wiki/Visual_Basic_Script_(VBS):_Einf%C3%BChrung
* https://wiki.scn.sap.com/wiki/display/Snippets/How+to+use+VBScript+in+ABAP
* https://www.experts-exchange.com/questions/26443935/Login-to-SAP-with-VBScript.html

* VBS-Code mit Beispielfunktionen einfügen
* Option Explicit --> Alle Variablen im VBS müssen mit Dim deklariert werden
DATA(it_vbs_code) = VALUE stringtab( ( |Option Explicit| )
                                     ( || )
* Funktion mit Übergabeparametern
                                     ( |Function Add(val1, val2)| )
                                     ( |  Add = val1 + val2| )
                                     ( |End Function| )
                                     ( || )
* Funktion zur Anzeige einer Messagebox
                                     ( |Function ShowMsgBox(InfoText)| )
                                     ( |  Dim msg| )
                                     ( |  msg = MsgBox(InfoText, vbOkOnly, "Info")| )
                                     ( |End Function| )
                                     ( || )
* Sub (keine Parameter) mit Beispiel für Dateiausführung
                                     ( |Sub Run()| )
                                     ( |  Dim wshell| )
                                     ( |  Set wshell = CreateObject("Wscript.Shell")| )
                                     ( |  wshell.Run "cmd.exe", 1, False| )
                                     ( |End Sub| )
                                     ( || )
* einfacher Eingabedialog
                                     ( |Function Input()| )
                                     ( |  Input = InputBox("Namen:", "Namen eingeben", "Horst")| )
                                     ( |End Function| )
                                     ( || )
* Excel-OLE
                                     ( |Function GenerateXLSX(FileName)| )
                                     ( |  Dim oXL| )
                                     ( |  Set oXL = CreateObject("Excel.Application")| )
                                     ( |  oXL.Visible = True| )
                                     ( |  oXL.Workbooks.Add| )
                                     ( |  oXL.Cells(1, 1).Value = "Test"| )
                                     ( |  oXL.ActiveWorkbook.SaveAs(FileName)| )
                                     ( |  oXL.Quit| )
                                     ( |  oXL = Nothing| )
                                     ( |End Function| )
                                   ).

DATA: lv_vbscode TYPE string.

* VBA-Code in String wandeln
LOOP AT it_vbs_code ASSIGNING FIELD-SYMBOL(<c>).
  DATA(lv_codeline) = condense( <c> ).
  IF lv_vbscode IS INITIAL.
    lv_vbscode = lv_codeline.
  ELSE.
    lv_vbscode = |{ lv_vbscode }{ cl_abap_char_utilities=>cr_lf }{ lv_codeline }|.
  ENDIF.
ENDLOOP.

* Scriptcontrol-Objekt erzeugen
DATA: o_scr TYPE ole2_object.
CREATE OBJECT o_scr 'MSScriptControl.ScriptControl'.

* wenn Erzeugung ok
IF sy-subrc = 0 AND o_scr-handle <> 0 AND o_scr-type = 'OLE2'.
  WRITE: / 'Header', o_scr-header.
  WRITE: / 'Type', o_scr-type.
  WRITE: / 'Handle', o_scr-handle.
  WRITE: / 'CB-Index', o_scr-cb_index.
  WRITE: / 'CLSID', o_scr-clsid.

* GUI anzeigen
  SET PROPERTY OF o_scr 'AllowUI' = 1.
* Sprache ist 'VBScript'
  SET PROPERTY OF o_scr 'Language' = 'VBScript'.

* Code hinzufügen und Syntaxcheck
  CALL METHOD OF o_scr 'AddCode'
    EXPORTING
      #1 = lv_vbscode.

* wenn Syntax ok
  IF sy-subrc = 0.
* einfacher Funktionsaufruf mit Übergabeparametern
    DATA: lv_res_i TYPE i.

    CALL METHOD OF o_scr 'Eval' = lv_res_i
      EXPORTING
        #1 = 'Add(1, 2)'.

    WRITE: / 'Add(1, 2) =', lv_res_i.

* Eingabefeld mit Rückgabestring
    DATA: lv_res_s TYPE string.

    CALL METHOD OF o_scr 'Eval' = lv_res_s
      EXPORTING
        #1 = 'Input()'.

    WRITE: / 'Input() =', lv_res_s.

    DATA(lv_txt) = |ShowMsgBox("Hallo { lv_res_s }!")|.
* MessageBox anzeigen
    CALL METHOD OF o_scr 'Eval'
      EXPORTING
        #1 = lv_txt.

    WRITE: / lv_txt.

* Subroutine für Ausführung Exe-Datei aufrufen
    CALL METHOD OF o_scr 'Run'
      EXPORTING
        #1 = 'Run'.

    WRITE: / 'Run()'.

* Excel-Datei generieren
    CALL METHOD OF o_scr 'Eval'
      EXPORTING
        #1 = 'GenerateXLSX("c:\temp\test.xlsx")'.

    WRITE: / 'GenerateXLSX()'.
  ELSE.
    WRITE: / 'Fehler im Code: ', sy-subrc.
  ENDIF.

* Objekt zerstören
  FREE OBJECT o_scr.
ENDIF.

[ABAP] ABAP Char- / String-Zeichenkonstanten (Hilfsmittel für Zeichenverarbeitung)

* Klasse CL_ABAP_CHAR_UTILITIES

VERTICAL_TAB           - "Vertical Tab Stop" Character
SPACE_STR_LANGU        - Language ID for SPACE_STR
SPACE_STR              - String with Whitespace Characters
NEWLINE                - Newline
MINCHAR                - Minimum Possible Value
MAXCHAR                - Maximum Possible Value
HORIZONTAL_TAB         - "Horizontal Tab Stop" Character
FORM_FEED              - "Page Break Character"
BACKSPACE              - Backspace Character
ENDIAN                 - Byte Sequence of Application Server
BYTE_ORDER_MARK_BIG    - Unicode - Big Endian
CR_LF                  - "Carriage Return and Line Feed" Character Pair
BYTE_ORDER_MARK_LITTLE - Unicode - Little Endian
CHARSIZE               - Character Length in Bytes
BYTE_ORDER_MARK_UTF8   - UTF-8 Representation of FEFF