问题描述
com.alibaba.excel.exception.ExcelDataConvertException: Can not find ‘Converter’ support class List.
问题解释
EasyExcel开源框架中Converter接口的convertToExcelData只实现了转换BigDecimal、Bolean、Byte[]、btye[]、Byte、Date、Double、File、Float、InputStream、Integer、Long、Short、URL这些类型,意味着参数data最多只能是个二维数据,但是本次业务逻辑需要转换List< String >的数据。
实体类数据
/**
* 名下企业名称
*/
@ExcelProperty(value = "名下企业名称",index = 12)
private List<String> enterpriseList;
解决办法
编写自定义Converter转换器:
@Component
public class ListConverter implements Converter<List> {
@Override
public Class supportJavaTypeKey() {
return List.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public List convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
String stringValue = cellData.getStringValue();
String[] split = stringValue.split(",");
List<String> enterpriseList = new ArrayList<>();
for(int i = 0; i < split.length; i++){
enterpriseList.add(split[i]);
}
return enterpriseList;
}
@Override
public CellData convertToExcelData(List list, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
list.forEach(o -> {
String s = o.toString();
stringBuilder.append(s+",");
});
return new CellData(stringBuilder.toString());
}
}