0
我已經包含了以下功能,將數據表與數據導出到excel表格中,形成各種互聯網來源。它適用於英文字符,但當表格中包含中文字母時,excel文檔顯示隨機字符,而不是中文字符。這是否與excel V中的編碼有關?我的頁面?我怎樣才能解決這個問題?漢字在Excel中丟失
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 the link to be in the body
dlLink.download = outputFile;
dlLink.href = uri;
dlLink.click();
document.body.removeChild(link); // remove the 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]; });
}
歡迎來到'字符編碼'的美妙世界!這可能有所幫助:http://superuser.com/questions/280603/how-to-set-character-encoding-when-opening-excel – Pogrindis
我只是看着這個,但這有什麼幫助? – user3916570
啊,我明白了。我怎樣才能告訴excel在沒有讓最終用戶進入他們的設置的情況下以UTF-8打開文檔? – user3916570