easyexcel 异常处理

x33g5p2x  于2021-12-28 转载在 其他  
字(5.3k)|赞(0)|评价(0)|浏览(1176)

easyexcel 异常处理

**********************

相关类与接口

AnalysisEventListener:异常在监听接口中处理

public abstract class AnalysisEventListener<T> implements ReadListener<T> {
    public AnalysisEventListener() {
    }

    public void invokeHead(Map<Integer, CellData> headMap, AnalysisContext context) {
        this.invokeHeadMap(ConverterUtils.convertToStringMap(headMap, context), context);
    }

    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }

    public void extra(CellExtra extra, AnalysisContext context) {
    }

    public void onException(Exception exception, AnalysisContext context) throws Exception {
        throw exception;
    }  //默认抛出异常

    public boolean hasNext(AnalysisContext context) {
        return true;
    }
}

ReadListener

public interface ReadListener<T> extends Listener {

    void invoke(T var1, AnalysisContext var2);
    void invokeHead(Map<Integer, CellData> var1, AnalysisContext var2);

    void doAfterAllAnalysed(AnalysisContext var1);

    void onException(Exception var1, AnalysisContext var2) throws Exception;  //异常处理

    void extra(CellExtra var1, AnalysisContext var2);
    boolean hasNext(AnalysisContext var1);
}

Listener

public interface Listener {
}

ExcelDataConvertException:数据转换异常错误

public class ExcelDataConvertException extends RuntimeException {

    private Integer rowIndex;
    private Integer columnIndex;

    private CellData cellData;
    private ExcelContentProperty excelContentProperty;

    public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData,
    public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData,

    public void setRowIndex(Integer rowIndex) {
    public void setColumnIndex(Integer columnIndex) {
    public void setCellData(CellData cellData) {
    public void setExcelContentProperty(ExcelContentProperty excelContentProperty) {

    public Integer getRowIndex() {
    public Integer getColumnIndex() {
    public CellData getCellData() {
    public ExcelContentProperty getExcelContentProperty() {

ExcelAnalysisStopException:onexception中抛出该异常后停止解析

public class ExcelAnalysisStopException extends ExcelAnalysisException {

    public ExcelAnalysisStopException() {}

    public ExcelAnalysisStopException(String message) {
        super(message);
    }

    public ExcelAnalysisStopException(String message, Throwable cause) {
        super(message, cause);
    }

    public ExcelAnalysisStopException(Throwable cause) {
        super(cause);
    }
}

**********************

示例


head 类

Order

@Data
public class Order {

    private Integer id;

    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    //@ExcelProperty(converter = CustomConverter.class)
    private LocalDateTime createTime;

    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    //@ExcelProperty(converter = CustomConverter.class)
    private LocalDateTime payTime;

    @NumberFormat("##.00")
    private Double totalFee;
}

监听器

CustomListener

public class CustomListener extends AnalysisEventListener<Order> {

    private final Logger logger= LoggerFactory.getLogger(CustomListener3.class.getName());

    @Override
    public void invoke(Order order, AnalysisContext context) {
        System.out.println(order);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("数据解析完成");
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        logger.info("解析出错:"+exception.getMessage());

        int row=0,column=0;
        if (exception instanceof ExcelDataConvertException){
            ExcelDataConvertException convertException=(ExcelDataConvertException) exception;

            row=convertException.getRowIndex();
            column=convertException.getColumnIndex();
            logger.error("解析出错:{}行 {}列",row,column);
        }
    }
}

测试类

Test

public class Test {

    private static final String file_path="e:"+ File.separator+"java"+File.separator+"easyexcel"+File.separator+"write_test6.xlsx";

    public static void main(String[] args){
        EasyExcel.read(file_path, Order.class,new CustomListener3()).sheet().doRead();
    }
}

**********************

使用测试

测试数据

              

控制台输出

10:32:44.226 [main] INFO com.example.demo.listener.CustomListener3 - 解析出错:Converter not found, convert STRING to java.time.LocalDateTime
10:32:44.226 [main] ERROR com.example.demo.listener.CustomListener3 - 解析出错:1行 1列
10:32:44.227 [main] INFO com.example.demo.listener.CustomListener3 - 解析出错:Converter not found, convert STRING to java.time.LocalDateTime
10:32:44.227 [main] ERROR com.example.demo.listener.CustomListener3 - 解析出错:2行 1列
数据解析完成

发生异常后,默认会继续解析后续数据

**********************

解析出现异常就停止

CustomListener

public class CustomListener3 extends AnalysisEventListener<Order> {

    private final Logger logger= LoggerFactory.getLogger(CustomListener3.class.getName());

    @Override
    public void invoke(Order order, AnalysisContext context) {
        System.out.println(order);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("数据解析完成");
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        logger.info("解析出错:"+exception.getMessage());

        int row=0,column=0;
        if (exception instanceof ExcelDataConvertException){
            ExcelDataConvertException convertException=(ExcelDataConvertException) exception;

            row=convertException.getRowIndex();
            column=convertException.getColumnIndex();
            logger.error("解析出错:{}行 {}列",row,column);
        }

        throw new ExcelAnalysisStopException("解析出错:"+row+"行 "+column+"列,停止运行");
    }
}

**********************

使用测试

控制台输出

10:35:50.577 [main] INFO com.example.demo.listener.CustomListener3 - 解析出错:Converter not found, convert STRING to java.time.LocalDateTime
10:35:50.577 [main] ERROR com.example.demo.listener.CustomListener3 - 解析出错:1行 1列
10:35:50.577 [main] DEBUG com.alibaba.excel.analysis.ExcelAnalyserImpl - Custom stop!

抛出ExcelAnalysisStopException异常后,立即停止,不会继续解析

相关文章