Javascript表格导出为ex​​cel utf8

我需要一些function,导出我的HTML表格作为Excel文件。 下面的代码这样做,但它保存在错误的字符集表。 我需要使它成为UTF-8。

这是我的代码:

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

注意:我真正的表的id是“pztable”,但是我删除了最后一列,因为我不想导出该列。 所以我创build了一个id =“toExcel”的表格,然后导出该表格。

我解决了这个问题。 这是代码的最后一个版本。

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