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

Output an internal table to application server using field separator

Former Member
0 Likes
2,150

Hi guys,

I want to output the contents of a rather large internal table to the application server with the columns tab separated. Is there an easy way to do this?

Here is the general idea:


data: begin of large_itab occurs 100,
field1(10) type c,
field2(8) type c,
...
field50(3) type c.
data: end of large_itab.

data: l_file type string.
l_file  = '/tmp/tst.txt'

* Fill the table with data


open dataset l_file in text mode for output type 'UNIX' encoding default.
loop at large_itab into wa_large.
  transfer wa_large to l_file.
endloop.
close dataset l_file.

This obviously does not output the fields with a field separator (only all the fileds after one another), but is there an easy way to do it? Would like CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB as my separator. Have tried concatenating the fields one by one into a string with that separator, i.e.:


concatenate wa_large-field1 wa_tot-field2 ...  wa_tot-field50 into l_output separated by l_tab.
transfer l_output to l_file

This works, but it's not very pretty. Could be ok if done by a loop, but typing all the fields after one another on one line is not appealing.

Surely, there must be an easier way to do this. Outputting an internal table to the application server together with some form of separator seems pretty basic to me.

Thanks in advance.

1 ACCEPTED SOLUTION
Read only

michael-john_turner
Active Participant
0 Likes
1,389

Hi,

Why not use ASSIGN COMPONENT OF STRUCTURE in a loop to process each field in each row? Something like:


field-symbols:
  <field> type any.
count = " number of fields in structure

loop at large_itab into wa_large.
  do count times.
    assign component sy-index of structure wa_large to <field>.
    if sy-subrc = 0.
      transfer <field> to l_file.
      transfer cl_abap_char_utilities=>horizontal_tab to l_file.
    endif.
  enddo.

endloop.

I haven't tested the above, but it should do what you need.

MJ

7 REPLIES 7
Read only

Former Member
0 Likes
1,389

you can do something like this :

FORM download_to_excel_server .

DATA: lv_cnt TYPE i,

lv_fld TYPE string,

  • ls_product_group TYPE ty_product_group.

ls_product_group TYPE ty_product_group_download.

FIELD-SYMBOLS <fs> TYPE ANY.

IF NOT gt_product_group[] IS INITIAL.

OPEN DATASET p_fnmprg FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

SET CURSOR FIELD p_fnmprg.

MESSAGE e060 WITH p_fnmprg.

EXIT.

ENDIF.

LOOP AT gt_product_group INTO ls_product_group.

CLEAR: file_text,lv_cnt.

DO.

ADD 1 TO lv_cnt.

ASSIGN COMPONENT lv_cnt OF STRUCTURE ls_product_group TO <fs>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

lv_fld = <fs>.

CONCATENATE file_text lv_fld INTO file_text SEPARATED BY gc_tab.

ENDDO.

SHIFT file_text LEFT BY 1 PLACES.

TRANSFER file_text TO p_fnmprg.

ENDLOOP.

CLOSE DATASET p_fnmprg.

ENDIF.

ENDFORM. " download_to_excel_server

Read only

Former Member
0 Likes
1,389

Hi,

try

data: begin of large_itab occurs 100,
field1(10) type c,
delimit1(1) type c,   
field2(8) type c,
delimit2(1) type c,
...
field50(3) type c.
data: end of large_itab.
 
data: l_file type string.
l_file  = '/tmp/tst.txt'
 
* Fill the table with data and delimiter
 
open dataset l_file in text mode for output type 'UNIX' encoding default.
loop at large_itab into wa_large.
  transfer wa_large to l_file.
endloop.
close dataset l_file.

Regards

Bernd

Read only

Former Member
0 Likes
1,389

hi,

Concatenate all the fields into one string and use seperated by space addition before you transfer the record to the opened file.

santhosh

Read only

michael-john_turner
Active Participant
0 Likes
1,390

Hi,

Why not use ASSIGN COMPONENT OF STRUCTURE in a loop to process each field in each row? Something like:


field-symbols:
  <field> type any.
count = " number of fields in structure

loop at large_itab into wa_large.
  do count times.
    assign component sy-index of structure wa_large to <field>.
    if sy-subrc = 0.
      transfer <field> to l_file.
      transfer cl_abap_char_utilities=>horizontal_tab to l_file.
    endif.
  enddo.

endloop.

I haven't tested the above, but it should do what you need.

MJ

Read only

Former Member
0 Likes
1,389

Hi,

As you already ahve data in your internal table, only you want to add separator to fileds

do the following:-

data: separator type c/ABAp_char1.

move CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB to c.

open dataset l_file in text mode for output type 'UNIX' encoding default.

loop at large_itab into wa_large.

transfer wa_large to l_file.

transfer c to l_file.

endloop.

*if want to add separator after the last field also then

transfer c to l_file.

close dataset l_file.

hope this will solve your problem.

Read only

Former Member
0 Likes
1,389
try this...

data : fields like RSTRUCINFO occurs 0 with header line,
         text(1000).

data:  len type i,
       len2 type i,
       len3 type i.



CALL FUNCTION 'GET_COMPONENT_LIST'
  EXPORTING
    program          = sy-repid
    fieldname        = 'LARGE_ITAB'
  tables
    components       = fields[]
          .

open dataset l_file in text mode for output type 'UNIX' encoding default.

describe table fields lines len3.

loop at large_itab into wa_large.
 text = '"'.
loop at fields.
    len = strlen( text ).
  text+len(fields-olen) =
wa_large+len2(fields-olen).

    len2 = len2 + fields-olen.

    len = len + fields-olen.

    if not sy-tabix = len3.
      text+len = '","'.
    endif.
endloop.

  text+len = '"'.

transfer  text to l_file

    clear: len,len2, text.

endloop.

pls let me know if u need any clarifications

Message was edited by:

chandrasekhar jagarlamudi

Read only

Former Member
0 Likes
1,389

Thanks MJ, your answer did the trick. All I needed to add was the "NO END OF LINE" parameter to the transfer statement, and add some logic to enter a newline character at the end of the table row.


do count times.
  assign component sy-index of structure wa_large to <field>.
  if sy-subrc = 0.
    transfer <field> to l_file NO END OF LINE.
    if sy-index = count.
      transfer cl_abap_char_utilities=>newline to l_file NO END OF LINE.
    else.
      transfer cl_abap_char_utilities=>horizontal_tab to l_file NO END OF LINE.
    endif.
  endif.
enddo.