将HTML表导出到Excel时的阿拉伯语编码

时间:2017-09-25 16:42:41

标签: javascript html excel encoding utf-8

我正在使用此功能从表格

导出
var tableToExcel = (function(e) {
    //getting values of current time for generating the file name
    var dt = new Date();
    var day = dt.getDate();
    var month = dt.getMonth() + 1;
    var year = dt.getFullYear();
    var hour = dt.getHours();
    var mins = dt.getMinutes();
    //var postfix = day + "." + month + "." + year + "_" + hour + "." + mins;
    var postfix = day + "." + month + "." + year;
    //creating a temporary HTML link element (they support setting file names)
    var a = document.createElement('a');
    //getting data from our div that contains the HTML table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('reptoxlsx');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');
    var sheetname = $("#chainnames").children(":selected").text();
    a.href = data_type + ', ' + table_html;
    //setting the file name
    a.download = sheetname + ' _ ' + postfix + '.xls';
    //triggering the function
    a.click();
    //just in case, prevent default behaviour
    //e.preventDefault();
    return false;
});

经过大量搜索(如何以阿拉伯语格式导出数据)后我发现我必须更改文件的Charset,所以,我找到了代码

var uri = 'data:application/vnd.ms-excel;charset=UTF-8;base64,'

所以我添加到我的代码中:

var data_type = 'data:application/vnd.ms-excel;charset=UTF-8;base64';

但是当我尝试从OnClick事件导出XLS文件时:

$(document).on('click','#exportreptoexcel',function(){
        tableToExcel();
    });

文件(浏览器)给了我:

  

失败 - 网络错误

所以我通过删除; base64 将代码更改为:

来编辑我的
var data_type = 'data:application/vnd.ms-excel;charset=UTF-8';

导出/下载文件很棒,但阿拉伯语仍然相同:

Arabic issue in XLS after Export from HTML table

那么,问题是,如何使用给定代码在Excel中导出阿拉伯语?

提前致谢

<小时/> 编辑1 我找到了另一个,它使用阿拉伯语字符集,但没有支持文件名变量。

var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()

导出文件,只需在OnClick事件中调用它:

$(document).on('click','#exportreptoexcelfile',function(){
    //working great with Arabic without filename
    var sheetname = $("#chainnames").children(":selected").text();
    tableToExcel('reptoxlsx',sheetname);

});

- 问题是:如何使这个函数支持“fileName”作为变量?!

再次感谢

3 个答案:

答案 0 :(得分:1)

我在编辑1

中找到了该功能的解决方案
    //window.location.href = uri + base64(format(template, ctx))

    var dt = new Date();
    var day = dt.getDate();
    var month = dt.getMonth() + 1;
    var year = dt.getFullYear();
    var postfix = day + "." + month + "." + year;
    var result = uri + base64(format(template, ctx));
    var a = document.createElement('a');
    a.href = result;
    a.download = name + ' _ ' + postfix + '.xls';
    a.click();
    return true;

答案 1 :(得分:0)

您应该将文本编码为base64:

var data_type = 'data:application/vnd.ms-excel;charset=UTF-8;base64';
...

var table_html_base64 = btoa(encodeURIComponent(table_html)
  .replace(/%([0-9A-F]{2})/g, function (match, code) {
    return String.fromCharCode(parseInt(code, 16));
  }));
...

a.href = data_type + ', ' + table_html_base64;

答案 2 :(得分:0)

Option Explicit