layui使⽤EasyExcel进⾏模板填充,进⾏下载Excel模板:
{xxx}属于Map填充,key对应xxx,value放⼊值。
{.xxx}(这⾥有个点)属于List填充,xxx对应List的值名称,如果是List<;实体类>,xxx则对应实体类的属性
前端:
使⽤a标记作为链接下载
<script>
function exportExcel(){//使⽤按钮点击进⾏下载
var demoReload=$('#param').val();
var url = '地址';
//创建⼀个a标签进⾏下载
var a = ateElement('a');
a.href = url+'?param='+demoReload+'&excel_id=1';
$('body').append(a);  // 修复firefox中⽆法触发click
a.click();
$(a).remove();
layui下载setTimeout(function(){
layer.msg("导出结束", {
icon:1,
time:500
},function(){
load();
});
}, 2000);
}
</script>
后台:
controller只需要接收前端传来到参数,进⾏查询,放⼊List<;实体类>⾥。
@RequestMapping("/exportExcelEmpPayrollAccount")
public void exportExcelEmpPayrollAccount(String param,Integer excel_id,        HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("payrollName", param);
//查询名称返回的结果
List<EmpPayrollAccountVO> list = baseService.queryExceltLedger(param);
excelTemplate.ExcelTemplate(excel_id, list, map, response);
}
public void ExcelTemplate(Integer excel_id,List list, Map<String,Object> map, HttpServletResponse response){        String name="";
switch(excel_id){
case 1:
name="模板名称.xlsx";
break;
case 2:
name="模板名称.xlsx";
break;
case 3:
name="模板名称.xlsx";
break;
case 4:
name="模板名称.xlsx";
break;
case 5:
name="模板名称.xlsx";
break;
}
String ModelPath()+"\\"+name;//到本地模板路径
ExcelWriter excelWriter=null;
try {
String placeAll(".xlsx","");
String fileName = s + Date();//导出的⽂件名称
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这⾥de可以防⽌中⽂乱码当然和easyexcel没有关系
String filename = de(fileName, "utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xls");
/
/使⽤OutputStream()下载,并使⽤项⽬下的模板填充
excelWriter = EasyExcel.OutputStream()).withTemplate(templateFileName).build();            WriteSheet writeSheet = EasyExcel.writerSheet().build();
if(map!=null){
excelWriter.fill(map, writeSheet);//存⼊map
}
if(list!=null){
excelWriter.fill(list, writeSheet);//存⼊list
}
excelWriter.finish();
} catch (Exception e) {
// 重置response
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
R<String> r = new R<>();
r.setCode(CommonConstants.FAIL);
r.setMsg("导出失败!");
r.Message());
try {
} catch (IOException e1) {
throw new NetException(500,"导出失败!");
}
throw new NetException(500,"导出失败!");
}
}
下载结果:
官⽅链接:

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