一、导入layui资源
在页面写导出按钮
<input type="hidden" id="excel" value="${time}">//获取数据的条件,可无
<th id="exc" onclick="setExcel()" lang>excel</th>
<script>
function setExcel() {
var time = document.getElementById("excel").value;//获取数据的参数,可无
$.ajax({
cache: false,
type: "post",
url: "${ctx}/Order/excel2.do",//获取数据后台接口
data: "time=" + time,
async: false,
success: function (res) {
var na = res.name;
var tableStr = '<table border="0" cellspacing="" cellpadding="">'
tableStr += '<tr style="font-size:16px;">';
for (var i = 0; i < na.length; i++) {//动态添加表头
tableStr += '<th width="15%">' + na[i] + '</th>';
}
tableStr += '</tr>';
var len = res.list.length;
var data = res;
var count = 0;
for (var i = 0; i < len; i++) {5
tableStr += '<tr style="font-size:16px;">';
tableStr += '<td>' + (i + 1) + '</td>';
tableStr += '<td>' + res.list[i].roomNumber + '</td>';
tableStr += '<td>' + res.list[i].PHP + '</td>';
tableStr += '<td>' + res.list[i].RMB + '</td>';
tableStr += '<td>' + res.list[i].rent + '</td>';
tableStr += '<td>' + res.list[i].water + '</td>';
tableStr += '<td>' + res.list[i].electricity + '</td>';
/* tableStr += '<td>' + res.list[i].maintenanceCost + '</td>';*/
tableStr += '<td>' + res.list[i].network + '</td>';
tableStr += '<td>' + res.list[i].buildingManagementFee + '</td>';
for (var d = 0; d < data.list[i].dlist.length; d++) {
tableStr += '<td>' + data.list[i].dlist[d].mm + '</td>';
}
/* tableStr += '<td>' + res.list[i].linenCleaningfee + '</td>';
tableStr += '<td>' + res.list[i].dailySupplies + '</td>';
tableStr += '<td>' + res.list[i].otherExpenses + '</td>';*/
tableStr += '<td>' + res.list[i].count + '</td>';
tableStr += '</tr>';
count++;
}
if (len == count) {
tableStr += '<tr style="font-size:16px;">';
tableStr += '<td>' + (len + 1) + '</td>';
tableStr += '<td colspan="11">接单提成walkinguestdapfasom</td>';
tableStr += '<td>' + res.booking + '</td>';
tableStr += '</tr>';
tableStr += '<tr style="font-size:16px;">';
tableStr += '<td>' + (len + 2) + '</td>';
tableStr += '<td colspan="11">合计Total(PHP)</td>';
tableStr += '<td>' + res.sumPHP + '</td>';
tableStr += '</tr>';
tableStr += '<tr style="font-size:16px;">';
tableStr += '<td>' + (len + 3) + '</td>';
tableStr += '<td colspan="11">合计Total(CNY)</td>';
tableStr += '<td>' + res.sumCNY + '</td>';
tableStr += '</tr>';
}
tableStr += '</table>';
exporExcel(time + "财务报表", tableStr);
}
});
}
/**
* 注:如果想设置单元格格式,比如数字太多,默认导出会按科学计数法转换,这个时候要写成文本格式
* 可以这样使用 在td 上 使用style;如:<td style='mso-number-format:"@";'>第一行 </td>
* style='mso-number-format:"@";' 转文本
* **/
/**
* @params: FileName:导出Excel的文件名称,excel:需要导出的table
* 如果没有table列表,只有json数据的话,将json数据拼接成table字符串模板即可
* **/
function exporExcel(FileName, excel) {
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 = FileName; //格式默认为.xls
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>