通过JavaScript以Unicode字符为Excel生成CSV [英] Generate CSV for Excel via JavaScript with Unicode Characters

查看:179
本文介绍了通过JavaScript以Unicode字符为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();

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

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