阿里easyexcel导出图片 图片路径失效excel写出空白解决

1.定义自己的Converter,不使用默认的StringImageConverter

public class MyStringImageConverter implements Converter {
    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

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

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        throw new UnsupportedOperationException("Cannot convert images to string");
    }

    //图片失效处理
    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
                                       GlobalConfiguration globalConfiguration) throws IOException {
        File file = new File(value);
        if(file.exists()){
            //文件存在
            return new CellData(FileUtils.readFileToByteArray(new File(value)));
        }
        //直接返回文字描述
        //FileUtils.readFileToByteArray(new File("/home/test.jpg"))
        return new CellData("无法加载图片");
    }

}

2.更改图片字段注解

@ExcelProperty(value = {"图片"},index = 0,converter = MyStringImageConverter.class)
private String siteImages;

 

你可能感兴趣的:(后端)