📅  最后修改于: 2022-03-11 14:48:46.411000             🧑  作者: Mango
function exportToExcel() {
var tab_text = "";
var textRange; var j = 0, rows = '';
tab = document.getElementById('student-detail');
tab_text = tab_text + tab.rows[0].innerHTML + " ";
var tableData = $('#student-detail').DataTable().rows().data();
for (var i = 0; i < tableData.length; i++) {
rows += ''
+ '' + tableData[i].value1 + ' '
+ '' + tableData[i].value2 + ' '
+ '' + tableData[i].value3 + ' '
+ '' + tableData[i].value4 + ' '
+ '' + tableData[i].value5 + ' '
+ '' + tableData[i].value6 + ' '
+ '' + tableData[i].value7 + ' '
+ '' + tableData[i].value8 + ' '
+ '' + tableData[i].value9 + ' '
+ '' + tableData[i].value10 + ' '
+ ' ';
}
tab_text += rows;
var data_type = 'data:application/vnd.ms-excel;base64,',
template = '{table}
',
base64 = function (s) {
return window.btoa(unescape(encodeURIComponent(s)))
},
format = function (s, c) {
return s.replace(/{(\w+)}/g, function (m, p) {
return c[p];
})
}
var ctx = {
worksheet: "Sheet 1" || 'Worksheet',
table: tab_text
}
document.getElementById("dlink").href = data_type + base64(format(template, ctx));
document.getElementById("dlink").download = "StudentDetails.xls";
document.getElementById("dlink").traget = "_blank";
document.getElementById("dlink").click();
}