英文:
Converting UTF-8 data to UTF-16 with BOM
问题
我有一个名为csv
的变量,它包含我需要的所有数据。我想将这些数据转换为UTF-16LE
,因为我希望Excel只需打开文件就能识别数据,而不需要点击获取数据
按钮。
我尝试使用这个解决方案,虽然LibreOffice Calc确实识别它是一个UTF-16
文件,但Excel不能正确解码希腊字符。
以下是我拥有的JavaScript代码:
csv = csvRows.join('\r\n')
var byteArray = new Uint8Array(csv.length * 2);
for (var i = 0; i < csv.length; i++) {
byteArray[i * 2] = csv.charCodeAt(i) // & 0xff;
byteArray[i * 2 + 1] = csv.charCodeAt(i) >> 8 // & 0xff;
}
var blob = new Blob([byteArray], { type: 'text/csv', encoding: "UTF-16LE" });
var url = URL.createObjectURL(blob);
sendFileToClient(url, this.el.id + ".csv")
在Elixir中,我曾经遇到类似的问题,我是通过使用:unicode
模块来解决的,如下所示:
csv =
:unicode.characters_to_binary(
:unicode.encoding_to_bom(:utf8) <> build_csv(data),
:utf8,
{:utf16, :little}
)
我还尝试在文件开头添加\uFEFF
BOM字符,但这会使其被识别为UTF-8 BOM
文件(根据Notepad++的说法)。当我尝试\uFFFE
时,这个UTF-BOM
字符被转化为不同的字母。
LibreOffice Calc打开带有\ufeff
BOM的文件
英文:
I have a variable called csv
and it contains all the data I require. I want to convert this data to UTF-16LE
as I want Excel to recognize the data by just opening the file, without the need of the Get Data
button.
I tried using this solution, and while LibreOffice Calc does recognize it is a UTF-16
file, Excel does not decode the greek characters correctly.
Here is the JavaScript code I have:
csv = csvRows.join('\r\n')
var byteArray = new Uint8Array(csv.length * 2);
for (var i = 0; i < csv.length; i++) {
byteArray[i * 2] = csv.charCodeAt(i) // & 0xff;
byteArray[i * 2 + 1] = csv.charCodeAt(i) >> 8 // & 0xff;
}
var blob = new Blob([byteArray], { type: 'text/csv', encoding: "UTF-16LE" });
var url = URL.createObjectURL(blob);
sendFileToClient(url, this.el.id + ".csv")
A similar problem I had in Elixir I solved by using the :unicode
module like so:
csv =
:unicode.characters_to_binary(
:unicode.encoding_to_bom(:utf8) <> build_csv(data),
:utf8,
{:utf16, :little}
)
I tried also adding a \uFEFF
BOM character at the beginning of the file but that makes it so its recognized as UTF-8 BOM
file (according to Notepad++). When I tried \uFFFE
which is the `UTF-BOM character was turned into different letters.
LibreOffice Calc opening the file without BOM
LibreOffice Calc opening the file with \ufeff
BOM
答案1
得分: 0
我通过在将内容转换为UTF-16之前添加UTF-8 BOM来解决了这个问题。
英文:
I solved the problem by adding the UTF-8 BOM before converting the content to UTF-16.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论