easyexcel 导入SpringBoot搭配EasyExcel,一切表格就这么完美解决了

2022-07-19 08:09:14

点击蓝字关注我们

作者:GreenCode

出处:https://www.cnblogs.com/nayou/p/13896456.html


# 概述

EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。

github地址:https://github.com/alibaba/easyexcel

# EasyExcel控制表格注解

@ContentRowHeight(int):

设置 row 高度,不包含表头标记在 类上@ContentRowHeight(15) //设置行高

@HeadRowHeight(int):

设置 表头 高度(与 @ContentRowHeight 相反)标记在 类上@HeadRowHeight(20)  //设置表头高度

@ColumnWidth(int):

设置列宽标记在属性上@ColumnWidth(20)  //设置列宽

@ExcelProperty(value = String[], index = int):

设置表头信息value: 表名称index: 列号@ExcelProperty(index = 0, value = "学生名字")

# SpringBoot实战应用

导入依赖

<dependency>    <groupId>com.alibabagroupId>    <artifactId>easyexcelartifactId>    <version>2.0.0-beta5version>dependency>

创建Excel实体类对象

@Data@ContentRowHeight(15)@HeadRowHeight(20)@ColumnWidth(20)public class StudentCurrentExcel {    @ExcelProperty(index = 0, value = "学生名字")    private String studentName;    @ExcelProperty(index = 1, value = "性别")    private String gender;    @ExcelProperty(index = 2, value = "年级/班级")    private String deptCode;    @ExcelProperty(index = 3, value = "通道名称")    private String screenName;    @ExcelProperty(index = 4, value = "通行方向")    private String direction;    @ExcelProperty(index = 5, value = "通行时间")    private Date passageTime;    @ExcelProperty(index = 6, value = "体温")    private String temperate;    @ExcelProperty(index = 7, value = "组织结构")    private Integer deptId;}

导出Controller

@UnAuthorized@ApiOperation(value = "导出学生通行记录")@GetMapping("studentCurrents/export")public void exportStudentCurrents(HttpServletResponse response, HttpServletRequest request) throws IOException {    //设置响应类型    response.setContentType("application/vnd.ms-excel");    //设置字符编码    response.setCharacterEncoding("utf-8");    //设置文件名字    String fileName = "学生通行记录" + DateUtils.getDate("yyyy-MM-dd HH:mm") + ".xlsx";    //设置响应头信息    response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));    List all = this.studentCurrentService.findAll();    List list = getNotPassData2(all);    //写入文件数据    EasyExcel.write(response.getOutputStream(), StudentCurrentExcel.class).sheet("员工考勤信息").doWrite(list);}

格式转换

/*** 将实体类对象 转为Excel对象* @param studentCurrentList* @return*/private List getNotPassData2(List studentCurrentList) {    Listlist = new ArrayList<>();    for (StudentCurrent studentCurrent : studentCurrentList) {        StudentCurrentExcel excel = new StudentCurrentExcel();        Integer gender = studentCurrent.getGender();        excel.setStudentName(studentCurrent.getStudentName());        excel.setGender(studentCurrent.getGender() == 4 ? "男" : "女");        excel.setDeptCode(studentCurrent.getDeptCode());        excel.setDeptId(studentCurrent.getDeptId());        excel.setPassageTime(studentCurrent.getPassageTime());        excel.setTemperate(studentCurrent.getTemperate());        excel.setScreenName(studentCurrent.getScreenName());        list.add(excel);    }    return list;}

是不是很easy啊?

  • 作者:言三岁
  • 原文链接:https://blog.csdn.net/weixin_42376940/article/details/113068088
    更新时间:2022-07-19 08:09:14