UTF-8 encoidng issue when exporting csv file , JavaScript
Problem solved by adding BOM at the start of the csv string:
var csv = "\ufeff"+CSV;
This is my solution:
var blob = new Blob(["\uFEFF"+csv], { type: 'text/csv; charset=utf-18'});
According to RFC2781, the byte order mark (BOM) 0xFEFF is the BOM for UTF-16 little endian encoding (UTF16-LE). While adding the BOM may resolve the issue for Windows, the problem still exists if one is about to open the generated CSV file using Excel on MacOS.
A solution for writing a multibyte CSV file that works across different OS platforms (Windows, Linux, MacOS) applies these three rules:
- Separate the field with a tab character instead of comma
- Encode the content with UTF16-LE
- Prefix the content with UTF16-LE BOM, which is 0xFEFF
More detailed elaboration, sample code, and use cases can be seen in this article