Hossam
Hossam

Reputation: 177

Arabic encoding when exporting HTML table to Excel

I'm using this function to export from table

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

after a lot of search about (how to export data in Arabic format) I found that I have to change Charset of the file, So, I found that code

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

So I added to my code:

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

but when I tried to export XLS file from OnClick event:

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

the file (browser) gives me:

Failed - Network Error

So I edit mine by deleting ;base64 changing the code to:

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

Exporting/Downloading the file was great, but Arabic still the same:

Arabic issue in XLS after Export from HTML table

So, the question is, how to export Arabic in Excel using the giving code?

thanks in advance


EDIT 1 I found another and it worked greaaat with Arabic charset, BUT without supporting file name variable.

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

to export file, just call it in OnClick event:

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

});

-- the Question is: How to make this Function support "fileName" as a variable?!

thanks again

Upvotes: 2

Views: 3713

Answers (3)

Reza Mahmoodi
Reza Mahmoodi

Reputation: 706

    let name = 'test';
    let table = 'table1'; // table id
    let uri = 'data:application/vnd.ms-excel;base64,';
    let 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>';
    let base64 = function (s) {
        return window.btoa(unescape(encodeURIComponent(s)));
    };
    let format = function (s, c) {
        return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; });
    };
    if (!table.nodeType) { table = document.getElementById(table); }
    let ctx = { worksheet: name || 'Worksheet', table: table.innerHTML };

    let hiddenElement = document.createElement('a');
    hiddenElement.href = uri + base64(format(template, ctx));
    hiddenElement.target = '_blank';
    hiddenElement.download = `${name}.xls`;
    hiddenElement.click();

Upvotes: 0

Hossam
Hossam

Reputation: 177

I found a solution for the function in EDIT 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;

Upvotes: 2

Artem
Artem

Reputation: 1870

You should encode text as 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;

Upvotes: 0

Related Questions