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.
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.
Share Improve this question edited Oct 24, 2017 at 13:46 Alaan asked Oct 24, 2017 at 12:11 AlaanAlaan 2032 silver badges12 bronze badges 2- 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 Commented Oct 24, 2017 at 16:39
- 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 Commented Oct 24, 2017 at 19:09
1 Answer
Reset to default 5You 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/wiki/Byte_order_mark#UTF-8