Java-Layui导出
引入excel.js
<script src="../../static/js/excel.js"></script>
script
layui.use(['table', 'form', 'laydate','excel'], function () {......
var loading = layer.load(1, {
content: '数据加载中...', shade: [0.8, '#393D49'], success: function (layero) {
layero.css('padding-left', '30px');
layero.find('.layui-layer-content').css({
'padding-top': '40px',
'width': '90px',
'color': 'white',
'background-position-x': '16px'
});
}
});
var reqData = {};
var starttime = $('#starttime').val();
if(starttime == undefined || starttime.toString().trim().length == 0){
layer.close(loading);
layer.alert("请输入开始时间", {icon: 16, time: 0});
return false;
}
reqData.starttime = starttime;
var endtime = $('#endtime').val();
if(endtime == undefined || endtime.toString().trim().length == 0){
layer.close(loading);
layer.alert("请输入结束时间", {icon: 16, time: 0});
return false;
}
reqData.endtime = endtime;
var typeStr = $('#typeStr').val();
if(typeStr == undefined || typeStr.toString().trim().length == 0){
layer.close(loading);
layer.alert("请选择导出数据类型时间", {icon: 16, time: 0});
return false;
}
reqData.typeStr = typeStr;
reqData.page = 1;
reqData.rows = 999999;
$.ajax({
method: "POST",
url: url + "/services/inner/basic/dataStatistics/exportFile",
contentType: 'application/json',
data: JSON.stringify(reqData),
success: function (res) {
layer.close(loading);
if (res.code == 0) {
var fieldArr = [];
var fieldArrStr = {};
$.each(res.fieldVoList, function (index,item) {
fieldArr.push(item.fieldStr);
var aa = item.fieldStr;
fieldArrStr[item.fieldStr] = item.fieldStr;//重中之重
});
var data = res.list;
// 重点!!!如果后端给的数据顺序和映射关系不对,请执行梳理函数后导出,定义列
data = excel.filterExportData(data, fieldArr);
// 重点2!!!一般都需要加一个表头,表头的键名顺序需要与最终导出的数据一致
data.unshift(fieldArrStr);
var dateStr = layui.util.toDateString(new Date(), "yyyyMMddHHmmss");
if(typeStr == 1){
excel.exportExcel(data, "出口类型明细综合查询" + dateStr + '.xlsx', 'xlsx');
}else if (typeStr == 2){
excel.exportExcel(data, "出口类型合计查询" + dateStr + '.xlsx', 'xlsx');
}else if (typeStr == 3){
excel.exportExcel(data, "进口类型明细查询" + dateStr + '.xlsx', 'xlsx');
}else if (typeStr == 4){
excel.exportExcel(data, "进口类型合计查询" + dateStr + '.xlsx', 'xlsx');
}
layer.msg('导出成功!')
} else {
layer.msg('没有导出数据')
}
},
error: function () {
layer.close(loading);
layer.msg('操作失败');
}
});
});