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';