Quantcast
Channel: SCN: Message List - ABAP Connectivity
Viewing all articles
Browse latest Browse all 3125

Re: Transfer data from SAP to FTP server in CSV file format

$
0
0

REPORT y_ni_data_mst.

TABLES: mara. "General Material Data

"Internal Table for General Material Data.

TYPES: BEGIN OF ty_mara,

matnr TYPE mara-matnr,

mtart TYPE mara-mtart,

END OF ty_mara.

DATA: it_mara TYPE TABLE OF ty_mara WITH HEADER LINE.

"Internal Table for taking all fields of the above table in one line

"separated by Comma.

TYPES: BEGIN OF ty_text,

text(250),

END OF ty_text.

DATA:it_text TYPE TABLE OF ty_text WITH HEADER LINE.

"wa_mara TYPE ty_mara.

CONSTANTS: c_key TYPE i VALUE 26101957,

c_dest TYPE rfcdes-rfcdest VALUE 'SAPFTP'.

DATA: g_dhdl TYPE i, " Handle

g_dlen TYPE i, " password length

g_dpwd(30). " For storing password

"Selection Screen Starts

*SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.

* PARAMETERS: p_user(30) DEFAULT 'SAPFTP' OBLIGATORY,

* p_pwd(30) DEFAULT 'N@sh123' OBLIGATORY,

* p_host(64) DEFAULT '192.168.12.4' OBLIGATORY.

DATA: p_user(30) TYPE c VALUE 'SAPFTP', "User-name of ftp server

p_pwd(30) TYPE c VALUE 'N@sh123', "Password of ftp server

p_host(64) TYPE c VALUE '192.168.12.4'. "IP-ADDRESS of FTP server

* l_dest LIKE rfcdes-rfcdest VALUE 'SAPFTPA'."Background RFC destination

*SELECTION-SCREEN END OF BLOCK blk1.

SELECTION-SCREEN BEGIN OF BLOCK blk2 WITH FRAME TITLE text-002.

PARAMETERS: p_file LIKE rlgrap-filename DEFAULT 'MATERIAL_DATA.csv'.

SELECTION-SCREEN END OF BLOCK blk2.

"Password not visible.

AT SELECTION-SCREEN OUTPUT.

LOOP AT SCREEN.

IF screen-name = 'P_PWD'.

screen-invisible = '1'.

MODIFY SCREEN.

ENDIF.

ENDLOOP.

g_dpwd = p_pwd.

"Start of selection

START-OF-SELECTION.

PERFORM material_data_select. "Material Data File

PERFORM build_template_data. "Concatenate Data File

PERFORM ftp_file_material. "FTP Connection

*&---------------------------------------------------------------------*

*& Form MATERIAL_DATA_SELECT

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM material_data_select .

"To fetch the data records from the table T777A.

SELECT matnr mtart

FROM mara

INTO TABLE it_mara

UP TO 15 ROWS.

"Sort the internal table by build.

IF NOT it_mara[] IS INITIAL.

SORT it_mara BY matnr.

ENDIF.

ENDFORM. " MATERIAL_DATA_SELECT

*&---------------------------------------------------------------------*

*& Form BUILD_TEMPLATE_DATA

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM build_template_data .

"Concatenate all the fields of above internal table records in one line

"separated by Comma.

LOOP AT it_mara.

CONCATENATE it_mara-matnr it_mara-mtart

INTO it_text-text SEPARATED BY ','.

APPEND it_text.

CLEAR it_text.

ENDLOOP.

ENDFORM. " BUILD_TEMPLATE_DATA

*&---------------------------------------------------------------------*

*& Form FTP_FILE_MATERIAL

*&---------------------------------------------------------------------*

* text

*----------------------------------------------------------------------*

* --> p1 text

* <-- p2 text

*----------------------------------------------------------------------*

FORM ftp_file_material .

" To get the length of the password.

g_dlen = strlen( g_dpwd ).

" Below Function module is used to Encrypt the Password.

CALL FUNCTION 'HTTP_SCRAMBLE'

EXPORTING

source = g_dpwd " Actual password

sourcelen = g_dlen

key = c_key

IMPORTING

destination = g_dpwd. " Encyrpted Password

* Connects to the FTP Server as specified by user.

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

text = 'Connecting to FTP Server'.

* Below function module is used to connect the FTP Server.

* It Accepts only Encrypted Passwords.

* This Function module will provide a handle to perform different

* operations on the FTP Server via FTP Commands.

CALL FUNCTION 'FTP_CONNECT'

EXPORTING

user = p_user " FTP USER-NAME

password = g_dpwd " FTP PASSWORD

host = p_host " FTP IP-ADDRESS

rfc_destination = c_dest " RFC Destination 'SAPFTP'

IMPORTING

handle = g_dhdl

EXCEPTIONS

not_connected.

IF sy-subrc NE 0.

FORMAT COLOR COL_NEGATIVE.

WRITE:/ 'Error in Connection'.

ELSE.

WRITE:/ 'FTP Connection is opened '.

ENDIF.

**Transferring the data from internal table to FTP Server.

CALL FUNCTION 'FTP_R3_TO_SERVER'

EXPORTING

handle = g_dhdl

fname = p_file " FILE NAME 'MATERIAL_DATA.txt'

character_mode = 'X'

TABLES

text = it_text " CONCATENATED MATERIAL DATA

EXCEPTIONS

tcpip_error = 1

command_error = 2

data_error = 3

OTHERS = 4.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ELSE.

WRITE:/ 'File has created on FTP Server'.

ENDIF.

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

EXPORTING

text = 'File has created on FTP Server'.

*To Disconnect the FTP Server.

CALL FUNCTION 'FTP_DISCONNECT'

EXPORTING

handle = g_dhdl.

*To Disconnect the Destination.

CALL FUNCTION 'RFC_CONNECTION_CLOSE'

EXPORTING

destination = c_dest

EXCEPTIONS

OTHERS = 1.

ENDFORM. " FTP_FILE_MATERIAL


Viewing all articles
Browse latest Browse all 3125

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>