导出html表到Excel的javascript函数特殊字符已更改 [英] Export html table to Excel javascript function special characters changed

查看:635
本文介绍了导出html表到Excel的javascript函数特殊字符已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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




  • 1º=1º

  • é=é



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

      解决方案

      替换字符是一个很差的解决方案。



      我替换了encodeURIComponent进行转义,工作正常,但自ECMAScript v3以来,转义已被弃用。



      出现此问题的原因是encodeURIComponent与UTF-8配合使用,Excel不支持。





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



      并将代码更改为:

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

      如果你不想使用jquery,可以使用这个base64_encode函数
      < a href =http://phpjs.org/functions/base64_encode =nofollow noreferrer> http://phpjs.org/functions/base64_encode



      Base64编码/解码已经是现代(tm)浏览器中的本机功能:btoa(str)和atob(str)是可以在没有任何外部重新实现的情况下使用的函数。 - chipairon


      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?

      解决方案

      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

      这篇关于导出html表到Excel的javascript函数特殊字符已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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