EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。 64M内存1分钟内读取75M(46W行25列)的Excel,当然还有急速模式能更快,但是内存占用会在100M多一点
- 方便在 web 环境下使用
easyexcel
,已上传至 maven 仓库
<dependency>
<groupId>com.pig4cloud.excel</groupId>
<artifactId>excel-spring-boot-starter</artifactId>
<version>0.0.8</version>
</dependency>
只需要在 Controller
层返回 List 并增加 @ResponseExcel
注解即可
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ResponseExcel {
String name() default "";
ExcelTypeEnum suffix() default ExcelTypeEnum.XLSX;
String password() default "";
String[] sheet() default {};
boolean inMemory() default false;
String template() default "";
String[] include() default {};
String[] exclude() default {};
Class<? extends WriteHandler>[] writeHandler() default {};
Class<? extends Converter>[] converter() default {};
Class<? extends HeadGenerator> headGenerator() default HeadGenerator.class;
}
- 返回单
sheet
, 全部字段导出
@ResponseExcel(name = "lengleng", sheet = "demoList")
@GetMapping("/e1")
public List<DemoData> e1() {
List<DemoData> dataList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
DemoData data = new DemoData();
data.setUsername("tr1" + i);
data.setPassword("tr2" + i);
dataList.add(data);
}
return dataList;
}
// 实体对象
@Data
public class DemoData {
private String username;
private String password;
}
- 自定义字段属性
@Data
public class DemoData {
@ColumnWidth(50) // 定义宽度
@ExcelProperty("用户名") // 定义列名称
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 40)
private String username;
@ExcelProperty("密码")
private String password;
}
- 忽略部分字段
@Data
public class DemoData {
@ColumnWidth(50) // 定义宽度
@ExcelProperty("用户名") // 定义列名称
@ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 40)
private String username;
@ExcelProperty("密码")
private String password;
}
@ResponseExcel(name = "lengleng", sheet = {"第一个sheet","第二个sheet"})
@GetMapping("/e1")
public List<List<DemoData>> e1() {
List<List<DemoData>> lists = new ArrayList<>();
lists.add(list());
lists.add(list());
return lists;
}
@ResponseExcel(name = "lengleng", sheet = "sheetName",password = "lengleng")
@GetMapping("/e1")
public List<List<DemoData>> e1() {
List<List<DemoData>> lists = new ArrayList<>();
lists.add(list());
lists.add(list());
return lists;
}
0.0.7
版本开始添加了全局自定义转换器注入的功能,你只需要将自定义的 Converter
注册成 Spring bean
即可。
示例代码如下(对 set 类型转换):
@Data
public class TestModel {
@ExcelProperty("名称集合")
private Set<String> nameSet;
}
/**
* 集合转换器
*
* @author L.cm
*/
@Component
public class SetConverter implements Converter<Set<?>> {
private final ConversionService conversionService;
SetConverter() {
this.conversionService = DefaultConversionService.getSharedInstance();
}
@Override
public Class<?> supportJavaTypeKey() {
return Set.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Set<?> convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String[] value = StringUtils.delimitedListToStringArray(cellData.getStringValue(), ",");
return (Set<?>) conversionService.convert(value, TypeDescriptor.valueOf(String[].class), new TypeDescriptor(contentProperty.getField()));
}
@Override
public CellData<String> convertToExcelData(Set<?> value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new CellData<>(StringUtils.collectionToCommaDelimitedString(value));
}
}
@ResponseExcel(name = "模板测试excel", sheet = "sheetName",template = "example.xlsx")
@GetMapping("/e1")
public List<DemoData> e1() {
return list();
}
- 理论上支持 alibaba/easyexcel v2.2.6 版本的大部分配置
- 支持 alibaba/easyexcel 原生的配置注解
- github stater 地址,可fork 魔改