使用Unicode字符通过JavaScript生成Excel的CSV [英] Generate CSV for Excel via JavaScript with Unicode Characters

查看:207
本文介绍了使用Unicode字符通过JavaScript生成Excel的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在客户端使用javascript生成CSV文件。我已遵循此stackoverflow问题的答案。我在内容中有unicode字符(我的情况下是希伯来字符)。

I am trying to generate a CSV file on the client side using javascript. I've followed the answer on this stackoverflow question. I have unicode characters in the content (Hebrew characters in my case).

文件生成成功,但是当我在Excel中打开文件时,显示所有unicode字符作为滑稽的字符。 ASCII字符(英文和数字)很好地出现。

The file generation succeeds, however when I open the file in Excel - all the unicode characters are shown as funny characters. ASCII characters (English and numbers) are presented well.

奇怪的是,如果我在记事本中打开文件,unicode字符显示得很好。所以我想这与Excel和我保存文件的方式有关。

The weird thing is that if I open the file in notepad, the unicode characters show well. So I guess this has something to do with Excel and the way I'm saving the file.

任何想法?

推荐答案

按照Jack Cole的评论和这个问题,什么修复我的问题是添加一个BOM前缀(\\\)到文件的开头。

Following Jack Cole's comment and this question, what fixed my problem was adding a BOM prefix (\uFEFF) to the beginning of the file.

这是工作代码: / p>

This is the working code:

var csvContent = "...csv content...";
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", "data:text/csv;charset=utf-8,\uFEFF" + encodedUri);
link.setAttribute("download","report.csv");
link.click();

这篇关于使用Unicode字符通过JavaScript生成Excel的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆