将 html 表导出到 Excel javascript 功能特殊字符已更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11084564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:22:37  来源:igfitidea点击:

Export html table to Excel javascript function special characters changed

javascriptexcelencodingexportspecial-characters

提问by VSP

I have the following function that exports an html to excel:

我有以下功能可以将 html 导出到 excel:

function generateexcel(tableid) {
  var table= document.getElementById(tableid);
  var html = table.outerHTML;
  window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}

One problem is that the especial characters in the data are transformed to other symbols:

一个问题是数据中的特殊字符被转换为其他符号:

  • 1o = 1?o
  • é = ??
  • 1o = 1?o
  • é = ??

How would you fix this? Is there any character replace to the html to prevent it? Any encoding option?

你会如何解决这个问题?是否有任何字符替换到 html 以防止它?任何编码选项?

回答by Pedro Muniz

Replacing chars is a poor solution.

替换字符是一个糟糕的解决方案。

I replaced encodeURIComponent for escape and works fine, but escape is deprecated since ECMAScript v3.

我将 encodeURIComponent 替换为转义并且工作正常,但转义自 ECMAScript v3 以来已被弃用。

This issue occurs because encodeURIComponent works with UTF-8 and Excel does not.

出现此问题的原因是 encodeURIComponent 可以使用 UTF-8,而 Excel 不能。

Better way for me.

对我来说更好的方法。

Encode data to base64 and export like this. I used jquery-base64 plugin from https://github.com/carlo/jquery-base64/blob/master/jquery.base64.min.js

将数据编码为 base64 并像这样导出。我使用了来自https://github.com/carlo/jquery-base64/blob/master/jquery.base64.min.js 的jquery-base64 插件

And change code to:

并将代码更改为:

window.open('data:application/vnd.ms-excel;base64,' + $.base64.encode(html));

If you don't want to use jquery, you can use this base64_encode function http://phpjs.org/functions/base64_encode

如果你不想使用 jquery,你可以使用这个 base64_encode 函数 http://phpjs.org/functions/base64_encode

"Base64 encoding/decoding is already a native function in modern(tm) browsers: btoa(str) and atob(str) are the functions that should can be used without any external reimplementation." - chipairon

“Base64 编码/解码已经是现代 (tm) 浏览器的本机功能:btoa(str) 和 atob(str) 是无需任何外部重新实现即可使用的功能。” - 芯片

回答by VSP

Solved adding a replace for the problematic symbols:

解决了为有问题的符号添加替换:

function generateexcel(tableid) {
  var table= document.getElementById(tableid);
  var html = table.outerHTML;

  //add more symbols if needed...
  while (html.indexOf('á') != -1) html = html.replace('á', 'á');
  while (html.indexOf('é') != -1) html = html.replace('é', 'é');
  while (html.indexOf('í') != -1) html = html.replace('í', 'í');
  while (html.indexOf('ó') != -1) html = html.replace('ó', 'ó');
  while (html.indexOf('ú') != -1) html = html.replace('ú', 'ú');
  while (html.indexOf('o') != -1) html = html.replace('o', 'º');

  window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}

回答by Deyvid Martinez

I have the same issue, just replace encodeURIComponent for escape.

我有同样的问题,只需将 encodeURIComponent 替换为转义。

function generateexcel(tableid) {
 var table= document.getElementById(tableid);
 var html = table.outerHTML;
 window.open('data:application/vnd.ms-excel,' + escape(html));
}

It works for me...

这个对我有用...

回答by Ramon Lucas

Just replace encodeURIComponentwith escape.

只需替换encodeURIComponentescape.

回答by Manolo

In my case I use generateexcel function previously posted, just adding Capital letters of special characters in order to make it work

就我而言,我使用之前发布的 generateexcel 函数,只需添加特殊字符的大写字母即可使其工作

    function generateexcel(tableid) {
      var table= document.getElementById(tableid);
      var html = table.outerHTML;
      while (html.indexOf('á') != -1) html = html.replace('á', 'á');
      while (html.indexOf('á') != -1) html = html.replace('á', 'Á');
      while (html.indexOf('é') != -1) html = html.replace('é', 'é');
      while (html.indexOf('é') != -1) html = html.replace('é', 'É');
      while (html.indexOf('í') != -1) html = html.replace('í', 'í');
      while (html.indexOf('í') != -1) html = html.replace('í', 'Í');
      while (html.indexOf('ó') != -1) html = html.replace('ó', 'ó');
      while (html.indexOf('ó') != -1) html = html.replace('ó', 'Ó');
      while (html.indexOf('ú') != -1) html = html.replace('ú', 'ú');
      while (html.indexOf('ú') != -1) html = html.replace('ú', 'Ú');
      while (html.indexOf('o') != -1) html = html.replace('o', 'º');
      while (html.indexOf('?') != -1) html = html.replace('?', 'ñ'); 
      while (html.indexOf('?') != -1) html = html.replace('?', 'Ñ');  

  window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}

Hope it helps...

希望能帮助到你...