Encoding Utf-8 When Exporting HTML Table to Excel

Javascript export table to excel UTF-8 problem

Unfortunatly, escape is almost deprecated, but it's your faster solution :
replacing

   sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

with

   sa = window.open('data:application/vnd.ms-excel,' + escape(tab_text));  

will do the trick.

It seems to be a problem with excel and encodeURIComponent
Does VBA have any built in URL decoding? . You will find out other solutons here

Problem with unicode data when exporting html table to excel with javascript

I found a solution in the link:
Encoding UTF-8 when exporting HTML table to Excel

and it works and all my unicode data saved correctly in the excel file. But I changed it a little because I wanted to use a given filename then I used an anchor tag instead of window.open, that dynamically added to the document and after clicking on, will be removed. Then it looks like below:

function exportExcel(tableText, filename, worksheetName) {        let downloadLink = document.createElement("a");        let 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"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><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]--></head><body>' + tableText + '</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]; }) }
let ctx = { worksheet: worksheetName || 'Worksheet', table: tableText } // window.location.href = uri + base64(format(template, ctx)); downloadLink.href = uri + base64(format(template, ctx)); downloadLink.download = (filename||"exportedTable") + ".xls";
document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink); }

Arabic encoding when exporting HTML table to Excel

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;

Export html table to excel with Spanish characters

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type: application/x-msexcel; charset=utf-8");
header("Pragma: no-cache");
header('Content-Encoding: UTF-8');
header ("Content-Disposition: attachment; filename=\"$file_name" );
header ("Content-Description: Generated Report" );

if (mb_detect_encoding($content) == 'UTF-8') {
$content = mb_convert_encoding($content , "HTML-ENTITIES", "UTF-8");
}

echo $content;


Related Topics



Leave a reply



Submit