Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

creating a file in application server

Former Member
0 Likes
2,404

How to create a file(.csv file) in application server and how to download the contents of an internal table to that file created in application server,

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,296

Hi Nivetha

Below example should give you some idea in downloading as .CSV file.

data: it_t001 type table of t001,
      wa_t001 type t001.
data: fname type filename value '/dir/file.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.

For downloading back,

use OPEN DATASET in INPUT MODE, READ DATASET and CLOSE DATASET.

Hope this helps.

Regards

Eswar

9 REPLIES 9
Read only

Former Member
0 Likes
1,296

Use the OPEN DATASET and TRANSFER statements.

FILE = /usr/sap/tmp/file.csv.

OPEN DATASET file IN TEXT MODE ENCODING DEFAULT.

if sy-subrc eq 0.

LOOP AT itab_csv.

TRANSFER itab_csv TO file.

ENDLOOP.

endif.

Manoj

Read only

Former Member
0 Likes
1,296

Hi,

You can create file as below.

OPEN DATASET F_NAME FOR OUTPUT IN TEXT MODE MESSAGE MESS.

Regards,

Ram

Pls reward points if helpful

Read only

0 Likes
1,296

REPORT ZGILL_AS message-id rp .

tables: pa0001.

select-options s_pernr for pa0001-pernr no intervals MODIF ID XYZ.

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.

start-of-selection.

OPEN DATASET P_DSNI FOR OUTPUT IN LEGACY TEXT MODE.

PERFORM FETCH_DATA.

STOP.

END-OF-SELECTION.

if itab[] is not initial.

perform print_file1 tables itab.

else.

write:/ 'No records exists'.

endif.

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 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

Read only

Former Member
0 Likes
1,296

HI..This is a sample program to create a CSV file. If you require to move this file to the application server, you can use OPEN DATASET...TRANSFER DATASET..etc to move it to the application server.

REPORT zcomma .

  • Open the file using Notepad or open and SaveAs Text file

DATA : BEGIN OF i_output OCCURS 0,

text(3000) TYPE c,

END OF i_output.

DATA: ws_rmwwr(15) TYPE c.

DATA: BEGIN OF i_rbkp OCCURS 0,

belnr LIKE rbkp-belnr,

gjahr LIKE rbkp-gjahr,

blart LIKE rbkp-blart,

bldat LIKE rbkp-bldat,

budat LIKE rbkp-budat,

usnam LIKE rbkp-usnam,

xblnr LIKE rbkp-xblnr,

bukrs LIKE rbkp-bukrs,

lifnr LIKE rbkp-lifnr,

waers LIKE rbkp-waers,

rmwwr LIKE rbkp-rmwwr,

END OF i_rbkp.

SELECT belnr

gjahr

blart

bldat

budat

usnam

xblnr

bukrs

lifnr

waers

rmwwr FROM rbkp UP TO 10 ROWS INTO TABLE i_rbkp.

BREAK-POINT.

IF sy-subrc = 0.

LOOP AT i_rbkp.

  • WRITE i_rbkp-rmwwr TO ws_rmwwr CURRENCY 'USD'.

ws_rmwwr = i_rbkp-rmwwr.

write ws_rmwwr no-gap to ws_rmwwr.

CONCATENATE i_rbkp-belnr

i_rbkp-gjahr

i_rbkp-blart

i_rbkp-bldat

i_rbkp-budat

i_rbkp-usnam

i_rbkp-xblnr

i_rbkp-bukrs

i_rbkp-lifnr

i_rbkp-waers

ws_rmwwr

INTO i_output SEPARATED BY ',' .

APPEND i_output.

CLEAR i_output.

ENDLOOP.

ENDIF.

CALL FUNCTION 'WS_DOWNLOAD'

EXPORTING

  • BIN_FILESIZE =

filename = 'C:\test.csv'

filetype = 'DAT'

  • APPEND = ' '

  • WRITE_FIELD_SEPARATOR = 'X'

  • HEADER = '00'

  • TRUNC_TRAILING_BLANKS = ' '

  • WRITE_LF = 'X'

  • COL_SELECT = ' '

  • COL_SELECT_MASK = ' '

  • DAT_MODE = ' '

  • IMPORTING

  • FILELENGTH =

TABLES

data_tab = i_output[]

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.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Read only

Former Member
0 Likes
1,297

Hi Nivetha

Below example should give you some idea in downloading as .CSV file.

data: it_t001 type table of t001,
      wa_t001 type t001.
data: fname type filename value '/dir/file.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.

For downloading back,

use OPEN DATASET in INPUT MODE, READ DATASET and CLOSE DATASET.

Hope this helps.

Regards

Eswar

Read only

Former Member
0 Likes
1,296

hi nivetha,

first u have to create the physical path for application server by using logical path(apllication server path in ur system).

u can do this by function module FILE_GET_NAME_USING_PATH

then u can open that file for output.

OPEN DATASET f_file FOR OUTPUT IN TEXT MODE.

TRANSFER itab TO f_file.

regards,

Read only

Former Member
0 Likes
1,296

Hi,

You can use this code.

suppose the i_levelall is ur internal table.

w_tab type c value cl_abap_char_utilities=>horizontal_tab,

w_newline type c value cl_abap_char_utilities=>newline,

F NOT i_levelall[] IS INITIAL.

LOOP AT i_levelall INTO wa_levelall.

wa_outtab-udate = wa_levelall-udate.

wa_outtab-h_t1 = w_tab.

wa_outtab-utime = wa_levelall-utime.

wa_outtab-h_t2 = w_tab.

wa_outtab-matnr = wa_levelall-matnr.

wa_outtab-h_t3 = w_tab.

wa_outtab-maktx = wa_levelall-maktx.

wa_outtab-h_t4 = w_tab.

wa_outtab-kunnr = wa_levelall-kunnr.

wa_outtab-h_t5 = w_tab.

wa_outtab-name1 = wa_levelall-name1.

wa_outtab-h_t6 = w_tab.

wa_outtab-kotabnr = wa_levelall-kotabnr.

wa_outtab-h_t7 = w_tab.

wa_outtab-type = wa_levelall-type.

wa_outtab-h_t8 = w_tab.

wa_outtab-ftext = wa_levelall-ftext.

wa_outtab-h_t9 = w_tab.

wa_outtab-kschl = wa_levelall-kschl.

wa_outtab-h_t10 = w_tab.

wa_outtab-username = wa_levelall-username.

wa_outtab-h_t11 = w_tab.

wa_outtab-f_new = wa_levelall-f_new.

wa_outtab-h_t12 = w_tab.

wa_outtab-validity = wa_levelall-validity.

wa_outtab-h_t13 = w_tab.

wa_outtab-f_old = wa_levelall-f_old.

wa_outtab-h_t14 = w_tab.

wa_outtab-vkorg = wa_levelall-vkorg.

wa_outtab-h_t15 = w_tab.

wa_outtab-vtweg = wa_levelall-vtweg.

wa_outtab-h_t16 = w_tab.

wa_outtab-pltyp = wa_levelall-pltyp.

wa_outtab-h_t17 = w_tab .

APPEND wa_outtab TO i_outtab.

CLEAR: wa_outtab,

wa_levelall.

ENDLOOP.

ENDIF.

CLEAR w_file.

CONCATENATE filepath space

INTO w_file.

CONDENSE w_file.

OPEN DATASET w_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc EQ 0.

MOVE : w_tab TO wa_heading-h_t1,

w_tab TO wa_heading-h_t2,

w_tab TO wa_heading-h_t3,

w_tab TO wa_heading-h_t4,

w_tab TO wa_heading-h_t5,

w_tab TO wa_heading-h_t6,

w_tab TO wa_heading-h_t7,

w_tab TO wa_heading-h_t8,

w_tab TO wa_heading-h_t9,

w_tab TO wa_heading-h_t10,

w_tab TO wa_heading-h_t11,

w_tab TO wa_heading-h_t12,

w_tab TO wa_heading-h_t13,

w_tab TO wa_heading-h_t14,

text-h01 TO wa_heading-h_udate,

text-h02 TO wa_heading-h_utime,

text-h16 TO wa_heading-h_matnr,

text-h17 TO wa_heading-h_maktx,

text-h18 TO wa_heading-h_kunnr,

text-h19 TO wa_heading-h_name1,

text-h06 TO wa_heading-h_kotabnr,

text-h15 TO wa_heading-h_type,

text-h20 TO wa_heading-h_ftext,

text-h05 TO wa_heading-h_kschl,

text-h03 TO wa_heading-h_username,

text-h11 TO wa_heading-h_f_new,

text-h07 TO wa_heading-h_validity,

text-h10 TO wa_heading-h_f_old.

MOVE : text-h21 TO wa_heading-h_vkorg,

text-h22 TO wa_heading-h_vtweg ,

text-h23 TO wa_heading-h_pltyp .

TRANSFER wa_heading TO w_file.

LOOP AT i_outtab INTO wa_outtab.

TRANSFER wa_outtab TO w_file.

ENDLOOP.

CLOSE DATASET w_file.

IF sy-subrc NE 0.

MESSAGE i106 WITH text-011." w_file.

ELSE.

MESSAGE s106 WITH text-012." w_file .

ENDIF.

Reward helpful answers...

Regards,

Siddhesh Sanghvi.

Read only

former_member189059
Active Contributor
0 Likes
1,296

and finally, u can move that file to your presentation server (local computer) by using the transaction CG3Y

Read only

Former Member
0 Likes
1,296

hi

simply see the tcode AL11

regards,

shamim