Export html table to Excel javascript function spe

2019-02-07 09:35发布

I have the following function that exports an html to 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:

  • 1º = 1º
  • é = é

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

5条回答
淡お忘
2楼-- · 2019-02-07 10:07

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

    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('º') != -1) html = html.replace('º', 'º');
      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...

查看更多
beautiful°
3楼-- · 2019-02-07 10:08

Replacing chars is a poor solution.

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

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

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

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

"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

查看更多
等我变得足够好
4楼-- · 2019-02-07 10:17

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

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...

查看更多
何必那么认真
5楼-- · 2019-02-07 10:19

Just replace encodeURIComponent with escape.

查看更多
Root(大扎)
6楼-- · 2019-02-07 10:22

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('º') != -1) html = html.replace('º', 'º');

  window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}
查看更多
登录 后发表回答