pom依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>${easyexcel.version}</version> </dependency>
创建excel的Model数据
@Data public class DabaiModel extends BaseRowModel { @ExcelProperty(value = "用户名", index = 0) private String name; @ExcelProperty(value = "编号", index = 1) private String idCode; private String desc; }
未加@ExcelProperty的属性不会被解析写到excel中。
导出Excel
Controller层:
List<DabaiDto> list=dabaiDao.queryList(); List<DabaiModel>modelList=new ArrayList<>(list.size()); foreach(DabaiDto dto:list){ DabaiModel mo=new DabaiModel(); BeanUtils.copyProperties(dto,mo); } try{ String filename = URLEncoder.encode("导出excel名", "UTF-8") + ".xls"; response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment;filename="+filename); OutputStream outputStream = response.getOutputStream(); ExcelWriter writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLS,true); Sheet sheet1 = new Sheet(1,0, DabaiModel.class); sheet1.setSheetName("sheet1"); writer.write(modelList, sheet1); writer.finish(); }catch(IOException e){ }
@ExcelProperty