Export html table to Excel javascript function special characters changed

后端 未结 5 1386
Happy的楠姐
Happy的楠姐 2021-01-01 02:22

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

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


        
5条回答
  •  灰色年华
    2021-01-01 02:34

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

提交回复
热议问题