Вы находитесь на странице: 1из 7

Download File to SAP Application Server using ABAP (upload a file in AL11)

Use the following steps to download the ABAP internal table data to a file in SAP application server.

Step 1 :Declare a ABAP internal table and fill the internal table with required data.

Step 2 :Use OPEN DATASET ABAP statement to open/create a file on the SAP application server.

Step 3 :Loop through the internal table and use TRANSFER ABAP statement to move each internal
table record to file on application server.

Step41 :Close the file on the application server using CLOSE DATASET ABAP statement.

REPORT ZSATYA_TEST_AL11.

*----------------------------------------------------------------------*
* Data Decalaration
*----------------------------------------------------------------------*
DATA: gt_spfli TYPE TABLE OF spfli,
gwa_spfli TYPE spfli.
DATA: gv_file TYPE rlgrap-filename.

*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
PERFORM get_data.
IF NOT gt_spfli[] IS INITIAL.
PERFORM save_file.
ELSE.
MESSAGE 'No data found' TYPE 'I'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
FORM get_data.
*Get data from table SPFLI
SELECT * FROM spfli
INTO TABLE gt_spfli.
ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form save_file
*&---------------------------------------------------------------------*
FORM save_file.
DATA: lv_data TYPE string.

*Move complete path to filename


gv_file = 'spfli.txt'.

* Open the file in output mode


OPEN DATASET gv_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc NE 0.
MESSAGE 'Unable to create file' TYPE 'I'.
EXIT.
ENDIF.

LOOP AT gt_spfli INTO gwa_spfli.


CONCATENATE gwa_spfli-carrid
gwa_spfli-connid
gwa_spfli-countryfr
gwa_spfli-cityfrom
gwa_spfli-airpfrom
gwa_spfli-countryto
gwa_spfli-cityto
gwa_spfli-airpto
gwa_spfli-arrtime
INTO lv_data
SEPARATED BY ','.
*TRANSFER moves the above fields from workarea to file with comma
*delimited format
TRANSFER lv_data TO gv_file.
CLEAR: gwa_spfli.
ENDLOOP.
* close the file
CLOSE DATASET gv_file.

ENDFORM. " save_file


Read data form file to abap internal table

steps to upload data from a file in SAP application server to ABAP internal
table.

1. Declare a ABAP internal table.


2. Use OPEN DATASET ABAP statement to open file on appliction server.
3. Use READ DATASET ABAP statement to read each line in the file to workarea.
Append work area data to internal table.
4. Use CLOSE DATASET ABAP statement to close the application server file.
5. Process the data in the internal table.

*----------------------------------------------------------------------*
* Data Decalaration
*----------------------------------------------------------------------*
DATA: gt_spfli TYPE TABLE OF spfli,
gwa_spfli TYPE spfli.
DATA: gv_file TYPE rlgrap-filename.

*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
PERFORM read_file.
PERFORM display_data.

*&---------------------------------------------------------------------*
*& Form read_file
*&---------------------------------------------------------------------*
FORM read_file.
DATA: lv_data TYPE string.

*Move complete path to filename


gv_file = 'D:\usr\sap\EHP\DVEBMGS21\work\spfli.txt'.

*Open the file in application server to read the data


OPEN DATASET gv_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF sy-subrc NE 0.
MESSAGE 'Unable to open file' TYPE 'I'.
ENDIF.
DO.
* Loop through the file, if a record is found move it
* to temporary structure else exit out of the loop.
READ DATASET gv_file INTO lv_data.
IF sy-subrc = 0.
* Split the fields in temporary structure to corresponding
* fields in workarea.
SPLIT lv_data AT ',' INTO gwa_spfli-carrid
gwa_spfli-connid
gwa_spfli-countryfr
gwa_spfli-cityfrom
gwa_spfli-airpfrom
gwa_spfli-countryto
gwa_spfli-cityto
gwa_spfli-airpto
gwa_spfli-arrtime.
APPEND gwa_spfli TO gt_spfli.
CLEAR gwa_spfli.
ELSE.
EXIT.
ENDIF.
ENDDO.
*Close the file
CLOSE DATASET gv_file.

ENDFORM. " read_file

*&---------------------------------------------------------------------*
*& Form DISPLAY_DATA
*&---------------------------------------------------------------------*
FORM display_data .
LOOP AT gt_spfli INTO gwa_spfli.
WRITE:/ gwa_spfli-carrid,
gwa_spfli-connid,
gwa_spfli-countryfr,
gwa_spfli-cityfrom,
gwa_spfli-airpfrom,
gwa_spfli-countryto,
gwa_spfli-cityto,
gwa_spfli-airpto,
gwa_spfli-arrtime.
CLEAR: gwa_spfli.
ENDLOOP.
ENDFORM. " DISPLAY_DATA
Download File to SAP
Presentation Server using ABAP

Use the following steps to download the ABAP internal table data to a file in
SAP application server.

1. Declare a ABAP internal table.


2. Fill the internal table with required data.
3. Use function module GUI_DOWNLOAD’ or ‘GUI_DOWNLOAD’ method of
‘CL_GUI_FRONTEND_SERVICES’ class to download the data.

*----------------------------------------------------------------------*
* Data Decalaration
*----------------------------------------------------------------------*
DATA: gt_spfli TYPE TABLE OF spfli,
gwa_spfli TYPE spfli.

DATA: gv_filename TYPE string,


gv_filetype TYPE char10.
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
PERFORM get_data.
IF NOT gt_spfli[] IS INITIAL.
PERFORM save_file.
ELSE.
MESSAGE 'No data found' TYPE 'I'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
FORM get_data.
*Get data from table SPFLI
SELECT * FROM spfli
INTO TABLE gt_spfli.
ENDFORM. " get_data
*&---------------------------------------------------------------------*
*& Form save_file
*&---------------------------------------------------------------------*
FORM save_file.
*Move complete file path to file name
gv_filename = 'C:\test\data.txt'.
*Download the internal table data into a file in SAP presentation server
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = gv_filename
filetype = 'ASC'
write_field_separator = 'X'
TABLES
data_tab = gt_spfli.

ENDFORM. " save_file

Upload File from SAP


Presentation Server using ABAP
Use the following steps to upload data from a file in SAP presentation server
to ABAP internal table.

1. Declare a ABAP internal table.


2. Use function module ‘GUI_UPLOAD’ or ‘GUI_UPLOAD’ method of
‘CL_GUI_FRONTEND_SERVICES’ class to upload the data to ABAP internal table.
3. Process the data in the internal table.

*----------------------------------------------------------------------*
* Data Decalaration
*----------------------------------------------------------------------*
DATA: gt_spfli TYPE TABLE OF spfli,
gwa_spfli TYPE spfli.

DATA: gv_filename TYPE string,


gv_filetype TYPE char10.
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
PERFORM read_file.
PERFORM process_data.

*&---------------------------------------------------------------------*
*& Form read_file
*&---------------------------------------------------------------------*
FORM read_file.
*Move complete file path to file name
gv_filename = 'C:\test\data.txt'.

*Upload the data from a file in SAP presentation server to internal


*table
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = gv_filename
filetype = 'ASC'
has_field_separator = 'X'
TABLES
data_tab = gt_spfli.

ENDFORM. " read_file

*&---------------------------------------------------------------------*
*& Form process_data
*&---------------------------------------------------------------------*
FORM process_data.

*Display the internal table data


LOOP AT gt_spfli INTO gwa_spfli.
WRITE:/ gwa_spfli-carrid,
gwa_spfli-connid,
gwa_spfli-countryfr,
gwa_spfli-cityfrom,
gwa_spfli-airpfrom,
gwa_spfli-countryto,
gwa_spfli-cityto,
gwa_spfli-airpto,
gwa_spfli-arrtime.
CLEAR: gwa_spfli.
ENDLOOP.
ENDFORM. " process_data

Вам также может понравиться