前言
主要是來了解如何簡單的使用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)大好用!
