在Excel中打开UTF-8 CSV文件,而不必强制用户设置编码

时间:2021-12-12 11:23:00

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:

我有一个CSV文件,我用Javascript生成。我将编码设置为utf-8,在将数据和URI编码为输出之前附加数据和URI,如下所示:

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

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

const encodedUri = encodeURI(csvContent)

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.

在标准(.e.g LibreOffice)强制执行UTF-8编码的应用程序中打开文件时,该文件以非ASCII文本,法语和阿拉伯语为例打开。但是,当我尝试在Excel中打开文件时,据我所知,默认情况下使用的是Windows-1252,输出可以理解地显示垃圾文本。

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.

我的问题是;无论如何强制Excel以UTF-8打开文件而无需用户手动更改其编码?我处在一个我不能指望用户这样做的位置。

1 个解决方案

#1


0  

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.

您可以在文件开头包含UTF-8 BOM(0xEF,0xBB,0xBF)。这将使微软软件将其识别为UTF-8。

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

请参阅:https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8

#1


0  

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.

您可以在文件开头包含UTF-8 BOM(0xEF,0xBB,0xBF)。这将使微软软件将其识别为UTF-8。

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

请参阅:https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8