blog de Floren

SAP HCM y Programación en ABAP

Subir fichero CSV a SAP

Posted by Floren en febrero 26, 2011

Os dejo el código para subir ficheros csv a SAP. Algo muy práctico ya que pasar el Excel a fichero plano suele dar muchos dolores de cabeza:

REPORT ZMIGRA.
* Dado un fichero tabulado por un caracter, sube los datos a la tabla
* correspondiente.
*
* Form Externos
*
* ue__       :-> Tratamiento del campo cuando está lleno
* ue_               :-> Tratamiento cuado la tabla está llena
* ue__ERROR         :-> Tratamiento si el grabación ha pedado
* ue___CONST :-> Tratamiento del campo cuando está lleno

TABLES: DD03L, SSCRFIELDS, RLGRAP.
*———— Tabla a Cargar
INCLUDE ZMIGRA_TOP. “Aqui estaran las tablas.
*———— Tabla a Cargar
DATA: BEGIN OF ITAB OCCURS 0,
REG(255),
END OF ITAB,
MITAB LIKE ITAB OCCURS 0 WITH HEADER LINE,
KITAB(72) OCCURS 0 WITH HEADER LINE.

SELECTION-SCREEN BEGIN OF BLOCK B01 WITH FRAME.
PARAMETERS:     FICHERO LIKE RLGRAP-FILENAME.
SELECTION-SCREEN SKIP 1.
PARAMETERS:     TABLA LIKE DD02L-TABNAME OBLIGATORY.
SELECT-OPTIONS: KMPOLONG FOR ITAB-REG.
SELECT-OPTIONS: KMPOVALE FOR ITAB-REG.
PARAMETERS:     POSICUT(3) TYPE N DEFAULT ‘001’,
USEREXIT(15) DEFAULT ‘ZUE_*’,
TEST AS CHECKBOX DEFAULT ‘X’.
SELECTION-SCREEN END OF BLOCK B01.
SELECTION-SCREEN FUNCTION KEY 1.
SELECTION-SCREEN FUNCTION KEY 2.
DATA: BP(20).
FIELD-SYMBOLS: , .

AT SELECTION-SCREEN ON VALUE-REQUEST FOR FICHERO.
DATA: FICHERO_IN LIKE RLGRAP-FILENAME.
CALL FUNCTION ‘WS_FILENAME_GET’
EXPORTING
DEF_FILENAME     = FICHERO_IN
MASK             =
‘,All Files,*.*,Data Files,*.dat,Text Files,*.txt.’
MODE             = ‘O’
TITLE            = ‘Selecciona un fichero…’
IMPORTING
FILENAME         = FICHERO
EXCEPTIONS
INV_WINSYS       = 01
NO_BATCH         = 02
SELECTION_CANCEL = 03
SELECTION_ERROR  = 04.

AT SELECTION-SCREEN.
IF SSCRFIELDS-UCOMM = ‘FC01’. PERFORM RELOAD. ENDIF.
IF SSCRFIELDS-UCOMM = ‘FC02’. PERFORM EDIT_SRC. ENDIF.

INITIALIZATION.
INCLUDE .
MOVE ‘@42@’ TO SSCRFIELDS-FUNCTXT_01.
MOVE ‘@0Q@’ TO SSCRFIELDS-FUNCTXT_02.
PERFORM RELOAD.

END-OF-SELECTION.

PERFORM UP_FILE.
CHECK SY-SUBRC EQ 0.
PERFORM LOOP_ITAB.

FORM EDIT_SRC.
DATA: SRC(72) OCCURS 0 WITH HEADER LINE.
DATA: SRC3(72) OCCURS 0 WITH HEADER LINE.
DATA: SRC2(72) OCCURS 0 WITH HEADER LINE,
ANS(1).
DATA: MESSI(240),
LINEA TYPE I,
PROG(30),
DIRECTIVA(72).
DATA: F(240),
G TYPE I,
H(72).
REFRESH: SRC2, SRC3, SRC.
READ REPORT ‘ZMIGRA_TOP’ INTO SRC3.
CONCATENATE ‘include’ USEREXIT ‘.’ INTO SRC2 SEPARATED BY ‘ ‘.
APPEND SRC2.
READ REPORT USEREXIT INTO SRC.
CHECK SY-SUBRC EQ 0.
SY-SUBRC = 4.
DATA: FT(1).
CLEAR FT.
WHILE SY-SUBRC NE 0 AND SY-SUBRC NE 2.
CLEAR SY-SUBRC.
EDITOR-CALL FOR SRC TITLE ‘Modificando include.’.
IF SY-SUBRC NE 0.
SY-SUBRC = 2.
CHECK SY-SUBRC EQ 0.
ENDIF.
REFRESH SRC2.
SRC2 = ‘report znadar_ejemplo.’. APPEND SRC2.
IF USEREXIT NP ‘*TOP*’.
LOOP AT SRC3. SRC2 = SRC3. APPEND SRC2. ENDLOOP.
ENDIF.
LOOP AT SRC. SRC2 = SRC. APPEND SRC2. ENDLOOP.
SYNTAX-CHECK FOR SRC2 MESSAGE F LINE G WORD H.
CHECK SY-SUBRC NE 0.
MESSAGE S398(00) WITH ‘ Line: ‘ G ‘.’ F.
ENDWHILE.
CHECK SY-SUBRC EQ 0.
CALL FUNCTION ‘POPUP_TO_CONFIRM’
EXPORTING
TITLEBAR              = ‘Have a Nice Dat… ‘
TEXT_QUESTION         = ‘Would you like to save the source ?’
ICON_BUTTON_1         = ‘ICON_SYSTEM_SAVE’
ICON_BUTTON_2         = ‘ICON_CANCEL’
DEFAULT_BUTTON        = ‘1’
DISPLAY_CANCEL_BUTTON = ‘X’
START_COLUMN          = 25
START_ROW             = 6
IMPORTING
ANSWER                = ANS
EXCEPTIONS
TEXT_NOT_FOUND        = 1
OTHERS                = 2.
CHECK ( ANS = ‘1’ AND SY-SUBRC EQ 0 ).
INSERT REPORT USEREXIT FROM SRC.
ENDFORM.

FORM RELOAD.
CLEAR: KMPOLONG, KMPOVALE. REFRESH: KMPOLONG, KMPOVALE.
KMPOLONG-SIGN   = KMPOVALE-SIGN   = ‘I’.
KMPOLONG-OPTION = KMPOVALE-OPTION = ‘BT’.
SELECT * FROM DD03L WHERE TABNAME = TABLA.
KMPOLONG-HIGH    = DD03L-FIELDNAME.
KMPOLONG-LOW     = DD03L-INTLEN.
KMPOVALE-HIGH    = DD03L-FIELDNAME.
KMPOVALE-LOW     = ‘=’.
APPEND: KMPOLONG, KMPOVALE.
ENDSELECT.
ENDFORM.

FORM LOOP_ITAB.
DATA: DI(20),
SI(20),
AX TYPE I,
BX TYPE I,
CX LIKE SY-INDEX,
DX LIKE AX, AL(1), SF(1), SS(1).
ASSIGN (TABLA) TO .
LOOP AT ITAB.
CLEAR MITAB. REFRESH MITAB.
AX = 1.
AL = ITAB-REG+POSICUT(AX).
SPLIT ITAB-REG AT AL INTO TABLE MITAB.
CLEAR .
LOOP AT KMPOLONG.
READ TABLE MITAB INDEX SY-TABIX.
IF SY-SUBRC NE 0.
CLEAR SY-SUBRC. CHECK SY-SUBRC NE 0.
ENDIF.
CONCATENATE TABLA ‘-‘ KMPOLONG-HIGH INTO DI.
ASSIGN (DI) TO .
AX = 0.
BX = KMPOLONG-LOW.
CONCATENATE ‘ue_’ TABLA ‘_’ KMPOLONG-HIGH INTO BP.
PERFORM (BP) IN PROGRAM (USEREXIT) IF FOUND.
= MITAB-REG+AX(BX).
ENDLOOP.
CHECK SY-SUBRC EQ 0.
CONCATENATE ‘ue_’ TABLA INTO BP.
PERFORM (BP) IN PROGRAM (USEREXIT) IF FOUND.
LOOP AT KMPOVALE.
CONCATENATE TABLA ‘-‘ KMPOVALE-HIGH INTO DI.
ASSIGN (DI) TO .
SHIFT  KMPOVALE-LOW LEFT.
= KMPOVALE-LOW.
CONCATENATE ‘ue_’ TABLA ‘_’ KMPOLONG-HIGH ‘_CONST’ INTO BP.
PERFORM (BP) IN PROGRAM (USEREXIT) IF FOUND.
ENDLOOP.
IF TEST = ‘ ‘.
INSERT (TABLA) FROM .
ENDIF.
IF SY-SUBRC NE 0.
CONCATENATE ‘ue_’ TABLA ‘_ERROR’ INTO BP.
PERFORM (BP) IN PROGRAM (USEREXIT) IF FOUND.
ENDIF.
ENDLOOP.
ENDFORM.

FORM UP_FILE.
CLEAR: ITAB.
REFRESH: ITAB.
CALL FUNCTION ‘WS_UPLOAD’
EXPORTING
FILENAME                = FICHERO
FILETYPE                = ‘ASC’
TABLES
DATA_TAB                = 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
GUI_REFUSE_FILETRANSFER = 8
OTHERS                  = 9.
ENDFORM.

Deja un comentario

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

 
A %d blogueros les gusta esto: