原 easyexcel(二十四):easyexcel多列表组合填充
版权声明:本文为博主原创文章,请尊重他人的劳动成果,转载请附上原文出处链接和本声明。
本文链接:https://www.91mszl.com/zhangwuji/article/details/1474
@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 阅读(424)
名师出品,必属精品 https://www.91mszl.com
博主信息