layui动态表格导出复杂表头以下是我⾃⼰总结的解决办法(需下载excel.js⽂件):
第⼀(动态表格):
⾃定义表格(复杂表头)
function initHeader(){
var header1 =[]
,header2 =[];
//第⼀⾏标题
header1.push({title:'序号',type:'numbers',rowspan:2})
header1.push({field:'DisName',title:'渠道名称',minWidth:150,rowspan:2,totalRowText:'合计'});
header1.push({title:'⽀出',colspan:2});
header1.push({title:'收⼊',colspan:2});
header1.push({field:'RemainMoney',title:'⾦额',minWidth:90,rowspan:2});
//第⼆⾏标题
header2.push({field:'CustomerName',title:'终端名称',minWidth:180});
header2.push({field:'SettleMoney',title:'结算⾦额',minWidth:70,totalRow:true});
header2.push({field:'PaymentName',title:'回款类型',minWidth:60});
header2.push({field:'ReceivedMoney',title:'回款⾦额',minWidth:70,totalRow:true});
header.push(header1);
header.push(header2);
}
第⼆(初始化表格):
$.ajax({
type:"POST",
url:访问的Url,
contentType:"application/x-www-form-urlencoded;charset=utf-8",
dataType:"json",
data:{
//参数
},
async:true,//异步
success:function(res)
{
//成功后初始化表格
var result = res.data;
exportData = result;//赋值给导出的数据
elem:'#departTable'
layui下载
,cols: header
,data: result
,id:'departTable'
,page:true
,limit:20//任何你限制的数量
,totalRow:true
});
}
});
第三(导出):
//设置导出字段
exportData = excel.filterExportData(exportData,{
RowNumber:'RowNumber'
,DisName:'DisName'
,CustomerName:'CustomerName'
,CustomerName:'CustomerName'
...
//需要导出的字段
});
//设置导出表头
exportData.unshift({
CustomerName:'终端名称'
,SettleMoney:'结算⾦额'
,PaymentName:'回款类型'
,ReceivedMoney:'回款⾦额'
})
exportData.unshift({
RowNumber:'序号'
,DisName:'渠道名称'
,
CustomerName:'⽀出'
,SettleMoney:''
,PaymentName:'收⼊'
,ReceivedMoney:''
,RemainMoney:'余额'
})
//合并单元格
var mergeConf = excel.makeMergeConfig([
['A1','A2'],
['B1','B2'],
['C1','C2']
.
..
])
//设置样式
excel.setExportCellStyle(exportData,'A1:I2',{//A1-I2是范围区间
s:{
//设置居中
alignment:{
horizontal:'center',
vertical:'center'
}
}
},function(cell, newCell, row, config, currentRow, currentCol, fieldKey){
// 回调参数,cell:原有数据,newCell:根据批量设置规则⾃动⽣成的样式,
//row:所在⾏数据,config:传⼊的配置,currentRow:当前⾏索引,currentCol:当前列索引,fieldKey:当前字段索引return  newCell ;
});
//设置导出⽂件单元格宽度
//可以避免因单元格数据长度过长,显⽰科学计数法
var colConf =  excel.makeColConfig({
'A':30,
'B':150,
'C':100,
'D':150,
'E':100,
'F':100
...
},60)
sheet1: exportData
}, excel标题+'.xlsx','xlsx',{
extend:{
sheet1:{
'!merges': mergeConf,
'!cols': colConf
}
}
}
}); })

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。