91名师指路-头部
91名师指路

easyexcel(二十四):easyexcel多列表组合填充

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

一:代码

@GetMapping("/download")
public void download(HttpServletResponse response) throws IOException {
ClassPathResource resource=new ClassPathResource("template/user.xlsx");
InputStream inputStream=resource.getInputStream();

// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
String fileName = System.currentTimeMillis() + ".xlsx";
response.reset();
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
OutputStream os=response.getOutputStream();

List<UserInfo> userList=new ArrayList<>();
UserInfo ui1=new UserInfo();
ui1.setName("张无忌");
ui1.setAge(20);
UserInfo ui2=new UserInfo();
ui2.setName("赵敏");
ui2.setAge(18);

userList.add(ui1);
userList.add(ui2);

List<UserCity> cityList=new ArrayList<>();
UserCity uc1=new UserCity();
uc1.setCity("上海");
uc1.setAddress("上海火车站");
UserCity uc2=new UserCity();
uc2.setCity("北京");
uc2.setAddress("北京西站");

cityList.add(uc1);
cityList.add(uc2);

try (ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(inputStream).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
excelWriter.fill(new FillWrapper("data1", userList), fillConfig, writeSheet);
excelWriter.fill(new FillWrapper("data2", cityList), fillConfig, writeSheet);
}
}


二:模板


三:效果图






2024-02-04 09:22:48     阅读(423)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部