1

I'm using ajax/javascript to generate a English to French translations csv file. The issue is that my CSV file contains strange characters.

I think that I have to change some options in Excel?

My generated CSV file: My generated CSV file

'February' should be 'Février' and 'Metal' should be 'Métal'

Thanks :)

Here is the function that I'm using

function exportToCsv(filename, rows) {
var processRow = function (row) {
    var finalVal = '';
    for (var j = 0; j < row.length; j++) {
        var innerValue = row[j] === null ? '' : row[j].toString();
        if (row[j] instanceof Date) {
            innerValue = row[j].toLocaleString();
        };
        var result = innerValue.replace(/"/g, '""');
        if (result.search(/("|,|\n)/g) >= 0)
            result = '"' + result + '"';
        if (j > 0)
            finalVal += ',';
        finalVal += result;
    }
    return finalVal + '\n';
};

var csvFile = '';
for (var i = 0; i < rows.length; i++) {
    csvFile += processRow(rows[i]);
}

var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
    navigator.msSaveBlob(blob, filename);
} else {
    var link = document.createElement("a");
    if (link.download !== undefined) { // feature detection
        // Browsers that support HTML5 download attribute
        var url = URL.createObjectURL(blob);
        link.setAttribute("href", url);
        link.setAttribute("download", filename);
        link.style.visibility = 'hidden';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
}
}
  • 1
    Excel really doesn't play well with character encoding. [This](https://superuser.com/questions/280603/how-to-set-character-encoding-when-opening-excel) may be of use – cybernetic.nomad Dec 27 '18 at 18:45

2 Answers2

1

This is an issue Excel has been having for ages.
What you can do is open import/paste the sheet in Google Sheets and then export as CSV.
Another option if you're on Windows is opening the sheet in Notepad and choose File -> Save As and select ANSI encoding.

Laurens Deprost
  • 1,653
  • 5
  • 16
  • No problem. Make sure to [accept an anwer](https://stackoverflow.com/help/someone-answers) if you're question has been answered. – Laurens Deprost Dec 27 '18 at 19:29
  • For arab language I should : `File -> Save As` and select `UNICODE` encoding. My application can generate a CSV depends on a selected language in a dropdown. A new issue... – mustapha mechken Dec 27 '18 at 19:47
  • French displays correctly but Arabic doesn't? Have you tried saving with `UTF-8` encoding? – Laurens Deprost Dec 27 '18 at 19:58
  • 1
    Its works as UTF-8, and because I have to handle many languages, I think that the best solution if the CSV is broken, is to open the file with Notepad or Notepad++ (better format) – mustapha mechken Dec 27 '18 at 20:25
  • @mustaphamechken the important thing is to use any UTF with a "Byte Order Mark" (BOM) at the beginning of the file. I prefer the UTF-16LE encoding with tabs for separating columns (not commas), it will work better than UTF-8 with Excel, whatever your Excel version is English or other country, as shown [here (old test though)](https://wiki.scn.sap.com/wiki/display/ABAP/CSV+tests+of+encoding+and+column+separator). To add the UTF BOM with notepad, it's "Save As... Encoding Unicode", and in Notepad++, it's "Convert to UCS-2 LE BOM". – Sandra Rossi Dec 29 '18 at 20:04
0

This works for all languages that I tested! Open google docs, File, Open, chose Upload Tab and drag and drop your CSV file