Importar Excel || tratamiento de datos || ABAP

** LA ESTRCUTURA DE MI PROGRAMA PRINCIPAL ES LA SIGUIENTE:**

ZMM_CRITICIDAD
*& DESCRIPCION:
*    Carga un archivo excel para mostrar el contenido en un ALV

report  ZMM_CRITICIDAD no standard page heading.

include zmm_top.
include zmm_form.

*&---------------------------------------------------------------------*
*& Validaciones de Pantalla
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_ruta.
  PERFORM set_filepath CHANGING p_ruta.

*&---------------------------------------------------------------------*
*& INITIALIZATION
*&---------------------------------------------------------------------*
INITIALIZATION.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  PERFORM upload_excel_it USING p_ruta CHANGING ti_data.

 IF ti_data[] IS INITIAL.
    MESSAGE text-e02 TYPE 'I' DISPLAY LIKE 'E'.
  ELSE.
    PERFORM cargar_alv.
  ENDIF.



  END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& END-OF-SELECTION
*&---------------------------------------------------------------------*

EL CONTENIDO DEL INCLUDE TOP ES EL SIGUIENTE:

Include TOP
TYPE-POOLS: truxs.
*&---------------------------------------------------------------------*
*&  CONSTANTES
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&  Estructuras internas
*&---------------------------------------------------------------------*
TYPES: BEGIN OF type_datos,
        lifnr TYPE lifnr, "Proveedor
        matnr TYPE matnr, "Material
        esokz TYPE esokz, "Tipo Registro Info
        netpr TYPE iprei, "Precio
        peinh TYPE epein, "Cantidad Base
        waers TYPE waers, "moneda
        bprme TYPE bbprm, "UM Precio
   END OF type_datos.

TYPES: type_t_datos TYPE STANDARD TABLE OF type_datos.
*&---------------------------------------------------------------------*
*&  Tablas internas
*&---------------------------------------------------------------------*
DATA: ti_data TYPE type_t_datos.

*&---------------------------------------------------------------------*
*& DEFINICION DE TABLAS Y ESTRUCTURAS NECESARIAS ALV
*&---------------------------------------------------------------------*
TYPE-POOLS: slis.
*
** Catálogo de campos: contiene la descripción de los campos de salida
DATA: gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gs_layout            TYPE slis_layout_alv,
      gt_list_top_of_page  TYPE slis_t_listheader,
      gt_events            TYPE slis_t_event,
      gt_sort              TYPE slis_t_sortinfo_alv WITH HEADER LINE,
      ls_vari              TYPE disvariant,
      g_repid        LIKE sy-repid.

*&---------------------------------------------------------------------*
*& Parámetros de Selección
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK q1 WITH FRAME TITLE text-001.
PARAMETERS: p_ruta  TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN END OF BLOCK q1.

EL CONTENIDO DEL INCLUDE FORM ES EL SIGUIENTE:

Include FORM
*&---------------------------------------------------------------------*
*&      Form  set_filepath
*&---------------------------------------------------------------------*
* Este FORM sirve para el explorador en el que se selecciona el archivo
*----------------------------------------------------------------------*
FORM set_filepath  CHANGING po_ruta TYPE rlgrap-filename.

  CONSTANTS: c_ext_exl   TYPE string     VALUE '*.XLS'.

  DATA: lt_filetable TYPE filetable,
        lx_filetable TYPE file_table,
        wl_sel_text  TYPE string,
        lv_rc TYPE i.

  CLEAR po_ruta.

  wl_sel_text = text-s01.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      window_title            = wl_sel_text
      default_extension       = c_ext_exl
    CHANGING
      file_table              = lt_filetable
      rc                      = lv_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    READ TABLE lt_filetable INTO lx_filetable INDEX 1.
    CHECK sy-subrc EQ 0.
    po_ruta = lx_filetable-filename.
  ENDIF.


ENDFORM.                    " SET_FILEPATH

*&---------------------------------------------------------------------*
*&      Form  UPLOAD_EXCEL_IT
*&---------------------------------------------------------------------*
FORM upload_excel_it USING    pi_ruta TYPE rlgrap-filename
                     CHANGING to_file TYPE type_t_datos.


  TYPES: BEGIN OF type_excel,
       matnr(18) TYPE c, "Material
       peinh(5)  TYPE c, "Cantidad Base
       bprme(3)  TYPE c, "UM Precio
  END OF type_excel.

  DATA: tl_exc TYPE STANDARD TABLE OF type_excel.
  data: it_raw type truxs_t_text_data.


  FIELD-SYMBOLS: <fs> TYPE type_excel,
                 <fo> TYPE LINE OF type_t_datos.

  REFRESH: to_file, tl_exc.

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*     I_FIELD_SEPERATOR    =
*     i_line_header        = 'X'
      i_tab_raw_data       = it_raw       " WORK TABLE
      i_filename           = pi_ruta
    TABLES
      i_tab_converted_data = tl_exc[]    "ACTUAL DATA
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.

  IF ( sy-subrc <> 0 ).
    MESSAGE text-e02  TYPE 'I' DISPLAY LIKE 'E'.
  ELSE.

    DELETE  tl_exc INDEX 1.                    "eliminar la cabecera

    LOOP AT tl_exc ASSIGNING <fs>.

      APPEND INITIAL LINE TO to_file ASSIGNING <fo>. "ME QUITA LA PRIMER FILA DE LA CABECERA DEL EXCEL

      PERFORM conversion_sap_format USING <fs>-matnr CHANGING <fo>-matnr.
      PERFORM conversion_sap_unit USING <fs>-bprme CHANGING <fo>-bprme.
      PERFORM conversion_sap_num USING <fs>-peinh CHANGING <fo>-peinh.

  ENDLOOP.
ENDIF.

ENDFORM.                    " UPLOAD_EXCEL_IT
*&---------------------------------------------------------------------*
*&      Form  CARGAR_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM cargar_alv.

  PERFORM init_fieldcat.
  PERFORM init_layout.
  PERFORM mostrar_alv_01.

ENDFORM.                    " CARGAR_ALV

*&---------------------------------------------------------------------*
*&      Form  init_fieldcat
*&---------------------------------------------------------------------*
*       Informacion de cada columna del ALV
*----------------------------------------------------------------------*
FORM init_fieldcat.

  REFRESH: gt_fieldcat.

  gt_fieldcat-tabname       = 'TI_DATA'.
  gt_fieldcat-fieldname     = 'MATNR'.
  gt_fieldcat-ddictxt       = 'L'.
  gt_fieldcat-seltext_l     = 'Material'.
  APPEND gt_fieldcat. CLEAR gt_fieldcat.

  gt_fieldcat-tabname       = 'TI_DATA'.
  gt_fieldcat-fieldname     = 'PEINH'.
  gt_fieldcat-ddictxt       = 'L'.
  gt_fieldcat-seltext_l     = 'Cant Base'.
  APPEND gt_fieldcat. CLEAR gt_fieldcat.

  gt_fieldcat-tabname       = 'TI_DATA'.
  gt_fieldcat-fieldname     = 'BPRME'.
  gt_fieldcat-ddictxt       = 'L'.
  gt_fieldcat-seltext_l     = 'UM'.
  APPEND gt_fieldcat. CLEAR gt_fieldcat.

ENDFORM.                    " init_fieldcat



*&---------------------------------------------------------------------*
*&      Form  init_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM init_layout.
  gs_layout-colwidth_optimize = 'X'.
  gs_layout-zebra             = 'X'.
ENDFORM.                    " init_layout


*&---------------------------------------------------------------------*
*&      Form  MOSTRAR_ALV_01
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM mostrar_alv_01.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
        i_callback_program             = g_repid
*        i_callback_user_command        = 'USER_COMMAND_01'
*        i_callback_pf_status_set       = 'SET_PF_STATUS_01'
*        i_structure_name               = 'T_OUTTAB'
        is_layout                      = gs_layout
        it_fieldcat                    = gt_fieldcat[]
*        it_sort                        = gt_sort[]
*        it_excluding                   = gt_exclude[]
*        I_DEFAULT                      = 'X'
        i_save                         = ' '           "Grabar Variante
         is_variant                     = ls_vari
         it_events                      = gt_events[]
*        IT_EVENT_EXIT                  =
*        IS_PRINT                       =
*        IS_REPREP_ID                   =
*        I_SCREEN_START_COLUMN          = 0
*        I_SCREEN_START_LINE            = 0
*        I_SCREEN_END_COLUMN            = 0
*        I_SCREEN_END_LINE              = 0
*     IMPORTING
*        E_EXIT_CAUSED_BY_CALLER        =
*        ES_EXIT_CAUSED_BY_USER         =
     TABLES
        t_outtab                       = ti_data
     EXCEPTIONS
        program_error                  = 1
        OTHERS                         = 2.

  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    " MOSTRAR_ALV_01


*---------------------------------------------------------------------*
*       FORM USER_COMMAND_01  Process Call Back Events (Begin)         *
*---------------------------------------------------------------------*
FORM user_command_01 USING ucomm    LIKE sy-ucomm
                        selfield TYPE slis_selfield.

ENDFORM.                    "user_command_01
*&---------------------------------------------------------------------*
*       FORM SET_PF_STATUS_01
*&---------------------------------------------------------------------*
FORM set_pf_status_01 USING lt_cua_exclude TYPE slis_t_extab.

  DATA: lf_gui_status  TYPE gui_status.

  SET PF-STATUS 'ZZSTANDARD'.

ENDFORM.                    "set_pf_status_01
*&---------------------------------------------------------------------*
*&      Form  CONVERSION_SAP_FORMAT
*&---------------------------------------------------------------------*
FORM conversion_sap_format  USING    pi_output
                            CHANGING po_output.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = pi_output
    IMPORTING
      output = po_output.

ENDFORM.                    " CONVERSION_SAP_FORMAT
*&---------------------------------------------------------------------*
*&      Form  CONVERSION_SAP_UNIT
*&---------------------------------------------------------------------*
FORM conversion_sap_UNIT  USING    pi_output
                          CHANGING po_output.

      CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT'
        EXPORTING
          input          = pi_output
          language       = sy-langu
        IMPORTING
          output         = po_output
        EXCEPTIONS
          unit_not_found = 1.

      IF sy-subrc <> 0.
        clear po_output.
      ENDIF.

ENDFORM.                    " CONVERSION_SAP_FORMAT
*&---------------------------------------------------------------------*
*&      Form  CONVERSION_SAP_NUM
*&---------------------------------------------------------------------*
FORM conversion_sap_NUM  USING    pi_output
                          CHANGING po_output.

      CALL FUNCTION 'MOVE_CHAR_TO_NUM'
        EXPORTING
          chr             = pi_output
        IMPORTING
          num             = po_output
        EXCEPTIONS
          convt_no_number = 1
          convt_overflow  = 2
          OTHERS          = 3.

      IF sy-subrc <> 0.
        clear po_output.
      ENDIF.

ENDFORM.   

SALUDOS…YO LO ESTOY UTILIZANDO PARA DARLE TRATAMIENTO A LA TABLA INTERNA…

3 Me gusta

@iespino pero no me quedo claro, si es una pregunta o un aporte?

Updated


Este aporte surge de la necesidad de un colega que ha presentado una inquietud aqui.

@Wleuyacc podrias venir a ver si te es de utilidad el aporte.

1 me gusta

es en aporte perdón…¡¡¡ saludos

1 me gusta

Movido a #educacion ! Gracias @iespino por el aportazo !!! seguramente será de utilidad para los abapers !

1 me gusta

Si hay dudas sobre este aporte, abrir tema nuevo “relacionado” a este :slight_smile: