EasyExcel使用
Posted 云村的王子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 + \'}\'; } }
以上是关于EasyExcel使用的主要内容,如果未能解决你的问题,请参考以下文章
使用EasyExcel将MySQL中查询出来的数据下载到Excel文件中
使用easyexcel导入表格时接收前端的参数,并传入监听器