天天看點

使用EasyPoi快速實作excel導入導出功能

easypoi功能如同名字easy,主打的功能就是容易,讓一個沒見接觸過poi的人員 就可以友善的寫出Excel導出,Excel模闆導出,Excel導入,Word模闆導出,通過簡單的注解和模闆 語言(熟悉的表達式文法),完成以前複雜的寫法EasyPoi快速實作excel導入導出功能

添加依賴

<!--easypoi依賴-->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-web</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.2.0</version>
</dependency>
           

導入功能實作

建立一個excel,填充資料

使用EasyPoi快速實作excel導入導出功能

建立一個學生實體類

public class StudentDo {
    /**
     * @Excel 作用在一個filed上面,對列的描述
     * name 列名
     * orderNum 下标,從0開始
     */
    @Excel(name = "姓名", orderNum = "0")
    private String name;
    @Excel(name = "性别", orderNum = "1")
    private String gender;
    @Excel(name = "成績", orderNum = "2")
    private String score;

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }
}
           

在每列增加@Excel注解,name對應列名,

控制層:

@RestController
@RequestMapping("student")
public class StudentController {
    /**
     * excel導入資料
     * @param file
     * @return
     */
    @RequestMapping("import")
    public List<StudentDo> importData(@RequestParam(value = "file") MultipartFile file) {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);
        try {
            List<StudentDo> list = ExcelImportUtil.importExcel(file.getInputStream(), StudentDo.class, importParams);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
           

經過上面的配置,就可以完成excel的資料導入了。

測試:

使用EasyPoi快速實作excel導入導出功能

導出功能實作

控制層

/**
 * 資料導出excel
 * @return
 */
@RequestMapping("export")
public void exportData(HttpServletResponse response) {
    try {
        List<StudentDo> list = new ArrayList<>();
        StudentDo studentDo1 = new StudentDo();
        studentDo1.setName("孫常勝");
        studentDo1.setGender("男");
        studentDo1.setScore("100");
        list.add(studentDo1);
        String fileName = "學生資訊表.xls";
        String sheetName = "學生";
        response.setHeader("content-Type", "application/vnd.ms-excel");
        fileName = new String(fileName.getBytes(), "ISO-8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        ServletOutputStream out = response.getOutputStream();
        ExportParams params = new ExportParams(); //sheet
        params.setSheetName(sheetName);
        params.setTitle("學生資訊表,标題");
        Workbook workbook = ExcelExportUtil.exportExcel(params, StudentDo.class, list);
        workbook.write(out);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
           

這裡是測試資料,在浏覽器中通路,會自動生成excel檔案。

使用EasyPoi快速實作excel導入導出功能

至此,EasyPoi簡單的excel導入導出就完成了。