ExcelWriteDataConvertException:Can not find ‘Converter‘ support class Character.

问题

今天使用 EasyExcel 来进行写操作时,出现如下错误:

com.alibaba.excel.exception.ExcelWriteDataConvertException: Can not find 'Converter' support class Character.
	at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.doConvert(AbstractExcelWriteExecutor.java:323) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.convert(AbstractExcelWriteExecutor.java:277) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.write.executor.AbstractExcelWriteExecutor.converterAndSet(AbstractExcelWriteExecutor.java:58) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addJavaObjectToExcel(ExcelWriteAddExecutor.java:174) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.addOneRowOfDataToExcel(ExcelWriteAddExecutor.java:82) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.write.executor.ExcelWriteAddExecutor.add(ExcelWriteAddExecutor.java:58) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:59) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:73) ~[easyexcel-3.0.5.jar:na]
	at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:50) ~[easyexcel-3.0.5.jar:na]

主要是 Can not find ‘Converter’ support class Character.

解决

出现上面错误的原因是我 pojo 类中的性别字段是 char 类型,而 EasyExcel 目前还不支持 char 类型的转换。EasyExcel 支持的类型在枚举类 CellDataTypeEnum 中有定义。
ExcelWriteDataConvertException:Can not find ‘Converter‘ support class Character._第1张图片
可以看到,并不支持 char 类型,因此,需要我们自定义 convert。这里的 convert 不是 SpringBoot 的,是 EasyExcel 的。

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.springframework.stereotype.Component;

/**
 * @author zxb 2022/4/12 17:52
 */
@Component
public class CharConverter implements Converter<Character> {

    /**
     * 开启对 Character 类型的支持
     */
    @Override
    public Class<?> supportJavaTypeKey() {
        return Character.class;
    }


    /**
     * 自定义对 Character 类型数据的处理
     * 我这里就拿 String 去包装了下
     */
    @Override
    public WriteCellData<?> convertToExcelData(Character value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData<Character>(String.valueOf(value));
    }
}

定义完自定义 convert 后,需要在写操作时,将 convert 注册到 EasyExcel 中去。

    @RequestMapping(path = "/writeStudentToExcel", method = RequestMethod.GET)
    public Result writeStudentToExcel() {
        String fileName = "E:\\" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        List<Student> studentList = listAllStudent();
        log.info("{}",studentList);
        EasyExcel.write(fileName, Student.class).registerConverter(new CharConverter()).sheet("sheet1").doWrite(studentList);
        return Result.success();
    }

在这里插入图片描述
问题解决!

你可能感兴趣的:(#,SpringBoot,java,easyexcel,springboot)