1. 程式人生 > 實用技巧 >阿里巴巴-EasyExcel使用

阿里巴巴-EasyExcel使用

EasyExcel

概述

EasyExcel是一個基於Java的簡單、省記憶體的讀寫Excel的開源專案。在儘可能節約記憶體的情況下支援讀寫百M的Excel。

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

開源專案不容易,如果覺得本專案對您的工作還是有幫助的話,請在幫忙在點個★Star。

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實戰應用

匯入依賴

<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.0.0-beta5</version>
</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<StudentCurrent> all = this.studentCurrentService.findAll();

    List<StudentCurrentExcel> list = getNotPassData2(all);
    //寫入檔案資料
    EasyExcel.write(response.getOutputStream(), StudentCurrentExcel.class).sheet("員工考勤資訊").doWrite(list);
}

格式轉換

/**
* 將實體類物件 轉為Excel物件
* @param studentCurrentList
* @return
*/
private List<StudentCurrentExcel> getNotPassData2(List<StudentCurrent> studentCurrentList) {
    List<StudentCurrentExcel> list = 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;
}