javascript - Chinese Characters Lost in Excel -
i have comprised following functions take table data , export excel sheet form various internet sources. works great english characters when table contains chinese letters excel document shows random chars , not chinese. have encoding in excel v. page? how can fix this?
function exporttoexcel(table, name) { var uri = 'data:application/vnd.ms-excel;base64,';//application/vnd.openxmlformats-officedocument.spreadsheetml.sheet var 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>'; if(!table.nodetype) table = document.getelementbyid(table); var ctx = {worksheet: name || 'worksheet', table: table.innerhtml}; uri += tobase64(format(template, ctx)); //window.location.href = uri; var dllink = document.createelement('a'); if (typeof dllink.download === 'string') { document.body.appendchild(dllink); // firefox requires link in body dllink.download = outputfile; dllink.href = uri; dllink.click(); document.body.removechild(link); // remove link when done } else { location.replace(uri); } } function tobase64(data) { if (window.btoa) return window.btoa(unescape(encodeuricomponent(data))); else // ie { var struni = data; var strutf = struni.replace(/[\u0080-\u07ff]/g, function(c) { var cc = c.charcodeat(0); return string.fromcharcode(0xc0 | cc >> 6, 0x80 | cc & 0x3f); }) .replace(/[\u0800-\uffff]/g, function(c) { var cc = c.charcodeat(0); return string.fromcharcode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3f, 0x80 | cc & 0x3f); }); return strutf; } } function format(template, ctx) { return template.replace(/{(\w+)}/g, function(m, p) { return ctx[p]; }); }
i added head tag in template string , tells excel open file utf-8. retained chineese characters in excel.
Comments
Post a Comment