Export html table to Excel javascript function special characters changed

后端 未结 5 1375
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条回答
  •  梦毁少年i
    2021-01-01 03:00

    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

提交回复
热议问题