加载excel
layui.config({
base: '__PUBLIC__/YlJianguan/layuiadmin/' //静态资源所在路径
}).extend({
index: 'lib/index' //主入口模块
}).use(['index', 'table', 'form', 'laydate', 'element','excel'], function () {
var $ = layui.$
, table = layui.table
, laydate = layui.laydate
, element = layui.element
,excel = layui.excel
, form = layui.form;
//批量导出excel
$('.export').on('click', function(){
if (index == 0) {
var time_start = $('#time_start1').val()
var time_end = $('#time_end1').val()
} else {
var time_start = $('#time_start').val()
var time_end = $('#time_end').val()
}
if (time_start && time_end && time_end < time_start) {
layer.msg('结束时间不能小于开始时间', {
icon: 2,
time: 2000 //2秒关闭(如果不配置,默认是3秒)
})
return false;
}
if ((time_start && !time_end) || (!time_start && time_end)) {
layer.msg('开始和结束时间必须全选', {
icon: 2,
time: 2000 //2秒关闭(如果不配置,默认是3秒)
})
return false;
}
// 模拟从后端接口读取需要导出的数据
$.ajax({
url: "__URL__/exportServiceSubsidyStatisticsH"
,dataType: 'json'
,data: {time_start:time_start,time_end:time_end}
,beforeSend:function () {
layer.load(1, {
shade: [0.2,'#fff'] //0.1透明度的白色背景
});
} //发送请求
,success(res) {
res = excel.filterExportData(res, [
'months'
,'sum_amount'
,'clearAmount'
]);
// 重点2!!!一般都需要加一个表头,表头的键名顺序需要与最终导出的数据一致
res.unshift({ months: "月份",sum_amount:'当月消费总金额', clearAmount: "当月清零金额"});
excel.exportExcel({
sheet1: res
}, '消费金额统计.xlsx', 'xlsx');
layer.closeAll();
}
,error() {
layer.msg('获取数据失败!', {
icon: 2,
time: 2000 //2秒关闭(如果不配置,默认是3秒)
}, function(){
layer.closeAll();
});
}
});
});