Application Development 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: 

csv

Former Member
0 Kudos

how do i create an outbound interface whose output should be in csv format

4 REPLIES 4

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You can create a csv file by coding the following.



report zrich_0001 .

data: it001 type table of t001 with header line.
data: iout type table of string .
data: xout type string.
field-symbols: <fs>.

select * into table it001 from t001.

loop at it001.

  clear xout.
  do.
    assign component sy-index of structure it001 to <fs>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      xout = <fs>.
    else.
      concatenate xout <fs> into xout separated by ','.
    endif.
  enddo.

  append xout to iout.

endloop.

call function 'GUI_DOWNLOAD'
     exporting
          filename = 'C:test.csv'
     tables
          data_tab = iout.

Regards,

Rich Heilman

ferry_lianto
Active Contributor
0 Kudos

Hi,

You can use FM SAP_CONVERT_TO_CSV_FORMAT.

Please check this sample code from other thread.


report ztest.

type-pools:TRUXS.

data: begin of itab occurs 0,
      vbeln like vbap-vbeln,
      posnr like vbap-posnr,
      end of itab.

data:  itab1 type TRUXS_T_TEXT_DATA.

select vbeln
       posnr
       up to 10 rows
       from vbap
       into table itab.
 
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
  EXPORTING
    I_FIELD_SEPERATOR          = ','
  TABLES
    I_TAB_SAP_DATA             = itab
  CHANGING
    I_TAB_CONVERTED_DATA       =  itab1
  EXCEPTIONS
    CONVERSION_FAILED          = 1
    OTHERS                     = 2.
 
IF SY-SUBRC <> 0.
  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
        WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 
CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    filename = 'C:TEMPTEST.CSV'
  TABLES
    data_tab = itab1
  EXCEPTIONS
    OTHERS   = 1.

Regards,

Ferry Lianto

Former Member
0 Kudos

If you want CSV file to be downloaded to application server, use below code

  • Local variables declaration.

DATA: tab.

DATA: outrec(300).

FIELD-SYMBOLS: <f>.

  • Column break in excel sheet

tab = CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

  • Open application server filepath

OPEN DATASET p_path FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc <> 0.

Message e000 with 'Error in opening the file'.

ENDIF.

LOOP AT head_tab.

ASSIGN head_tab-header TO <f>.

IF sy-tabix = '1'.

MOVE <f> to outrec.

ELSE.

CONCATENATE outrec <f> into outrec SEPARATED BY tab.

ENDIF.

ENDLOOP.

TRANSFER outrec to p_path.

clear outrec.

  • Split each field in internal table to separate columns in excel file

LOOP AT data_tab_download.

DO.

ASSIGN COMPONENT sy-index OF STRUCTURE data_tab_download TO <f>.

IF sy-subrc NE 0.

EXIT.

ENDIF.

IF sy-index = 1.

MOVE <f> TO outrec.

ELSE.

CONCATENATE outrec <f> INTO outrec SEPARATED BY tab.

ENDIF.

ENDDO.

  • Transfer data to excel file

TRANSFER outrec TO p_path.

CLEAR outrec.

ENDLOOP.

CLOSE DATASET p_path.

Former Member
0 Kudos

thanx....