SAP ABAP FILES

From SapWiki
Revision as of 20:56, 4 July 2024 by WikiSysop (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

OPEN DATASET & TRANSFER

  DATA: l_error TYPE string,
        lo_x TYPE REF TO cx_root,
        l_file TYPE string,
        l_nombre TYPE string,
        l_butxt TYPE t001-butxt,
        l_linea(8000).
        
      OPEN DATASET l_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
      IF sy-subrc <> 0.
        MESSAGE i000(0k) WITH 'Error al crear archivo' l_file DISPLAY LIKE 'E'.
        RETURN.
      ENDIF.

      LOOP AT gt_csv_tab INTO ls_csv_tab.
        TRY.
            move ls_csv_tab to l_linea.
            TRANSFER l_linea TO l_file.
          CATCH cx_sy_codepage_converter_init
                cx_sy_conversion_codepage
                cx_sy_file_authority
                cx_sy_file_io
                cx_sy_file_open
                cx_sy_file_open_mode
                cx_sy_pipe_reopen
                cx_sy_too_many_files INTO lo_x.

             l_error = lo_x->get_text( ).

            MESSAGE i000(0k) WITH 'Error en transferencia de datos' l_error DISPLAY LIKE 'E'.
            CLOSE DATASET l_file.
            RETURN.
        ENDTRY.
      ENDLOOP.

      CLOSE DATASET l_file.
      IF sy-subrc = 0.
        MESSAGE i000(0k) WITH 'Archivo creado con éxito' p_file l_nombre DISPLAY LIKE 'S'.
      ELSE.
        MESSAGE i000(0k) WITH 'Error al crear archivo' l_file DISPLAY LIKE 'E'.
      ENDIF.

Crear archivo de largo fijo

TYPES: BEGIN OF ty_data,
         field01(11) TYPE c,
         field02(50) TYPE c,
         field03(80) TYPE c,
         field04(50) TYPE c,
       END OF ty_data.

DATA gt_filet TYPE ppe_cust_table.
DATA gt_data TYPE TABLE OF ty_data.
DATA wa_data LIKE LINE OF gt_data.

wa_data-field01 = 'HHHHHH'.
wa_data-field03 = 'AAAAAAAAAAAAAA'.
APPEND wa_data TO gt_data.

PERFORM crear_file USING gt_data CHANGING gt_filet.

FORM crear_file USING p_tabla TYPE ANY TABLE
                CHANGING p_tabla_csv TYPE ppe_cust_table.

  DATA: l_struc_raw_data TYPE char1024,
        l_begin          TYPE i.

  FIELD-SYMBOLS <fs_field> TYPE any.
  FIELD-SYMBOLS <ls_envio> TYPE any.

  DATA: lr_tab        TYPE REF TO data,
        lr_typedesc   TYPE REF TO cl_abap_typedescr,
        lr_tabdesc    TYPE REF TO cl_abap_tabledescr,
        lr_datadesc   TYPE REF TO cl_abap_datadescr,
        lr_structdesc TYPE REF TO cl_abap_structdescr.
  FIELD-SYMBOLS: <ls_structcomp> TYPE abap_compdescr.

*  GET REFERENCE OF p_tabla INTO lr_tab.
*  lr_typedesc   ?= cl_abap_tabledescr=>describe_by_data_ref( lr_tab ).
  lr_typedesc   ?= cl_abap_tabledescr=>describe_by_data( p_tabla ).
  lr_tabdesc    ?= lr_typedesc.
  lr_datadesc   =  lr_tabdesc->get_table_line_type( ).
  lr_structdesc ?= lr_datadesc.

  LOOP AT p_tabla ASSIGNING <ls_envio>.
    CLEAR:  l_struc_raw_data .
    DO.
      READ TABLE lr_structdesc->components ASSIGNING <ls_structcomp> INDEX sy-index.
      IF sy-subrc = 0.
        ASSIGN COMPONENT <ls_structcomp>-name OF STRUCTURE <ls_envio> TO <fs_field>.
        l_struc_raw_data+l_begin = <fs_field>.
        l_begin = l_begin + ( <ls_structcomp>-length / 2 ). "1 char = 2 bytes in unicode system
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
    l_struc_raw_data+l_begin = cl_bcs_convert=>gc_crlf.
    APPEND l_struc_raw_data TO p_tabla_csv.
  ENDLOOP.
ENDFORM.
  

Clase CL_GUI_FRONTEND_SERVICES

Crear archivo CSV

Leer archivo CSV

Leer archivo Excel

Leer archivo XML

Leer Arhivo PDF y Visualizarlo

Grabar XSTRING en equipo como Binario(XML,PDF,etc.)

Bajar/Subir archivos servidor

Obtener vía F4 archivo y directorio en un reporte