可以看下tableExport中是怎么导出excel文件的,tableExport是从html中到excel,你需要多一步,从data到html的一步
1、将data组合成table(table,tr,td等)字符串
2、将table字符串组合进包含excel文件信息的html字符串片段中
3、如果浏览器支持new Blob,直接将html字符串转换成blob流,然后使用filesave.js下载文件。否则将html字符串转换为base64格式,使用a标签下载文件(数据多的话,a标签可能超出长度,会报错)
部分源码
var defaults = {
csvSeparator: ',',
csvEnclosure: '"',
consoleLog: false,
displayTableName: false,
escape: false,
excelstyles: [ 'border-bottom', 'border-top', 'border-left', 'border-right' ],
fileName: 'tableExport',
htmlContent: false,
ignoreColumn: [],
jspdf: { orientation: 'p',
unit:'pt',
format:'a4',
margins: { left: 20, right: 10, top: 10, bottom: 10 },
autotable: { padding: 2,
lineHeight: 12,
fontSize: 8,
tableExport: { onAfterAutotable: null,
onBeforeAutotable: null,
onTable: null
}
}
},
onCellData: null,
outputMode: 'file', // file|string|base64
tbodySelector: 'tr',
theadSelector: 'tr',
tableName: 'myTableName',
type:'csv', //'txt', 'sql', 'json', 'xml', 'excel', 'doc', ---'png', 'pdf'
worksheetName: 'xlsWorksheetName'
};
if(defaults.type == 'excel' || defaults.type == 'doc'){
//console.log($(this).html());
var excel="<table>";
if(defaults.displayTableName)
excel +="<tr><td>" + parseString($('<p>' + defaults.tableName + '</p>')) + "</td></tr><tr><td></td></tr>";
var excel="<table>"+$(el).html()+"</table>";
excel += '</table>'
//console.log(excel)
if(defaults.consoleLog === true)
console.log(excel);
var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:"+defaults.type+"' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-'+defaults.type+'; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/';
excelFile += (defaults.type == 'excel')? 'vnd.ms-excel' : 'msword';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
if (defaults.type == 'excel') {
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += defaults.worksheetName;
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
}
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>";
if(defaults.outputMode == 'string')
return excelFile;
var base64data = base64encode(excelFile);
if(defaults.outputMode == 'base64')
return base64data;
var extension = (defaults.type == 'excel')? 'xls' : 'doc';
try {
var blob = new Blob([excelFile], {type: 'application/vnd.ms-'+defaults.type});
saveAs (blob, defaults.fileName+'.'+extension);
}
catch (e) {
downloadFile(defaults.fileName+'.'+extension, 'data:application/vnd.ms-'+defaults.type+';base64,' + base64data);
}
}
function downloadFile(filename, data){
var DownloadLink = document.createElement('a');
if ( DownloadLink ){
document.body.appendChild(DownloadLink);
DownloadLink.style = 'display: none';
DownloadLink.download = filename;
DownloadLink.href = data;
if ( document.createEvent ){
if ( DownloadEvt == null )
DownloadEvt = document.createEvent('MouseEvents');
DownloadEvt.initEvent('click', true, false);
DownloadLink.dispatchEvent(DownloadEvt);
}
else if ( document.createEventObject )
DownloadLink.fireEvent('onclick');
else if (typeof DownloadLink.onclick == 'function' )
DownloadLink.onclick();
document.body.removeChild(DownloadLink);
}
}
function utf8Encode(string) {
string = string.replace(/x0dx0a/g, "x0a");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
function base64encode(input) {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = utf8Encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) + keyStr.charAt(enc2) +
keyStr.charAt(enc3) + keyStr.charAt(enc4);
}
return output;
}