6

I am parsing an excel and extracting data using which I cam creating a csv file and inserting it as an attachment in salesforce. The problem is with the characters other than english. I am able to read the excel with other character, but when I insert it to salesforce as csv it renders as some junk characters. Here's the code that I am using:

Attachment attach = new Attachment();
        Blob body = blob.valueOf('"Initiative","Name","Location"\n"Primary Email","ನಮಸ್ಕಾರಾ","Name12","Currency"');
        attach.Body = body;
        attach.Name = 'updloadedData.csv';
        attach.IsPrivate = false;
        attach.ParentId =parentId;
        attach.contentType= 'text;charset=UTF-8';
        insert attach;

When I recover the csv the kannada (ನಮಸ್ಕಾರಾ) characters get rendered as junk characters. How do I make sure that data is rendered correctly

3
  • 1
    Are you sure you have an error? When I execute this exact code and open the downloaded attachment in LibreOffice Calc, it displays exactly what you have in code. Granted, I have to choose UTF-8 as encoding at import.
    – zdropic
    Oct 22, 2013 at 11:01
  • I am opening the csv in MS excel, but still the characters are coming as junk..
    – Sam
    Oct 22, 2013 at 11:16
  • Have you tried opening it with a different program?
    – Mike Chale
    Oct 22, 2013 at 12:25

2 Answers 2

4

Your problem is not with Salesforce or CSV, but with Excel. It doesn't handle CSV very well; it doesn't recognize the right encoding or, as far as I could find in a brief search, lets you choose the correct one. If you open your file in any regular text editor and set the encoding to UTF-8, you will see exactly what you expect.

For handling UTF-8 CSV files with Excel, you can find some advice here.

1
  • 1
    I agree, this is the problem with the excel not rendering the data correctly. I loaded the file with notepad++ setting the encoding to UTF-8 and the data was showing up properly.. THanks!
    – Sam
    Oct 22, 2013 at 13:02
4

Here is a better approach...You can actually set a Byte Order Marker at the beginning of the .csv that will allow Excel to open the file correctly. Adding this marker is really very easy. Just add this escape sequence('\uFEFF') text to the beginning of your string before converting it to a blob.

Blob body = blob.valueOf('\uFEFF'+'"Initiative","Name","Location"\n"Primary Email","ನಮಸ್ಕಾರಾ","Name12","Currency"');

This was a problem I ran into as well. Happy to pass along the solution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .