easyExcel基本簡單導(dǎo)出功能,入門級(jí)上手就會(huì)!

前言

主要是來了解如何簡單的使用eaysExcel,對(duì)于EasyExcel 是一個(gè)用于讀寫 Excel 的 Java 庫,它的主要優(yōu)點(diǎn)是處理大數(shù)據(jù)量時(shí),內(nèi)存占用極低,因此在性能和效率上都有很好的表現(xiàn)。
官方網(wǎng)址
https://easyexcel.opensource.alibaba.com/docs/current/
可以提供導(dǎo)出模板或者自定義模板去填充數(shù)據(jù),
本次就列舉一些基本自帶的導(dǎo)出操作

引入依賴

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.3</version>
        </dependency>

定義實(shí)例類

@Getter
@Setter
@EqualsAndHashCode
public class StudentVO {

    @ExcelProperty("id")
    @ColumnWidth(20)
    private int id;

    @ExcelProperty("名稱")
    @ColumnWidth(20)
    private String name;

    @ExcelProperty(value = "性別", converter = GenderConverter.class)
    @ColumnWidth(10)
    private Integer gender;

    @ExcelProperty("創(chuàng)建時(shí)間")
    @ColumnWidth(20)
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date createTime;
}
  • @ExcelProperty是列表的標(biāo)題,
  • @ColumnWidth(20)是導(dǎo)出的列表的寬,
  • @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")格式化時(shí)間
    以及其它屬性可以去看看


    image.png

converter 轉(zhuǎn)換類

@ExcelProperty屬性里有個(gè)conveter是用來轉(zhuǎn)換枚舉的,例如將性別(1轉(zhuǎn)成男,2轉(zhuǎn)成女)


image.png

GenderConverter

新增一個(gè)GenderConverter 對(duì)性別轉(zhuǎn)換

public class GenderConverter implements Converter<Integer> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) {
        return GenderEnum.convert(context.getReadCellData().getStringValue()).getValue();
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        return new WriteCellData<>(GenderEnum.convert(context.getValue()).getDescription());
    }
}

注意:
對(duì)于導(dǎo)出的話只要注意這個(gè)convertToExcelData方法,導(dǎo)出的時(shí)候他會(huì)根據(jù)你的枚舉類型轉(zhuǎn)換成對(duì)應(yīng)值,convertToJavaData這個(gè)方法是對(duì)于導(dǎo)入的時(shí)候用到的,這里暫不使用,也可以去掉

性別枚舉

@Getter
@AllArgsConstructor
public enum  GenderEnum {

    /**
     * 未知
     */
    UNKNOWN(0, "未知"),

    /**
     * 男性
     */
    MALE(1, "男性"),

    /**
     * 女性
     */
    FEMALE(2, "女性");

    private final Integer value;

    @JsonFormat
    private final String description;

    public static GenderEnum convert(Integer value) {
        return Stream.of(values())
                .filter(bean -> bean.value.equals(value))
                .findAny()
                .orElse(UNKNOWN);
    }

    public static GenderEnum convert(String description) {
        return Stream.of(values())
                .filter(bean -> bean.description.equals(description))
                .findAny()
                .orElse(UNKNOWN);
    }

}

導(dǎo)出接口

    @RequestMapping(value = "/doEasyExcel")
    public void doEasyExcel(HttpServletResponse response) throws IOException {
        try {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String newFileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + “學(xué)生基本信息” + ".xlsx");
                List<StudentVO> list = StudentVO.getList();
            EasyExcel.write(response.getOutputStream())
                    .head(StudentVO.class)
                    // excle 格式
                    .excelType(ExcelTypeEnum.XLSX)
                     // 導(dǎo)出sheet名稱
                    .sheet("用戶列表")
                    .doWrite(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

導(dǎo)出結(jié)果

image.png

結(jié)尾

主要是基本學(xué)習(xí)easyExcel基本的導(dǎo)出功能,用于easyExcel自帶的導(dǎo)出模板??傮w來說easyExcel還是挺強(qiáng)大好用!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容