use open dataset,
transfer and close dataset statements
Hi Nina,
Use the following Syntax:
Declarations:
p_binary --To download the file in binary format else text format.
p_file_path TYPE STRING -- contains the file path where the file needs to be downloaded on application server
f_messages--Contains Error Messages
itab_data--Internal table containing the data to be downloaded
<fs_wa>--Field Symbol
f_line TYPE string--Used to transfer the file line by line.
IF p_binary IS INITIAL. OPEN DATASET p_file_path FOR OUTPUT MESSAGE f_messages IN TEXT MODE. ELSEIF p_binary = 'X'. OPEN DATASET p_file_path FOR OUTPUT MESSAGE f_messages IN BINARY MODE. ENDIF. IF sy-subrc <> 0. EXIT. ENDIF. LOOP AT itab_data ASSIGNING <fs_wa>. DO. ASSIGN COMPONENT sy-index OF STRUCTURE <fs_wa> TO <fs_any>. IF sy-subrc <> 0. EXIT. ENDIF. IF NOT f_line IS INITIAL. CONCATENATE f_line <fs_any> INTO f_line SEPARATED BY p_separator. ELSE. f_line = <fs_any>. ENDIF. ENDDO. TRANSFER f_line TO p_file_path. CLEAR f_line. ENDLOOP. CLOSE DATASET p_file_path. ENDIF. ENDIF.
Regards,
Chetan.
PS:Reward points if this helps.
Use demo code as below n reward points if helpfull -
1) use transaction CG3Z
2) or demo code - (save ur file as TExt Tab)
&----
*& Report ZGILL_AS *
*& *
&----
*& *
*& *
&----
REPORT ZGILL_AS message-id rp .
tables: pa0001,pa0002.
select-options s_pernr for pa0001-pernr no intervals MODIF ID XYZ.
parameters: p_dwnld AS CHECKBOX ,
p_upld AS CHECKBOX DEFAULT 'X'.
parameters: P_DSNI(75) TYPE C MODIF ID ABG DEFAULT
'/usr/local/sapdata/amit.dat' LOWER CASE.
data: begin of itab occurs 0,
pernr(8),
sp1(1) value ',',
werks(4),
sp2(1) value ',',
persg(1),
sp3(1) value ',',
persk(2),
end of itab.
data: s_eof(3).
start-of-selection.
if p_upld = 'X'.
OPEN DATASET P_DSNI FOR OUTPUT IN LEGACY TEXT MODE.
PERFORM FETCH_DATA.
STOP.
elseif p_dwnld = 'X'.
OPEN DATASET P_DSNI FOR INPUT IN LEGACY TEXT MODE.
IF SY-SUBRC NE 0.
MESSAGE E016 WITH
'Error opening seq. file, RC:' SY-SUBRC.
EXIT.
ENDIF.
CLEAR S_EOF.
DO.
PERFORM FETCH_file.
IF S_EOF EQ 'YES'. stop. ENDIF.
ENDDO.
endif.
END-OF-SELECTION.
if itab[] is not initial.
perform print_file1 tables itab.
else.
write:/ 'No records exists'.
endif.
&----
*& Form FETCH_DATA
&----
text
----
--> p1 text
<-- p2 text
----
FORM FETCH_DATA .
SELECT * FROM PA0001 WHERE PERNR IN S_PERNR.
MOVE-CORRESPONDING PA0001 TO ITAB.
TRANSFER ITAB TO P_DSNI.
APPEND ITAB.
ENDSELECT.
CLOSE DATASET P_DSNI.
ENDFORM. " FETCH_DATA
&----
*& Form FETCH_file
&----
text
----
--> p1 text
<-- p2 text
----
FORM FETCH_file .
READ DATASET P_DSNI INTO itab.
append itab.
clear itab.
IF SY-SUBRC NE 0.
S_EOF = 'YES'. EXIT.
ENDIF.
ENDFORM. " FETCH_file
&----
*& Form print_file1
&----
text
----
-->P_ITAB text
----
FORM print_file1 tables P_ITAB structure itab .
write:/2 'EmpNo',
14 'Personnel Area',
34 'Emp Group',
47 'Emp SubGroup'.
skip 1.
loop at p_itab.
write:2 p_itab-pernr,
14 p_itab-werks,
34 p_itab-persg,
47 p_itab-persk.
skip 1.
endloop.
ENDFORM. " print_file1
Message was edited by:
Amit Tyagi
Hi Nina,
Try this code:
<b>OPEN DATASET</b> FILENAME <b>FOR OUTPUT IN TEXT MODE</b> .
IF SY-SUBRC <> 0.
MESSAGE 'Error in opening file'.
ELSE.
LOOP AT Internal table into work_area.
TRANSFER work_area TO FILENAME.
ENDLOOP.
<b>CLOSE DATASET </b>FILENAME.
Hope this helps,
Pragya
Below sample code might give you some idea:
data: it_t001 type table of t001, wa_t001 type t001. data: fname type filename value '/usr/sap/test.csv'. field-symbols: <wa>, <fld>. data: str type string. select * into table it_t001 from t001. open dataset fname for output in text mode encoding default. if sy-subrc ne 0. write:/ 'Unable to open file:', fname. else. loop at it_t001 assigning <wa>. clear str. do. assign component sy-index of structure <wa> to <fld>. if sy-subrc ne 0. exit. elseif sy-index = 1. move <fld> to str. else. concatenate str <fld> into str separated by ','. endif. enddo. transfer str to fname. endloop. close dataset fname. endif.
Regards
Eswar
Hi,
The tcodes CG3y, CG3Z to upload presentation file to application server file and download same.
I am also providing code for that.
data: ls_outfile type char2000, lt_infile type table of char2000. * Selection screen parameters: p_input type char255 obligatory. p_output type char255 obligatory. start-of-selection. open dataset p_output for output in text mode. if sy-subrc ne 0. Write 'Unable to open the specifield application server file', p_output. stop. Endif. call function 'WS_UPLOAD' exporting filename = p_input filetype = 'ASC' tables data_tab = lt_infile exceptions conversion_error = 01 file_open_error = 02 file_read_error = 03 invalid_table_width = 04 invalid_type = 05 no_batch = 06 unknown_error = 07. if syst-subrc NE 0. write: /'Unable to upload', p_input. stop. endif. loop at lt_infile into ls_outfile. transfer ls_outfile to p_output. endloop. describe table lt_infile lines lv_lines. write: / lv_lines, 'records transfered.'. close dataset p_output.
Regards
Bhupal Reddy
Add a comment