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: 

How to save complex itab to CSV on app server

Former Member
0 Kudos

Greetings.

I'm working on a report that, when ran in the background (SY-BATCH = 'X'), must write the data to either CSV or XLS (as long as it can be opened directly in Excel it matters not which format is actually used).

I've researched this problem on SDN and while I've seen some that come close I have not found anything to fit my particular need. Thus far I've tried the following FM's with the result as indicated:

SAP_CONVERT_TO_CSV_FORMAT

Short dump, i.e., "The current statement is only defined for character-type data objects." The error happens inside a call to FM "SAP_CONVERT_TO_TEX_FORMAT" which is called inside "SAP_CONVERT_TO_CSV_FORMAT". The particular line 164 (IF <F_SOURCE> CO C_DARL_NUMBER.". The line type of the internal table I need to write is complex in that it contains other structures, etc., and contains non-character data.

SAP_CONVERT_TO_XLS_FORMAT

Nothing happens; no errors, no file - nothing.

Before I start down the road of manually concatenating all these fields (and there are many of them) I want to see if there's a way to do this using some standard way to cut down my development time.

Many thanks.

1 ACCEPTED SOLUTION

matteo_montalto
Contributor
0 Kudos

sk312236,

You must use that FM as follows:

- declare a destination table that will contain your fields in comma-separated format:

DATA: it_tabcsv TYPE truxs_t_text_data.

- call the FM as follows:

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
 EXPORTING
   I_FIELD_SEPERATOR          = ';'
  TABLES
    i_tab_sap_data             = it_tbl "name of your itab
  CHANGING
    I_TAB_CONVERTED_DATA       = it_tabcsv "destination tbl
 EXCEPTIONS
   CONVERSION_FAILED          = 1
   OTHERS                     = 2

Once you get it_tabcsv, you can write it down with any download FM (gui_dowload, open dataset and so on).

Hope this helps,

Matteo

6 REPLIES 6

matteo_montalto
Contributor
0 Kudos

Hello,

please provide your call to SAP_CONVERT_TO_CSV_FORMAT, with parameters and declarative part of these ones. It's almost surely a matter of mistype.

0 Kudos

Thanks for looking into this.

The itab I need to convert is "GT_OUTPUT" and I'm using a table named "GT_CSV" to store the converted contents:

TYPES: BEGIN OF header_text,
         partnertxt TYPE bcontd-partnertxt,
         altparttxt TYPE bcontd-altparttxt,
         cclass_txt TYPE bcontct-cclasstxt,
         acttxt     TYPE bcontat-acttxt,
         f_comngtxt TYPE bcontcdt-text,
         priotxt    TYPE bcontpt-text,
         creat_txt  TYPE bcontd-name_o,
       END OF header_text.

TYPES: BEGIN OF ts_output,
         header   TYPE bcont,
         hdr_txt  TYPE header_text,
         ssn      TYPE p0002-perid,
         notes    TYPE tline,
         count    TYPE rseumod-tbmaxsel,
       END OF ts_output.

gt_output TYPE STANDARD TABLE OF ts_output WITH HEADER LINE,
gt_csv TYPE truxs_t_text_data.

Here is the call to the FM:

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
    TABLES
      i_tab_sap_data       = gt_output[]
    CHANGING
      i_tab_converted_data = gt_csv[]
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.

And here is the short dump error analysis text:

*For the statement*
    *"IF ... op1 CO op2 ..."*
 *only character-type data objects are supported at the argument position*
 *"op1".*

 *In this case. the operand "op1" has the non-character-type "I". The*
 *current program is a Unicode program. In the Unicode context, the type*
 *'X' or structures containing not only character-type components are*
 *regarded as non-character-type.*

Edited by: sk312236 on Jul 21, 2009 3:50 PM - I forgot to include the TYPE definition for "header_text". It's at the top now.

0 Kudos

sk312236,

seems that your table could contain non-unicode characters. Please check for this issue and eventually, consider converting the contents into unicode. However, seen that you have a limited number of field in the table, i'd consider for semplicity to create your own csv, for example concatenating every field separed by ';' and putting the row result in a STRING table.

Former Member
0 Kudos

After you have an internal table concatenating all fields seperated by comma(,)...the write the following code accordingly.

  • OPEN DATASET g_path_csv FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

  • IF NOT sy-subrc IS INITIAL.

  • MESSAGE s000 WITH text-001. "Error opening file from FTP Location

  • STOP.

  • ENDIF.

*

  • LOOP AT lt_csv INTO ls_csv.

  • TRANSFER ls_csv-zdata TO g_path_csv.

  • CLEAR : ls_csv.

  • ENDLOOP.

  • CLOSE DATASET g_path_csv.

Let me know if any issues.

Regards,

Vimal.

matteo_montalto
Contributor
0 Kudos

sk312236,

You must use that FM as follows:

- declare a destination table that will contain your fields in comma-separated format:

DATA: it_tabcsv TYPE truxs_t_text_data.

- call the FM as follows:

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
 EXPORTING
   I_FIELD_SEPERATOR          = ';'
  TABLES
    i_tab_sap_data             = it_tbl "name of your itab
  CHANGING
    I_TAB_CONVERTED_DATA       = it_tabcsv "destination tbl
 EXCEPTIONS
   CONVERSION_FAILED          = 1
   OTHERS                     = 2

Once you get it_tabcsv, you can write it down with any download FM (gui_dowload, open dataset and so on).

Hope this helps,

Matteo

Former Member
0 Kudos

Hi,

Please refer following link,

https://wiki.sdn.sap.com/wiki/display/Snippets/WorkingWithFiles

Thanks &Regards,

ShreeMohan