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: 

Convert to *.CSV format

Former Member
0 Kudos

Hi All~~

I want to convert a internal table to *.csv files, and set the seperator be ',' (not default value: ';' )

When I call a function "SAP_CONVERT_TO_CSV_FORMAT",

and set the parameter " I_FIELD_SEPERATOR = ',' " .

But it's seems didn't work.

The seperator is ';' still.

So.... Maybe somebody can tell me how to do? Thank you.

1 ACCEPTED SOLUTION

former_member226519
Active Contributor
0 Kudos

define a string for output.

concatenate the fields of your internal table into string separated by ','

9 REPLIES 9

former_member226519
Active Contributor
0 Kudos

define a string for output.

concatenate the fields of your internal table into string separated by ','

0 Kudos

This is my code:

TYPE-POOLS: TRUXS.

DATA: BEGIN OF ITAB_TEST OCCURS 0,

COLU_A(10) TYPE C,

COLU_B(10) TYPE C,

END OF ITAB_TEST.

DATA: Delimiter TYPE CHAR01 VALUE ','.

DATA: Ouput_String TYPE TRUXS_T_TEXT_DATA.

*INITIALIZATION

INITIALIZATION.

DO 5 TIMES.

ITAB_TEST-COLU_A = 'TERENCE'.

ITAB_TEST-COLU_B = 'TERENCE'.

APPEND ITAB_TEST.

ENDDO.

*START-OF-SELECTION.

START-OF-SELECTION.

CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'

EXPORTING

I_FIELD_SEPERATOR = Delimiter

TABLES

I_TAB_SAP_DATA = ITAB_TEST

CHANGING

I_TAB_CONVERTED_DATA = Ouput_String

EXCEPTIONS

CONVERSION_FAILED = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

WRITE: / 'Program failed to Convert data.'.

ENDIF.

But the data still convertd into "TERENCE;TERENCE" not "TERENCE,TAN"

0 Kudos

Hi,

Check this code.

It is working fine now.


DATA: BEGIN OF ITAB_TEST OCCURS 0,
COLU_A(10) TYPE C,
COLU_B(10) TYPE C,
END OF ITAB_TEST.

DATA: Delimiter TYPE CHAR01 VALUE ','.
TYPES: BEGIN OF t_data,
        data(100) TYPE c,
        END OF t_data.
DATA: Ouput_String TYPE t_data OCCURS 0 WITH HEADER LINE.

INITIALIZATION.
DO 5 TIMES.
ITAB_TEST-COLU_A = 'TERENCE'.
ITAB_TEST-COLU_B = 'TERENCE'.
APPEND ITAB_TEST.
ENDDO.

START-OF-SELECTION.
LOOP AT ITAB_TEST.
CONCATENATE ITAB_TEST-COLU_A ITAB_TEST-COLU_B INTO Ouput_String-data
SEPARATED BY ','.
APPEND Ouput_String.
ENDLOOP.

CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
*   BIN_FILESIZE                  =
    filename                      = 'E:\test.csv'
*   FILETYPE                      = 'ASC'
*   APPEND                        = ' '
   WRITE_FIELD_SEPARATOR         = '#'
*   HEADER                        = '00'
*   TRUNC_TRAILING_BLANKS         = ' '
*   WRITE_LF                      = 'X'
*   COL_SELECT                    = ' '
*   COL_SELECT_MASK               = ' '
*   DAT_MODE                      = ' '
* IMPORTING
*   FILELENGTH                    =
  tables
    data_tab                      = Ouput_String[]
 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.

For test purpose i have declared occurs 0 and header line.

Please avoid those in ur coding.

Thanks,

Vinod.

0 Kudos

Hi Tan,

If you debug that FM, it always use ';' as a separator (doesn't matter what you choose). This is because it has this constant declaration inside the FM:

CONSTANTS: C_FIELD_SEPARATOR VALUE ';'.

That it will use calling FM SAP_CONVERT_TO_TEX_FORMAT. You can try this FM though.

However, you can achieve your goal like:


LOOP AT itab_test.
  CONCATENATE itab_test-colu_a delimiter itab_test-colu_b INTO ouput_string.
  APPEND ouput_string.
ENDLOOP.

Regards,

Valter Oliveira.

0 Kudos

Hi~Vinod:

I have another question that if I use MS-EXCEL to open the output test.csv files.

That data in EXCEL will be TWO columns:

Column_A Column_B

TERENCE TERENCE

TERENCE TERENCE

TERENCE TERENCE

TERENCE TERENCE

If I use Notepad to open the files, it's will be right format: TERENCE,TERENCE

so, is it right?

Edited by: Tan Terence on Sep 29, 2008 5:14 PM

Edited by: Tan Terence on Sep 29, 2008 5:38 PM

0 Kudos

Hi Tan,

What u r seeing is correct only. When u open in notepad u can see the field seperator. But in excel this # menas that move that next data to # to next cell of excel. So don't worry.

If u want to see the data in excel also in single column then just comment WRITE_FIELD_SEPARATOR = '#'

of GUI_DOWNLOAD. It works.

Thanks,

Vinod.

0 Kudos

Hi,

Try this code it will convert any table for you into comma delimited ...

Declaration

Types:

begin of ty_utab,

upfield1,

upfield2,

upfield3,

--

--

upfieldn,

end of ty_utab,

begin of ty_crtb,

line type string,

end of ty_crtb.

data:

it_utab type table of ty_utab ,

it_crtb type table of ty_crtb.

Perform

perform convert_comma_delimiter tables it_crtb it_utab.

Routine

form convert_comma_delimiter tables pt_crtb pt_utab.

data: lw_crtb type string,

lw_utab type ref to data.

field-symbols: <lw> type any,

<line> type any,

<crtb> type any.

create data lw_utab like line of pt_utab.

assign lw_utab->* to <lw>.

loop at pt_crtb assigning <crtb>.

assign component 'LINE' of structure <crtb> to <line>.

perform separate_comma using <line>

changing <lw>.

if <lw> is not initial.

append <lw> to pt_utab.

clear <lw>.

endif.

endloop.

endform. " CONVERT_COMMA_DELIMITER

Routine

form separate_comma using pw_crtb

changing pw_utab.

data: lw_crtb type string,

l_char1 type char255,

l_char2 like l_char1,

l_idx type i.

field-symbols: <lfs> type any.

do.

l_idx = sy-index.

find ',' in pw_crtb.

if sy-subrc = 0.

assign component l_idx of structure pw_utab to <lfs>.

if sy-subrc <> 0.

exit.

endif.

split pw_crtb at ',' into <lfs> pw_crtb.

else.

if pw_crtb is not initial.

assign component l_idx of structure pw_utab to <lfs>.

if sy-subrc <> 0.

exit.

else.

<lfs> = pw_crtb.

endif.

exit.

else.

exit.

endif.

endif.

enddo.

endform. " SEPARATE_COMMA

former_member226519
Active Contributor
0 Kudos

instead of FM

loop at itab_test.

concatenate itab_test-col1 itab_test-col2 into output_string

separated by delimiter.

write: / output_string.

endloop.

P561888
Active Contributor
0 Kudos

hi,

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

Try this one....

try to download this one in CSV format by Gui_download.

regards,

bharani