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: 

Special characters in e-mail CSV attachment

former_member185943
Participant

Hello,

I am trying to attach a content of internal table as CSV file to e-mail. However, I have a problem with special characters (French in this case) when the file is opened by Excel application. If I open it with Notepad everything looks OK so I thought it is a PC issue. However, if I create a CSV file directly from Notepad, then those characters are OK both in Notepad and Excel which indicates that it could be ABAP problem after all. However, I am not sure.

Here is what I tried:

LV_LINE_STR is a comma-separated string, while line breaks are CL_ABAP_CHAR_UTILITIES=>CL_LF constants.

Attempt 1: Straightforward: handling pure text - result is good, except special characters

CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
    EXPORTING
      text      = lv_line_str
    IMPORTING
      length    = lv_length
    TABLES
      ftext_tab = lt_att_content_text.

  lo_document->add_attachment(
        i_attachment_type     = CONV #( 'CSV'  )
        i_attachment_subject  = CONV #( p_attnam )
        i_attachment_size     = CONV #( lv_length ) 
        i_att_content_text    = lt_att_content_text
  ).

Attempt 2: Maybe UTF-8 conversion (must go through binary) would help? The result was exactly the same as above

DATA:
    lt_att_content_text_bin TYPE solix_tab.
  DATA(lv_line_str_conv) = cl_abap_codepage=>convert_to(
      source                        = lv_line_str
      codepage                      = 'DEFAULT' "'UTF-8'
  ).

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer                = lv_line_str_conv
   IMPORTING
     OUTPUT_LENGTH         = lv_length
    tables
      binary_tab            = lt_att_content_text_bin .

  lo_document->add_attachment(
        i_attachment_type     = CONV #( 'CSV'  )
        i_attachment_subject  = CONV #( 'Item list' )
        i_attachment_size     = CONV #( lv_length ) 
        i_att_content_hex     = lt_att_content_text_bin
  ).

Thanks in advance for your help!

KR,

Igor

1 ACCEPTED SOLUTION

jozsef_hegyi
Active Participant

HI

please check SAP example report bcs_example_7 and convert the text string into utf-16e, as it is don in this report.

Best regards

Jozsef H

5 REPLIES 5

Tomas_Buryanek
Active Contributor

I think it is MS Excel vs. CSV problem. Not SAP or your program problem. Your CSV should be fine (UTF-8 CSV) using Attempt 2.

You can try manually open file in Excel via. CSV file import (it is wizard where you choose what is separator etc.) maybe there is encoding selection?

Anyway I just googled this article (and there are some other): https://donatstudios.com/CSV-An-Encoding-Nightmare which might help and explain problem.


NOTE: to your manual test creating CSV file:

Default codepage when creating Notepad (TXT) file in Windows is suprisingly not UTF-8. You can check it if you select "Save as" in notepad (in popup window on bottom there is a codepage selection).

-- Tomas --

0 Kudos

Hi, Tomas,

Thanks for your effort! Now I learned something useful about Notepad 🙂

You were right: manually created CSV via notepad creates it in 'ANSI' codepage, unavailable (or differently named in table TCP00A) in SAP. But luckily the class CL_BCS_CONVERT magically solves it all.

jozsef_hegyi
Active Participant

HI

please check SAP example report bcs_example_7 and convert the text string into utf-16e, as it is don in this report.

Best regards

Jozsef H

0 Kudos

Hi, Jozsef,

Thanks for this hint! The fantastic class CL_BCS_CONVERT for content conversion (RAW, TXT, STRING, XSTRING, SOLI, SOLIX, BIN, OTF, XTAB, TAB) has so many good methods. In my case it magically solved the problem and reduced number of calls and custom code size.

Cheers!

Igor

chaouki_akir
Contributor
0 Kudos

What is the french character involved ?

Why don't you provide a simple test program so everybody can do a try ?