0

We are exporting a csv template that contains one row of headers looks like this:

HEADER 1, HEADER 2, HEADER 3

Then, the user fills in the contents and saves the csv. However, if you open it in Excel (which 99.99% of our users will be doing), save Chinese characters as content, and then open it back up, it comes back with ?? or __. There is a fix that forces the user to save it as Unicode txt. However, we don't want to have to give people special instructions, and make it as easy as possible for them. This is currently our code that exports the template. However, it doesn't maintain the characters on re-save:

var blob = new Blob([ csvContent ],{encoding:"UTF-8",type:"text/plain;charset=UTF-8"});
var needsClick = true;

if (window.webkitURL) { // CHROME
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("click", null, null);

    $("<a>", {
        download : filename,
        href : webkitURL.createObjectURL(blob)
    }).get(0).dispatchEvent(evt);

} else {

    if (document.createEvent) { // FIREFOX
        var link = document.createElement("a");
        link.setAttribute("href", encodeURI("data:text/csv;charset=utf-8,"
                + csvContent));
        link.setAttribute("download", filename);
        var e = document.createEvent('MouseEvents');
        e.initEvent('click', true, true);
        needsClick = link.dispatchEvent(e);
    }

    if (needsClick) { // IE
        window.navigator.msSaveBlob(blob, filename);
    }
}
kb_
  • 1,245
  • 4
  • 18
  • 33
  • Does the CSV file initially contain non-ASCII characters? Also, since Excel is Microsoft's software, have you tried manually prepending a UTF-8 BOM to the file? – Karol S Jul 22 '14 at 20:12
  • No it won't contain non-ascii characters to begin with. What do you mean by prepending? We're creating the "file" with just a csv-formatted string. Do I just prepend it to that? If I start it with `var csvContent = "\uFEFF " + headers;`, it just adds `` to the first cell of the first line. – kb_ Jul 22 '14 at 22:02
  • Okay, so it looks like Excel doesn't handle that. I found a similar question: http://stackoverflow.com/questions/4221176/excel-to-csv-with-utf8-encoding – Karol S Jul 23 '14 at 00:25
  • Unfortunately the Google Doc approach won't work. We're trying to avoid disrupting how they do their day to day job with our tool redesign, and they rely heavily on Excel. – kb_ Jul 25 '14 at 08:29

0 Answers0