EasyExcel使用
将下面三层结合起来,请放心食用。
一、controller层
@RestController public class EasyExcelController { private Logger logger = LogManager.getLogger(EasyExcelController.class); @RequestMapping(value = "/Download") public void testExcelDownload(HttpServletResponse response) { //实际项目中,personList可以从数据库查出或者前端传过来 //在这里用于测试 List<Person> personList = new ArrayList<>(); for (int i = 1; i < 5; i++) { Person person = new Person(); person.setId(i); person.setName(i + "号杰瑞"); person.setAge(i); person.setAddress(i + "号石华街"); person.setDate(new Date()); personList.add(person); } try { String fileName = "数据"; // Excel文件名称 String sheetName = "欢乐番茄"; // 工作簿名称 EasyExcelUtil.writeExcel(response, personList, fileName, sheetName, Person.class); } catch (Exception e) { logger.error("模板下载失败", e); } } }
二、工具类
public class EasyExcelUtil { public static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception { try { fileName = URLEncoder.encode(fileName, "utf-8"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); // 此处指定了文件类型为xls,如果是xlsx的,请自行替换修改 response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls"); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "no-store"); response.addHeader("Cache-Control", "max-age=0"); return response.getOutputStream(); } catch (IOException e) { throw new Exception("导出文件失败!"); } } public static void writeExcel(HttpServletResponse response, List list, String fileName, String sheetName, Class clazz) throws Exception { ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLS); Sheet sheet = new Sheet(1, 0, clazz); sheet.setSheetName(sheetName); writer.write(list, sheet); writer.finish(); } }
三、实体类
@Data //自定义标题行高 @HeadRowHeight(30) //自定义标题字体大小 默认14 @HeadFontStyle(fontHeightInPoints = 15) //自定义文本内容行高 @ContentRowHeight(20) //自定义文本字体大小 默认14 @ContentFontStyle(fontHeightInPoints = 10) public class Person { //忽略这个字段 @ExcelIgnore private Integer id; @ExcelProperty({"数据统计", "姓名"}) private String name; @ExcelProperty({"数据统计", "年龄"}) private Integer age; @ExcelProperty({"数据统计", "家庭地址"}) @ColumnWidth(15) private String address; //默认 yyyy-MM-dd HH:mm:ss 可以使用DateTimeFormat自定义日期格式 @DateTimeFormat("yyyy年MM月dd日") //自定义列宽 @ColumnWidth(15) @ExcelProperty("日期") private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Person{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + ", date=" + date + '}'; } }
POM依赖
<!--ali--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency>