easyexcel 3.3.2 不再有表格image类型转为富文本类型

文章介绍了如何使用阿里巴巴Excel库中的MyStringImageConverter类,将图片转换为Rich_text_string类型,同时处理图片加载异常。
摘要由CSDN通过智能技术生成

CellDataTypeEnum.IMAGE  ---->  CellDataTypeEnum.RICH_TEXT_STRING

导出图片,导出二维码等工具类:

 

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.IoUtils;
import com.alibaba.excel.util.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;

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

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

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

    // 图片失效处理
    @Override
    public WriteCellData<Object> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                       GlobalConfiguration globalConfiguration)
        throws IOException
    {
        InputStream inputStream = null;
        try
        {
            if(StringUtils.isEmpty(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);
            WriteCellData<Object> cellData = new WriteCellData<>(bytes);
            cellData.setType(CellDataTypeEnum.NUMBER);
            return cellData;
        }
        catch (ConnectException exception)
        {
            return new WriteCellData<>("无法加载图片");
        }
        catch (FileNotFoundException fileNotFoundException)
        {
            return new WriteCellData<>("无法加载图片");
        }
        finally
        {
            if (inputStream != null)
            {
                inputStream.close();
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值