Google Search - Blog...........

ABAP - Download Data With A Delimiter.

Here is the simple coding for downloading any data with the specified delimiter given on the selection screen .





REPORT zdownload.



TABLES: mara.



TYPES: BEGIN OF ty_out,

line(150),

END OF ty_out,



BEGIN OF ty_mat,

matnr TYPE mara-matnr,

mtart TYPE mara-mtart,

mbrsh TYPE mara-mbrsh,

meins TYPE mara-meins,

END OF ty_mat.



DATA: it_out TYPE STANDARD TABLE OF ty_out WITH HEADER LINE,

it_mat TYPE STANDARD TABLE OF ty_mat WITH HEADER LINE.





SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE title.



SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN COMMENT 5(18) title1.

SELECT-OPTIONS: s_matnr FOR mara-matnr.

SELECTION-SCREEN POSITION 1 .

SELECTION-SCREEN END OF LINE .



SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN COMMENT 7(18) title2.

PARAMETERS: p_dlim TYPE c OBLIGATORY.

SELECTION-SCREEN POSITION 1 .

SELECTION-SCREEN END OF LINE .



SELECTION-SCREEN BEGIN OF LINE.

SELECTION-SCREEN COMMENT 7(18) title3.

PARAMETERS: p_file TYPE rlgrap-filename.

SELECTION-SCREEN POSITION 1 .

SELECTION-SCREEN END OF LINE .





SELECTION-SCREEN END OF BLOCK b1.



INITIALIZATION .



title = 'enter the entries'.

title1 = 'material number'.

title2 = 'delimiter'.

title3 = 'file name'.



AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file .



CALL FUNCTION 'F4_FILENAME'

EXPORTING

program_name = syst-cprog

dynpro_number = syst-dynnr

field_name = 'P_FILE'

IMPORTING

file_name = p_file.



START-OF-SELECTION.



**--Get the data from material table

SELECT matnr

mtart

mbrsh

meins

FROM mara

INTO TABLE it_mat

WHERE matnr IN s_matnr.

**--Prepare output file with specified delimit char

LOOP AT it_mat.

IF p_dlim = 'X'.

CONCATENATE it_mat-matnr

it_mat-mtart

it_mat-mbrsh

it_mat-meins

INTO it_out-line

SEPARATED BY cl_abap_char_utilities=>horizontal_tab.

ELSE.

CONCATENATE it_mat-matnr

it_mat-mtart

it_mat-mbrsh

it_mat-meins

INTO it_out-line

SEPARATED BY p_dlim.

ENDIF.

APPEND it_out.

CLEAR: it_out,

it_mat.

ENDLOOP.

**--Download the data



DATA: file TYPE string .



file = p_file .



CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filename = file

TABLES

data_tab = it_out

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

no_authority = 5

unknown_error = 6

header_not_allowed = 7

separator_not_allowed = 8

filesize_not_allowed = 9

header_too_long = 10

dp_error_create = 11

dp_error_send = 12

dp_error_write = 13

unknown_dp_error = 14

access_denied = 15

dp_out_of_memory = 16

disk_full = 17

dp_timeout = 18

file_not_found = 19

dataprovider_exception = 20

control_flush_error = 21

OTHERS = 22.

IF sy-subrc = 0.

WRITE:/ 'File downloaded successfully'.

ENDIF.

No comments:

Post a Comment