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

Problem while exporting to CSV file.

Former Member
0 Likes
1,194

Hi Experts,

I am exporting my internal table to a CSV file using following code.

******************************************************************

FORM create_csv .

   TYPE-POOLS: truxs.

   DATA: it_tabla2 TYPE truxs_t_text_data.

   CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'

     EXPORTING

       i_field_seperator    = ';'

     TABLES

       i_tab_sap_data       = it_csv

     CHANGING

       i_tab_converted_data = it_tabla2

     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.

   DATA : lv_fname TYPE string.

   lv_fname = 'C:\vishal\PnG.csv'.   " Provide the file path & file name with CSV extention

   CALL FUNCTION 'GUI_DOWNLOAD'

     EXPORTING

       filename              = lv_fname  " File name including path, give CSV as extention of the file

     TABLES

       data_tab              = it_tabla2       " Pass the Output internal table

     EXCEPTIONS

       file_write_error      = 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.

ENDFORM.                    " CREATE_CSV

*************************************************************************************************

I have used the field separator as a semi colon.

Now the problem is when the file is downloaded it separates the data when ever it encounters a Comma in the data.

for exa. if the address field contains two commas in it then it is split into total three columns. but I want it to be in only one column.

Same happens with the amount field. if 6,450 then it is split in two columns.

I have not used Comma separator but it still spits the data whenever it encounters a Comma.

So pls tell me where am I making a mistake ?

Or is there any other simpler way to convert the data to CSV file ??


Thanks,

Vishal.


4 REPLIES 4
Read only

Former Member
0 Likes
863

Try changing the field separator with '|' . Just to see if its a semicolon issue or some other.

Read only

0 Likes
863

Thanks Namrata for replying.

I tried '|' instead of ';' and also '@' but it still generates the same file with semicolon as separator.

I am confused why so ?

Also do I need to use the field separator in FM 'GUI_DOWNLOAD' ?

And how to add a header line in the CSV file for identifying all the columns ?

Regards,

Vishal     

Read only

Former Member
0 Likes
863

This message was moderated.

Read only

rosenberg_eitan
Active Contributor
0 Likes
863

Hi,

You need to follow the rules of CSV (http://en.wikipedia.org/wiki/Comma-separated_values#Basic_rules_and_examples)

or use cl_abap_char_utilities=>horizontal_tab as separator (Remove any tabs that might be in your  data) .

See a sample program here that uses cl_abap_char_utilities=>horizontal_tab as separator .

http://scn.sap.com/message/15127180#15127180

Regards.