当前位置: 首页 > >

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

发布时间:

1.1 使用String类型导出 ? 定义自己的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("无法加载图片");
}

}

1.2.更改图片字段注解


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

2.1使用URL类型导出 同理定义自己的Converter 处理异常


public class MyUrlImageConverter implements Converter {
@Override
public Class supportJavaTypeKey() {
return URL.class;
}

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

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

@Override
public CellData convertToExcelData(URL value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws IOException {
InputStream inputStream = null;
try {
//开启连接
URLConnection uc = value.openConnection();
URL url = null;
//获取响应状态
int statusCode = ((HttpURLConnection) uc).getResponseCode();
switch (statusCode){
case 200:
inputStream = value.openStream();
break;
case 404:
//默认给一个图片
url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http%3A%2F%2Fpic.616pic.com%2Fys_bnew_img%2F00%2F11%2F69%2Fj2AjnHspwT.jpg");
inputStream = url.openStream();
break;
default :
url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http%3A%2F%2Fpic.616pic.com%2Fys_bnew_img%2F00%2F11%2F69%2Fj2AjnHspwT.jpg");
inputStream = url.openStream();
break;
}
byte[] bytes = IoUtils.toByteArray(inputStream);
return new CellData(bytes);
}catch (ConnectException exception){
//捕获下链接异常
URL url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http%3A%2F%2Fpic.616pic.com%2Fys_bnew_img%2F00%2F11%2F69%2Fj2AjnHspwT.jpg");
inputStream = url.openStream();
byte[] bytes = IoUtils.toByteArray(inputStream);
return new CellData(bytes);
}catch (FileNotFoundException fileNotFoundException){
URL url = new URL("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598096095144&di=9a72ad26e83effb9341c711c9818b85f&imgtype=0&src=http%3A%2F%2Fpic.616pic.com%2Fys_bnew_img%2F00%2F11%2F69%2Fj2AjnHspwT.jpg");
inputStream = url.openStream();
byte[] bytes = IoUtils.toByteArray(inputStream);
return new CellData(bytes);
}finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}

2.2字段处理使用自定义MyUrlImageConverter


@ExcelProperty(value = {"头像"},index = 2,converter = MyUrlImageConverter.class)
private URL img;

其他类型导出一样做下处理



友情链接: