Este programa lo encontré hace mil años en una página en chino, permite cargar cualquier tabla "Z"! desde un excel exportado a .csv con los mismos campos que la estructura de la misma, especificándole el nombre de tabla y archivo con datos.
*&---------------------------------------------------------------------*
*& Report ZTABLE_LOAD *
*& *
*&---------------------------------------------------------------------** Este programa carga los datos en cualquier Tabla especificados en la
* pantalla de selección.
* El Archivo debe ser txt, delimitado por TAB y el orden de los campos
* en el archivo plano debe
* corresponder con los campos de la tabla.
*- Valores en pesos sin puntos ni comas
*- Fechas sin puntos
*
*-Considera 3 lineas de cabecera para documentación
*&---------------------------------------------------------------------*
REPORT ZTABLE_LOAD.
TABLES: dd03l. "Table Fields
DATA: dref TYPE REF TO data. "Reference Variable
DATA : fields_itab LIKE dd03l OCCURS 0 WITH HEADER LINE.
DATA : data_itab(1000) TYPE c OCCURS 0 WITH HEADER LINE.
FIELD-SYMBOLS: <t> , "Points to Table work area
<fd>. "Points to Table Field
"Especificar Tabla "Z"
SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE text-b01.
PARAMETER : p_table LIKE dd03l-tabname OBLIGATORY. "tabla
PARAMETER: p_delete AS CHECKBOX. "borrar tabla
SELECTION-SCREEN END OF BLOCK b01.
"Especificar Archivo de Carga
SELECTION-SCREEN BEGIN OF BLOCK b02 WITH FRAME TITLE text-b02.
PARAMETER : p_file TYPE rlgrap-filename OBLIGATORY. "archivo
PARAMETER : p_head AS CHECKBOX DEFAULT 'X'. "archivo tiene cabecera
SELECTION-SCREEN END OF BLOCK b02.
*DATA : v_hex TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
DATA : v_hex TYPE c VALUE ';'.
"Tab delimiter
DATA: i_tmp(100) OCCURS 0 WITH HEADER LINE. "Split internal table
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
mask = ',*.*,*.*.'
IMPORTING
filename = p_file
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
AT SELECTION-SCREEN.
REFRESH fields_itab.
SELECT * FROM dd03l
WHERE tabname = p_table
AND as4local = 'A'.
IF dd03l-fieldname NE '.INCLUDE '.
MOVE dd03l TO fields_itab.
APPEND fields_itab.
ENDIF.
ENDSELECT.
IF sy-subrc NE 0.
MESSAGE 'Tabla especificada no existe' TYPE 'E'.
ENDIF.
IF NOT p_table CP 'Z*' AND NOT p_table CP 'Y*'.
MESSAGE 'No se permiten tablas de sistema' TYPE 'E'.
ENDIF.
********************** MAIN PROCESSING BLOCK ***************************
START-OF-SELECTION.
PERFORM f_open_file.
PERFORM f_dynamic_table_load.
*---------------------------------------------------------------------*
* FORM f_open_file *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM f_open_file.
REFRESH data_itab.
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
filename = p_file
filetype = 'ASC'
TABLES
data_tab = data_itab
EXCEPTIONS
conversion_error = 1
file_open_error = 2
file_read_error = 3
invalid_table_width = 4
invalid_type = 5
no_batch = 6
unknown_error = 7
OTHERS = 8.
IF sy-subrc NE 0.
WRITE : ' Error in opening file : ' , p_file.
STOP.
ELSE.
"eliminar cabecera
IF p_head = 'X'.
DELETE data_itab INDEX 1.
DELETE data_itab INDEX 1.
DELETE data_itab INDEX 1.
ENDIF.
"borrar tabla
IF p_delete = 'X'.
DELETE FROM (p_table).
ENDIF.
ENDIF.
ENDFORM. "f_open_file
*---------------------------------------------------------------------*
* FORM f_dynamic_table_load *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM f_dynamic_table_load.
DATA lv_index TYPE sy-index.
*---Create a new data object of table specified in parameter (p_table)
*---Reference is stored in dref variable. dref now contains a pointer
*---to newly created object.
CREATE DATA dref TYPE (p_table).
*--Asssign the reference to field-symbol
ASSIGN dref->* TO <t>.
LOOP AT data_itab.
REFRESH i_tmp.
*--Split each input row data into tokens with Tab delimiter
SPLIT data_itab AT v_hex INTO TABLE i_tmp.
CLEAR <t>.
*---Assign each field of table to field-symbol
CLEAR lv_index.
LOOP AT fields_itab WHERE fieldname <> 'MANDT'.
ADD 1 TO lv_index.
ASSIGN COMPONENT fields_itab-fieldname OF STRUCTURE <t> TO <fd>.
IF sy-subrc NE 0.
EXIT.
ENDIF.
READ TABLE i_tmp INDEX lv_index..
IF sy-subrc EQ 0.
<fd> = i_tmp.
ENDIF.
ENDLOOP.
*--Now will have all the data - Modify the table
MODIFY (p_table) FROM <t>.
ENDLOOP.
CLEAR sy-tfill.
DESCRIBE TABLE data_itab LINES sy-tfill.
WRITE: /'Numero total de registros actualizados ', p_table NO-GAP,
' son : ', sy-tfill.
ENDFORM. " LOAD_TABLES
No hay comentarios:
Publicar un comentario