使用EasyExcel导出图片及异常处理

摘要:
1.使用字符串类型导出来定义自己的转换器。不要使用默认的StringImageConverter。如果图片路径为空或图片路径错误,则相应的内容importjava.io。返回FileNotFoundException;importjava.io。IOException;importjava.io。输入流;importjava.net.Connect异常

1、使用String类型导出   定义自己的Converter,不使用默认的StringImageConverter

如果图片路径为空或者图片路径是错误的,返回相应的内容

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 com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.IoUtils;
import com.njyjz.common.util.Validater;

public class MyStringImageConverter implements Converter<String>
{
    @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
    {
        InputStream inputStream = null;
        try
        {
            if(Validater.isEmptyString(value))
            {
                return new CellData("图片路径为空");
            }
            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 CellData("无法加载图片");
            }
            byte[] bytes = IoUtils.toByteArray(inputStream);
            return new CellData(bytes);
        }
        catch (ConnectException exception)
        {
            return new CellData("无法加载图片");
        }
        catch (FileNotFoundException fileNotFoundException)
        {
            return new CellData("无法加载图片");
        }
        finally
        {
            if (inputStream != null)
            {
                inputStream.close();
            }
        }
    }
    
}

2、更改图片字段注解

@ExcelProperty(value = "扫描图像", index = 0, converter = MyStringImageConverter.class)
private String fileUrl;

3、导出样例

使用EasyExcel导出图片及异常处理第1张

 使用EasyExcel导出图片及异常处理第2张

免责声明:文章转载自《使用EasyExcel导出图片及异常处理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[C#]asp.net生成高清晰缩略图vue中使用v-chart改变柱状图颜色以及X轴Y轴的文字颜色和大小以及标题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

yaf学习

<?php 安装 phpize ./configure --with-php-config=/usr/local/php/bin/php-config 路由类 final Yaf_Router { protected array _routes ; protected string _current_rout...

MD5Helper辅助类

DES加密和解密 public classMD5Helper { ///DES加密 ///sKey public string MD5Encrypt(string pToEncrypt, stringsKey) { DESCryp...

Swift-可选值(Optional)讲解

前提:Swift中有规定:对象中的任何属性在创建时,都必须要有明确的初始化值 1.定义可选类型 方式一:常规方式(不常用) var name : Optional<String> = nil 方式二:语法糖(常用) var name:String? = nil Optional理解:   Optional也是Objective-C没有的数据类型...

angular写的一个导航栏

ts import { Component } from '@angular/core'; // 定义一个interface interface Menu{ title:string,link:string,id:string } @Component({ selector: 'app-root', templateUrl: './app.co...

VC++ 复制整个文件夹

CopyFile只能复制一个文件,要想复制整个文件夹的所有文件,需要遍历文件夹里面的文件。 MyFile.h #pragma once #include <string> #include <Windows.h> #include <iostream> #include <stdio.h> #include...

操作百度API

1 string json = ""; 2 try 3 { //虽然两者都是异步请求事件,但是WebClient是基于事件的异步,而HttpWebRequst是基于代理的异步编程 4 WebClient client = ne...