6

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?

Piyush aggarwal
  • 750
  • 2
  • 14
  • 25
  • 1
    Sounds like an excel problem rather than a programming one. – Kaiido Apr 18 '17 at 05:06
  • Possible duplicate of [Is it possible to force Excel recognize UTF-8 CSV files automatically?](http://stackoverflow.com/questions/6002256/is-it-possible-to-force-excel-recognize-utf-8-csv-files-automatically) – Kaiido Apr 18 '17 at 05:07
  • Hi, Kaiido, 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 suggest something? – Piyush aggarwal Apr 18 '17 at 06:43
  • Nope, this is an excel issue. For some reason they're not even capable of doing BOM identification on csv files. You are doing right in saving it as utf-8, MS is doing crap. Not your fault. Your users which needs these character sets will probably already be used to this anyway. – Kaiido Apr 18 '17 at 06:46
  • 1
    Ps: you could try to save as xls directly instead of csv : https://github.com/SheetJS/js-xlsx – Kaiido Apr 18 '17 at 07:02

1 Answers1

11

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;
Vigor
  • 1,706
  • 3
  • 26
  • 47