EasyExcel导出图片到Excel

EasyExcel导出图片到Excel,需要自己重新写转换器,但是1000张以下导出是没问题的,10000以上会卡顿,有意向的小伙伴可以一起讨论下,如何处理大数量的图片导出到excel;

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 com.alibaba.excel.util.IoUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;

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

    // 图片失效处理
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,lobalConfiguration globalConfiguration) throws IOException
    {
        InputStream inputStream = null;
        try
        {
            if(StringUtils.isEmpty(value)|| Objects.isNull(value))
            {
                return new WriteCellData("下载图片为空");
            }
            URL urlValue = new URL(value);
            // 开启连接
            URLConnection uc = urlValue.openConnection();
            // 获取响应状态
            int statusCode = ((HttpURLConnection)uc).getResponseCode();
            switch (statusCode) {
                case 200:
                    inputStream = urlValue.openStream();
                    break;
                default:
                    return new WriteCellData("无法加载图片");
            }
            byte[] bytes = IoUtils.toByteArray(inputStream);
            return new WriteCellData(bytes);
        } catch (ConnectException exception) {
            return new WriteCellData("无法加载图片");
        }catch (FileNotFoundException fileNotFoundException)
        {
            return new WriteCellData("无法加载图片");
        } finally {
            if (inputStream != null)
            {
                inputStream.close();
            }
        }
    }
    
}

运用示例:

@ExcelProperty(value = “XX图像”,index = 16,converter = MyStringImageConverter.class)
@ColumnWidth(50)
@ApiModelProperty(value = “图像URL地址”)
private String pictureUrl;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值