Exporter la table html vers la fonction javascript Excel caractères spéciaux modifiés

J'ai la fonction suivante qui exporte un html pour exceller:

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

Un problème est que les caractères spéciaux dans les données sont transformés en d'autres symboles:

  • 1º = 1º
  • É = Ã ©

Comment réparerez-vous cela? Y a-t-il un caractère remplacé par le html pour l'empêcher? Toute option de codage?

Le remplacement de chars est une mauvaise solution.

J'ai remplacé encodeURIComponent pour échapper et fonctionne bien, mais l'échappement est obsolète depuis ECMAScript v3.

Ce problème se produit car encodeURIComponent fonctionne avec UTF-8 et Excel ne le fait pas.

Meilleur moyen pour moi.

Encodez les données à base64 et exportez comme ceci. J'ai utilisé le plugin jquery-base64 à partir de https://github.com/carlo/jquery-base64/blob/master/jquery.base64.min.js

Et modifiez le code pour:

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

Si vous ne souhaitez pas utiliser jquery, vous pouvez utiliser cette fonction base64_encode http://phpjs.org/functions/base64_encode

"L'encodage / décodage Base64 est déjà une fonction native dans les navigateurs modernes ™: btoa (str) et atob (str) sont les fonctions qui peuvent être utilisées sans aucune réimplémentation externe". – chipairon

Résolu à ajouter un remplacement pour les symboles problématiques:

 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)); } 

J'ai le même problème, il suffit de remplacer encodeURIComponent pour échapper.

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

Ça marche pour moi…

Remplacez simplement encodeURIComponent par escape .

Dans mon cas, j'utilise la fonction generateexcel précédemment affichée, en ajoutant simplement des majuscules de caractères spéciaux pour que cela fonctionne

  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)); } 

J'espère que cela aide…