Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: how to Download CSV file containing French text with UTF-8 encoding

I am trying to download CVS file using JavaScript. That CSV contains text in French language. I am creating file using blob

    var blob = new Blob([ csv ], {
                type : 'text/csv;charset=utf-8'
            });
    var csvUrl = window.URL.createObjectURL(blob);

But when i open the csv file it shows the content in ANSII format due to which French characters are converted to some unknown characters.

But if i open the same file as text, the encoding is ok (UTF-8).

Example: Text I am trying to download "Opération"
Text Visible in Excel (.csv) ==> Opération   <== With unknown characters
Text if opened in Notpad++ ==> Opération   <== Correct UTF-8

How can i open Download CSV file directly with UTF-8 encoding? I don't want user to change anything in excel. I want some javascript format so that excel could recognize all my characters irrespective of its encoding. Is it possible to do something?

like image 909
Piyush aggarwal Avatar asked Apr 18 '17 05:04

Piyush aggarwal


People also ask

What is the difference between CSV and CSV UTF-8?

CSV UTF-8 (comma delimited). This format is recommended for files that contain any non-ASCII characters since the classic CSV format destroys them. Besides CSV, there is one more format that may come in extremely handy for communicating with other programs.

What is UTF-8 encoded CSV file?

UTF-8, or "Unicode Transformation Format, 8 Bit" is a marketing operations pro's best friend when it comes to data imports and exports. It refers to how a file's character data is encoded when moving files between systems.


1 Answers

I met the same issue, and I found the following solution:

similar problem and solution

The only thing to do is add the "\ufeff" at the beginning of your csv string:

var csv = "\ufeff"+CSV;
like image 173
Vigor Avatar answered Oct 19 '22 03:10

Vigor