SAP ABAP Create Text File and Read Text File on Application Server
*&---------------------------------------------------------------------*
*& Report ZTEXTFILE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEXTFILE.
TYPES :
BEGIN OF T_EKKO,
EBELN TYPE EBELN,
BUKRS TYPE EKKO-BUKRS,
BSART TYPE EKKO-BSART,
AEDAT TYPE EKKO-AEDAT,
BEDAT TYPE EKKO-BEDAT,
LIFNR TYPE EKKO-LIFNR,
WAERS TYPE EKKO-WAERS,
ZTERM TYPE EKKO-ZTERM,
WKURS TYPE EKKO-WKURS,
ERNAM TYPE EKKO-ERNAM,
END OF T_EKKO.
DATA :
LT_EKKO TYPE T_EKKO OCCURS 0 WITH HEADER LINE,
LS_EKKO TYPE T_EKKO,
TAB TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB,
FILE LIKE RLGRAP-FILENAME,
RESULT TYPE STRING,
V_TMP TYPE STRING,
LS_STRING TYPE STRING.
FIELD-SYMBOLS <FS> TYPE ANY.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECTION-SCREEN BEGIN OF LINE.
*PARAMETERS : P_INCHES(10) TYPE C.
SELECTION-SCREEN COMMENT 1(19) TEXT-002.
SELECTION-SCREEN POSITION 20.
PARAMETERS : P_RAD1 RADIOBUTTON GROUP G1 DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(19) TEXT-003.
SELECTION-SCREEN POSITION 20.
PARAMETERS : P_RAD2 RADIOBUTTON GROUP G1.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK B1.
START-OF-SELECTION.
IF P_RAD1 = 'X'.
PERFORM F_CREATE_TEXT_FILE.
ELSEIF P_RAD2 = 'X'..
PERFORM F_READ_TEXT_FILE.
ENDIF.
FORM F_CREATE_TEXT_FILE.
SELECT * INTO CORRESPONDING FIELDS OF TABLE LT_EKKO
FROM EKKO UP TO 10 ROWS.
FILE = '/usr/sap/upload/ekko2.txt'.
OPEN DATASET FILE FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT LT_EKKO.
CLEAR: RESULT.
DO.
ASSIGN COMPONENT SY-INDEX OF
STRUCTURE LT_EKKO TO <FS>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
V_TMP = <FS>.
CONCATENATE RESULT V_TMP TAB INTO RESULT.
ENDDO.
TRANSFER RESULT TO FILE.
ENDLOOP.
CLOSE DATASET FILE.
MESSAGE 'Text file created' TYPE 'S' DISPLAY LIKE 'S'.
ENDFORM.
FORM F_READ_TEXT_FILE.
REFRESH LT_EKKO.
FILE = '/usr/sap/upload/ekko2.txt'.
OPEN DATASET FILE FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC NE 0.
ELSE.
DO.
CLEAR: LS_STRING, LS_EKKO.
READ DATASET FILE INTO LS_STRING.
IF SY-SUBRC NE 0.
EXIT.
ELSE.
SPLIT LS_STRING AT TAB INTO LS_EKKO-EBELN
LS_EKKO-BUKRS
LS_EKKO-BSART.
* MOVE-CORRESPONDING ls_ekko TO WA_RECORD.
APPEND LS_EKKO TO LT_EKKO[].
ENDIF.
ENDDO.
ENDIF.
CLOSE DATASET FILE.
LOOP AT LT_EKKO.
WRITE:/ SY-VLINE,
(10) LT_EKKO-EBELN, SY-VLINE,
(10) LT_EKKO-BUKRS, SY-VLINE,
(10) LT_EKKO-BSART, SY-VLINE.
ENDLOOP.
ENDFORM.
output read file
Comments
Post a Comment