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

internal table to csv file on application server

Former Member
0 Likes
11,155

hi,

i have an internal table i_item ,,

i have to write the content of that internal table to a csv file on the apllication server.

can i have the code please?

hi,

i have an internal table i_item ,,

i have to write the content of that internal table to a csv file on the apllication server.

can i have the code please?

10 REPLIES 10
Read only

Former Member
0 Likes
4,007

Convert data in internal table to a delimited text data

using a function module 'SAP_CONVERT_TO_TEX_FORMAT'

and then download the data from that text file using function module DOWNLOAD.

Read only

0 Likes
4,007

If the file is asved as ".xls" format then again open the file and change 'save as type' to ".CSV(Comma Delimited)".

Read only

Former Member
0 Likes
4,007

If the file is asved as ".xls" format then again open the file and change 'save as type' to ".CSV(Comma Delimited)".

Read only

huseyindereli
Active Contributor
0 Likes
4,007

Hi ,

CSV file is a simple text file which has fields separated by ; sign. When you open files that has csv extension using excel , you can't see the ; signs. There will only be fields inside the excel columns wrapped from the ; sign .

Code should be similar as below ;

DATA : FILENAME(128) DEFAULT '/usr/......../file.csv'. "" Your path

DATA : lv_str type string.

* Open File
OPEN DATASET FILENAME FOR OUTPUT IN TEXT MODE.
CHECK sy-subrc EQ 0.

loop at i_item.

    CONCATENATE 
                i_item-FIELD_1
                i_item-FIELD_2
                i_item-FIELD_3
                i_item-FIELD_4
                i_item-FIELD_5
                
                into lv_str

                SEPARATED BY ';'.

transfer lv_str to filename.

endloop.

* Close File
CLOSE DATASET FILENAME.

Hope it helps you.

Read only

former_member761936
Active Participant
0 Likes
4,007

Hi,

You can use above code of ' Hüseyin Dereli ' , but I think CSV file is ','(Comma) seperator not ';' seperator.This the only change .

Read only

0 Likes
4,007

Hi Narendra ;

Open an empty txt document and fill it like below ;

col11;col12;col13;col14

col21;col22;col23;col24

col31;col32;col33;col34

and than save as sample.csv .

Try to open it using excel.

Read only

former_member761936
Active Participant
0 Likes
4,007

Hi Hüseyin Dereli ,

Open some excel file , and give some data and save it as csv file.then open that csv file with note pad , you will find ',' between fields.Even when you saving excel as csv you can observe in brackets( Comma delimited)

Read only

0 Likes
4,007

Hi Narendra ,

I followed your instructions and i found ";" semi-colon between columns. Because it depends on windows regional settings. Go to control panel -> regional and language options-> customize -> list separator ...

Well i could say CSV means Comma Separated Value BUT ";" semi-colon is preffed lately because of the problems related with "," comma delimiter.

Regards

Hüseyin DERELİ

Read only

Former Member
0 Likes
4,007

Hii Rakhi!

Check out this sample code


REPORT  z_FILE2.
DATA: fname(40),
      w_line TYPE i VALUE 1.
DATA:
  BEGIN OF fs_flight,
    carrid   LIKE sflight-carrid,
    connid   LIKE sflight-connid,
    fldate   LIKE sflight-fldate,
  END OF fs_flight.

DATA:
  t_flight LIKE
     TABLE OF
           fs_flight.
DATA:
  t_flight1 LIKE
      TABLE OF
            fs_flight.
SELECT-OPTIONS:
  s_carrid FOR fs_flight-carrid,
  s_connid FOR fs_flight-connid.
fname = '.\ztest4.csv'.
PERFORM get_flight_data.
OPEN DATASET fname FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
LOOP AT t_flight INTO fs_flight.
  TRANSFER fs_flight TO fname.
ENDLOOP.
IF sy-subrc EQ 0.
  WRITE: 'File Opened On Apps Server'.
ELSE.
  WRITE: 'File could not be opened'.
ENDIF.
CLOSE DATASET fname.




*&---------------------------------------------------------------------*
*&      Form  get_flight_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_flight_data .
    SELECT carrid
           connid
           fldate
      FROM sflight
      INTO TABLE t_flight
     WHERE carrid IN s_carrid
       AND connid IN s_connid.
endform.                    " get_flight_data

Regards

Abhijeet

Read only

Former Member
0 Likes
4,007

*this is parameter for selection file from presentation (or) appilication serever.

PARAMETERS:p_files TYPE string.

*here we delcaring one structure as per the presentation server file length and for that structuru we declaring the explicit work area (for header) and internal table for that structure

TYPES:BEGIN OF gty_itab1,

abc(305),

END OF gty_itab1.

DATA: gt_itab1 TYPE STANDARD TABLE OF gty_itab1,

gwa_itab1 TYPE gty_itab1.

*here as per your flat file fields you declaring one structure accordingly for this stuructre one explicit work area (for header) and internal table.

TYPES :BEGIN OF gty_itab2,

ktokd(4), "customer account grp kna1

kunnr(10), "customer kna1

name1(35), "name1 kna1

END OF gty_itab2.

DATA: gt_itab2 TYPE STANDARD TABLE OF gty_itab2,

gwa_itab2 TYPE gty_itab2.

*here we are clling the function module GUI_UPLOAD for uploading the file from presentation into internaltable structure as we declared above as gt_itab1(once upload we are providing the file name and file tyep and datatable path as gt_itab1)

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = p_files

filetype = 'ASC'

TABLES

data_tab = gt_itab1

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

IF sy-subrc <> 0.

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

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

ENDIF.

*once uploaded we wil moving the uploaded data from gwa_itab1-abc(305) to gwa_iatb2

LOOP AT gt_itab1 INTO gwa_itab1.

gwa_itab2 = gwa_itab1-abc(305).

APPEND gwa_itab2 TO gt_itab2.

ENDLOOP.

*here we are splitting with comma and miving individual field values into itab2 filed structure like below.

LOOP AT gt_itab2 INTO gwa_itab2.

SPLIT gwa_itab2 AT ',' INTO gwa_itab2-ktokd

gwa_itab2-kunnr

gwa_itab2-name1

gwa_itab2-sortl.

*now our filed values are in internal table so we can move these values into bapi structure and we cann call related BAPI

please let me know if you have any questions...

reward if it is really helpfull.....

Edited by: Srinivas on Jul 17, 2008 2:37 PM