/**
*@@function 从页面表格获取数据然后导出Excel,导出的表格合计栏为空的必须加入空格符&npsc;
*@@param {tableID}页面table的ID,必传
*@@param {TableName}导出的Excel名,必传
*@@param {TableTwoHeader}二级表头,可为空
*/
function getTableData(tableID, TableName, TableTwoHeader) {
//表头处理
var data = [];
var header = $(“div [lay-id=’” + tableID + “’]”).find(".layui-table-header")[0].innerText;
var arrHeader = header.split(’\n \n’);
var HeaderjsonStr;
for (var i = 0; i < arrHeader.length; i++) {
HeaderjsonStr += ‘"item’ + i + ‘":"’ + arrHeader[i] + ‘",’;
}
HeaderjsonStr = HeaderjsonStr.slice(0, HeaderjsonStr.length - 1);
HeaderjsonStr = HeaderjsonStr.slice(9, HeaderjsonStr.length);
HeaderjsonStr = ‘{’ + HeaderjsonStr + ‘}’;
HeaderjsonStr = HeaderjsonStr.replace("", “”);
HeaderjsonStr = HeaderjsonStr.replace(" ", “”);
data.push(JSON.parse(HeaderjsonStr));
//表格内容处理
var ContextJsonStr;
var context = $("div [lay-id='" + tableID + "']").find(".layui-table-body")[0].innerText;
var arrContextLine = context.split('');
var arrLine;
for (var i = 0; i < arrContextLine.length; i++) {
arrLine = arrContextLine[i].split('\n\t');
for (var j = 0; j < arrLine.length; j++) {
ContextJsonStr += '"item' + j + '":"' + arrLine[j] + '",';
}
ContextJsonStr = ContextJsonStr.slice(0, ContextJsonStr.length - 1);
if (i == 0) {
ContextJsonStr = ContextJsonStr.slice(9, ContextJsonStr.length);
}
ContextJsonStr = '{' + ContextJsonStr + '}';
ContextJsonStr = ContextJsonStr.replace("", "");
ContextJsonStr = ContextJsonStr.replace(" ", "");
data.push(JSON.parse(ContextJsonStr));
ContextJsonStr = "";
}
//底部合计内容
var ToTalJsonStr;
var total = $("div [lay-id='" + tableID + "']").find(".layui-table-total")[0].innerText;
var arrtotal = total.split('\n');
arrtotal = removeArray(arrtotal,' ');
var TotalJsonStr;
for (var i = 0; i < arrtotal.length; i++) {
TotalJsonStr += '"item' + i + '":"' + arrtotal[i] + '",';
}
TotalJsonStr = TotalJsonStr.slice(0, TotalJsonStr.length - 1);
TotalJsonStr = TotalJsonStr.slice(9, TotalJsonStr.length);
TotalJsonStr = '{' + TotalJsonStr + '}';
TotalJsonStr = TotalJsonStr.replace("\n", "");
TotalJsonStr = TotalJsonStr.replace("\t", "");
data.push(JSON.parse(TotalJsonStr));
//打印内容
var arrData = typeof data != 'object' ? JSON.parse(data) : data;
var rowNoms = 0;
for (let key in arrData[0]) {
rowNoms ++;
}
var excel = '<table>';
var rowData = "<tr align='center'>";
if (TableName != null) {
rowData += "<td colspan='" + rowNoms + "' style='align:'center',font-weight:bold ,vnd.ms-excel.numberformat:@@'>" + TableName + "</td>";
excel += rowData + "</tr>";
}
if (TableTwoHeader != null) {
rowData = "<tr align='center'>";
for (var j = 0; j < TableTwoHeader.length; j++) {
rowData += "<td colspan='" + rowNoms / TableTwoHeader.length + "' style='align:'center',font-weight:bold,vnd.ms-excel.numberformat:@@'>" + TableTwoHeader[j].TableName + "</td>";
}
excel += rowData + "</tr>";
}
for (var i = 0; i < arrData.length; i++) {
var rowData = "<tr align='left'>";
for (let key in arrData[i]) {
rowData += "<td style='vnd.ms-excel.numberformat:@@'>" + arrData[i][key] + "</td>";
}
excel += rowData + "</tr>";
}
excel += "</table>";
var excelFile = "<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'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
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>";
var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = TableName + ".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function removeArray(arr, val) {
for(var i = 0; i < arr.length; i++) {
if(arr[i] == val) {