function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Character Encoding problems saving String in a Document record

I am trying to generate a CSV file using a Document record and a String I supply in the record's body.  Here is how I'm doing it at the moment:


Document doc = new Document(); doc.Name = suppliedName; doc.FolderId = suppliedFolder; doc.ContentType = 'text/csv'; doc.Type = 'csv'; doc.Body = Blob.valueOf(csvContent);



The problem I have is that some characters aren't appearing correctly when I open the file in Excel. Is there a way to fix this, maybe by encoding these characters in my csvContent String? How would I go about doing this?


This is just a guess but try adding a BOM(Byte-order-Mark) to see if it encodes corrctly as Unicode,  (Microsoft is known for the use of BOM)


BOM = EncodingUtil.base64decode('77u/').toString();

Why aren't you generating an excel file if you ultimately want to open or view the information in excel, as you must be aware of the fact that the rich formatting of the excel are not supported by csv and they show up as weird characters mostly as boxes. Is there any specific requirement of your that you are generating csv and then opening them in excel? Another thing can be done once you open the csv file in excel and you encounter strange characters you can use excel's clean function to get rid of them.