3

I have a Apex Code which generates a CSV file and saves it as an Attachment under a record. The issue is that when the file is opened using Excel, few characters do not show properly. Eg: ' as well as Arabic characters. However, while opening the file, if I set the character set as UTF-8, the characters show up properly. I wanted to know if there is a way to set the character set as UTF-8 of the Attachment when creating it?

Current code I use to save the attachment:

Attachment attc = new Attachment(ParentId = exportFile.Id);
attc.Body = Blob.valueOf(excelFileData);
attc.Name = 'My Attachment.csv';
insert attc;

Solved this: I was earlier giving contenttype as 'text/csv' The correct contenttype should have been

attc.ContentType = 'text/csv; charset=UTF-8';

  • RajeshShah - Please move your edit to an answer, then you can accept it a couple of days later - this is really useful as folks easily see it as an answered question. – metadaddy Sep 23 '13 at 17:05
  • All of a sudden this has stopped working. I am working on it. – RajeshShah Sep 25 '13 at 12:31
1
  1. SFDC Apex operates in UTF-8 so any strings you create will be in UTF-8
  2. If your SFDC Apex code is reading data from SObjects, those strings will also be in UTF-8
  3. If your APEX code is reading data from external sources, then APEX will think the data is UTF-8 so you need to be sure the source system created its strings in UTF-8
  4. I believe, by default, Excel CSV will not display most UTF-8 characters as UTF-8. I've noticed this when taking SFDC reports of UTF-8 fields and exporting as UTF-8 CSV vs UTF-8 Excel .xls (the latter will show UTF-8 by default). You may see different behavior opening the csv in Google Docs or Open Office.
  5. The best way to debug this is to take your CSV file and open in Notepad, Notepad++ or a browser with encoding set to UTF-8. This way you can verify that you are creating the CSV attachment correctly. If yes, then the issue is more with native Microsoft Excel behavior.
  • Thanks for the comment. Based on the comment, I tried changing the CSV to XLS extension. Now the data comes fine in the excel file. However, the excel file throws an error when the file is opened saying file is corrupted. If I click Yes, file opens properly. Any idea about the reason for this? – RajeshShah Sep 6 '13 at 11:11
  • RajeshShah - I'm not sure I understand what you did - you changed the file extension from .csv to .xls ? What exact error message do you see? Do you get the same error message if you create a normal .CSV in Excel and then rename the extension to .XLS and then open in Excel? If so, this question is best pursued on Excel forums. This is insightful: stackoverflow.com/questions/6002256/… – cropredy Sep 6 '13 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.