1. 程式人生 > >一行程式碼實現Java的Excel讀寫--EasyExcel 的方法封裝

一行程式碼實現Java的Excel讀寫--EasyExcel 的方法封裝

EasyExcel

一. 依賴

首先是新增該專案的依賴,目前的版本是 1.0.2

<dependency>

        <groupId>com.alibaba</groupId>

        <artifactId>easyexcel</artifactId>

         <version>1.0.2</version>

</dependency>

二. 需要的類

                                            

1. ExcelUtil

工具類,可以直接呼叫該工具類的方法完成 Excel 的讀或者寫

2. ExcelListener

監聽類,可以根據需要與自己的情況,自定義處理獲取到的資料,我這裡只是簡單地把資料新增到一個 List 裡面。

public class ExcelListener extends AnalysisEventListener {

    //自定義用於暫時儲存data。
    //可以通過例項獲取該值
    private List<Object> datas = new ArrayList<>();

    /**
     * 通過 AnalysisContext 物件還可以獲取當前 sheet,當前行等資料
     */
    @Override
    public void invoke(Object object, AnalysisContext context) {
        //資料儲存到list,供批量處理,或後續自己業務邏輯處理。
        datas.add(object);
        //根據自己業務做處理
        doSomething(object);
    }

    private void doSomething(Object object) {
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        /*
            datas.clear();
            解析結束銷燬不用的資源
         */
    }

    public List<Object> getDatas() {
        return datas;
    }

    public void setDatas(List<Object> datas) {
        this.datas = datas;
    }
}

3. ExcelWriterFactroy

用於匯出多個 sheet 的 Excel,通過多次呼叫 write 方法寫入多個 sheet

4. ExcelException

捕獲相關 Exception

三. 讀取 Excel

讀取 Excel 時只需要呼叫 ExcelUtil.readExcel() 方法

@RequestMapping(value = "readExcel", method = RequestMethod.POST)
public Object readExcel(MultipartFile excel) {
    return ExcelUtil.readExcel(excel, new ImportInfo());
}

其中 excel 是 MultipartFile 型別的檔案物件,而 new ImportInfo() 是該 Excel 所對映的實體物件,需要繼承 BaseRowModel 類,如:

public class ImportInfo extends BaseRowModel {
    @ExcelProperty(index = 0)
    private String name;

    @ExcelProperty(index = 1)
    private String age;

    @ExcelProperty(index = 2)
    private String email;

    /*
        作為 excel 的模型對映,需要 setter 方法
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

作為對映實體類,通過 @ExcelProperty 註解與 index 變數可以標註成員變數所對映的列,同時不可缺少 setter 方法

四. 匯出 Excel

1. 匯出的 Excel 只擁有一個 sheet

只需要呼叫 ExcelUtil.writeExcelWithSheets() 方法:

@RequestMapping(value = "writeExcel", method = RequestMethod.GET)
public void writeExcel(HttpServletResponse response) throws IOException {
    List<ExportInfo> list = getList();
    String fileName = "一個 Excel 檔案";
    String sheetName = "第一個 sheet";

    ExcelUtil.writeExcel(response, list, fileName, sheetName, new ExportInfo());
    }

fileName,sheetName 分別是匯出檔案的檔名和 sheet 名,new ExportInfo() 為匯出資料的對映實體物件,list 為匯出資料。

對於對映實體類,可以根據需要通過 @ExcelProperty 註解自定義表頭,當然同樣需要繼承 BaseRowModel 類,如:

public class ExportInfo extends BaseRowModel {
    @ExcelProperty(value = "姓名" ,index = 0)
    private String name;

    @ExcelProperty(value = "年齡",index = 1)
    private String age;

    @ExcelProperty(value = "郵箱",index = 2)
    private String email;

    @ExcelProperty(value = "地址",index = 3)
    private String address;
}

value 為列名,index 為列的序號

如果需要複雜一點,可以實現如下圖的效果:

對應的實體類寫法如下:

public class MultiLineHeadExcelModel extends BaseRowModel {

    @ExcelProperty(value = {"表頭1","表頭1","表頭31"},index = 0)
    private String p1;

    @ExcelProperty(value = {"表頭1","表頭1","表頭32"},index = 1)
    private String p2;

    @ExcelProperty(value = {"表頭3","表頭3","表頭3"},index = 2)
    private int p3;

    @ExcelProperty(value = {"表頭4","表頭4","表頭4"},index = 3)
    private long p4;

    @ExcelProperty(value = {"表頭5","表頭51","表頭52"},index = 4)
    private String p5;

    @ExcelProperty(value = {"表頭6","表頭61","表頭611"},index = 5)
    private String p6;

    @ExcelProperty(value = {"表頭6","表頭61","表頭612"},index = 6)
    private String p7;

    @ExcelProperty(value = {"表頭6","表頭62","表頭621"},index = 7)
    private String p8;

    @ExcelProperty(value = {"表頭6","表頭62","表頭622"},index = 8)
    private String p9;
}

2. 匯出的 Excel 擁有多個 sheet

呼叫 ExcelUtil.writeExcelWithSheets() 處理第一個 sheet,之後呼叫 write() 方法依次處理之後的 sheet,最後使用 finish() 方法結束

public void writeExcelWithSheets(HttpServletResponse response) throws IOException {
    List<ExportInfo> list = getList();
    String fileName = "一個 Excel 檔案";
    String sheetName1 = "第一個 sheet";
    String sheetName2 = "第二個 sheet";
    String sheetName3 = "第三個 sheet";

    ExcelUtil.writeExcelWithSheets(response, list, fileName, sheetName1, new ExportInfo())
                .write(list, sheetName2, new ExportInfo())
                .write(list, sheetName3, new ExportInfo())
                .finish();
}

write 方法的引數為當前 sheet 的 list 資料,當前 sheet 名以及對應的對映類

連結:https://juejin.im/post/5ba320546fb9a05d0b14304b