I need some function that export my html table as excel file. The following codes do this but it saves the table in wrong charset. I need to make it UTF-8.
Here is my code:
function exceller() { //UI
var uri = 'data:application/vnd.ms-excel;base64; charset=UTF-8,',
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]--></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];
});
};
table = document.getElementById("pztable");
$('#toExcel').html($(table).html());
$('#toExcel').find("thead > tr > th:last-child").remove();
$('#toExcel').find("tbody > tr > td:last-child").remove();
var toExcel = $('#toExcel').html();
var ctx = {
worksheet: name || 'Worksheet',
table: toExcel
};
$('#toExcel').remove();
window.open(uri + base64(format(template, ctx)));
}
Note: my real table's id is "pztable" but I am deleting the last column of it because I don't want to export that column. so I'm creating a table with id="toExcel" and then I am exporting that table.
I solved The problem. Here is the last version of the code is.
function exceller() { //UI
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 version="1.0" encoding="UTF-8" standalone="yes"?><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><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]; }) }
table = document.getElementById("pztable");
$('#toExcel').html($(table).html());
$('#toExcel').find("thead > tr > th:last-child").remove();
$('#toExcel').find("tbody > tr > td:last-child").remove();
var toExcel = $('#toExcel').html();
var ctx = {
worksheet: name || '',
table: toExcel
};
$('#toExcel').remove();
window.open(uri + base64(format(template, ctx)));
}