1
votes

I have a CSV file that I'm generating with Javascript. I'm setting the encoding to utf-8, appending the data and URI encoding the output before making it available for download, like so:

const csvContent = data:text/csv;charset=utf-8,${csvData}

const encodedUri = encodeURI(csvContent)

When opening the file in an application that enforces UTF-8 encoding as standard (.e.g LibreOffice), the file opens fine with non-ASCII text, French and Arabic as an example. However, when attempting to open the file in Excel, which as I understand it, uses windows-1252 by default, the output understandably displays garbage text.

My question is; is there anyway to force Excel to open the file as UTF-8 without having the user manually change their encoding? I'm in a position where I can't expect users to do this.

1
How would Excel know which encoding to use for a text file if you don't tell it? Same for LibreOffice. It just turns out that one's guess is better the other's in this case. (They also have to guess or ask the whole set of CSV specification options, too.) As an alternative, if you can't generate an xlsx, is to generate Excel XML SpreadsheetML. It is a lot simpler than OpenXML.Tom Blodget
The users don't have to change their encoding. They can get Excel to ask all the CSV questions—including file encoding—if they use Excel's Text Import Wizard (Data » From Text).Tom Blodget

1 Answers

4
votes

You can include the UTF-8 BOM (0xEF,0xBB,0xBF) at the beginning of the file. That will get Microsoft software to recognize it as UTF-8.

See: https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8