Search in sources :

Example 6 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project easyexcel by alibaba.

the class ExcelReaderSheetBuilder method doReadSync.

/**
 * Synchronous reads return results
 *
 * @return
 */
public <T> List<T> doReadSync() {
    if (excelReader == null) {
        throw new ExcelAnalysisException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
    }
    SyncReadListener syncReadListener = new SyncReadListener();
    registerReadListener(syncReadListener);
    excelReader.read(build());
    excelReader.finish();
    return (List<T>) syncReadListener.getList();
}
Also used : SyncReadListener(com.alibaba.excel.event.SyncReadListener) List(java.util.List) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 7 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project RuoYi-Vue-Plus by JavaLionLi.

the class DefaultExcelListener method onException.

/**
 * 处理异常
 *
 * @param exception ExcelDataConvertException
 * @param context   Excel 上下文
 */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    String errMsg = null;
    if (exception instanceof ExcelDataConvertException) {
        // 如果是某一个单元格的转换异常 能获取到具体行号
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        Integer rowIndex = excelDataConvertException.getRowIndex();
        Integer columnIndex = excelDataConvertException.getColumnIndex();
        errMsg = StrUtil.format("第{}行-第{}列-表头{}: 解析异常<br/>", rowIndex + 1, columnIndex + 1, headMap.get(columnIndex));
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    if (exception instanceof ConstraintViolationException) {
        ConstraintViolationException constraintViolationException = (ConstraintViolationException) exception;
        Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
        String constraintViolationsMsg = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(", "));
        errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg);
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    excelResult.getErrorList().add(errMsg);
    throw new ExcelAnalysisException(errMsg);
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ConstraintViolationException(javax.validation.ConstraintViolationException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 8 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project RuoYi-Flowable-Plus by KonBAI-Q.

the class DefaultExcelListener method onException.

/**
 * 处理异常
 *
 * @param exception ExcelDataConvertException
 * @param context   Excel 上下文
 */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    String errMsg = null;
    if (exception instanceof ExcelDataConvertException) {
        // 如果是某一个单元格的转换异常 能获取到具体行号
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        Integer rowIndex = excelDataConvertException.getRowIndex();
        Integer columnIndex = excelDataConvertException.getColumnIndex();
        errMsg = StrUtil.format("第{}行-第{}列-表头{}: 解析异常<br/>", rowIndex + 1, columnIndex + 1, headMap.get(columnIndex));
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    if (exception instanceof ConstraintViolationException) {
        ConstraintViolationException constraintViolationException = (ConstraintViolationException) exception;
        Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
        String constraintViolationsMsg = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(", "));
        errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg);
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    excelResult.getErrorList().add(errMsg);
    throw new ExcelAnalysisException(errMsg);
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ConstraintViolationException(javax.validation.ConstraintViolationException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 9 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project RuoYi-Cloud-Plus by JavaLionLi.

the class DefaultExcelListener method onException.

/**
 * 处理异常
 *
 * @param exception ExcelDataConvertException
 * @param context   Excel 上下文
 */
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
    String errMsg = null;
    if (exception instanceof ExcelDataConvertException) {
        // 如果是某一个单元格的转换异常 能获取到具体行号
        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
        Integer rowIndex = excelDataConvertException.getRowIndex();
        Integer columnIndex = excelDataConvertException.getColumnIndex();
        errMsg = StrUtil.format("第{}行-第{}列-表头{}: 解析异常<br/>", rowIndex + 1, columnIndex + 1, headMap.get(columnIndex));
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    if (exception instanceof ConstraintViolationException) {
        ConstraintViolationException constraintViolationException = (ConstraintViolationException) exception;
        Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
        String constraintViolationsMsg = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(", "));
        errMsg = StrUtil.format("第{}行数据校验异常: {}", context.readRowHolder().getRowIndex() + 1, constraintViolationsMsg);
        if (log.isDebugEnabled()) {
            log.error(errMsg);
        }
    }
    excelResult.getErrorList().add(errMsg);
    throw new ExcelAnalysisException(errMsg);
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ExcelDataConvertException(com.alibaba.excel.exception.ExcelDataConvertException) ConstraintViolationException(javax.validation.ConstraintViolationException) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 10 with ExcelAnalysisException

use of com.alibaba.excel.exception.ExcelAnalysisException in project easyexcel by alibaba.

the class ExcelAnalyserImpl method finish.

@Override
public void finish() {
    if (finished) {
        return;
    }
    finished = true;
    if (analysisContext == null || analysisContext.readWorkbookHolder() == null) {
        return;
    }
    ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
    Throwable throwable = null;
    try {
        if (readWorkbookHolder.getReadCache() != null) {
            readWorkbookHolder.getReadCache().destroy();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if ((readWorkbookHolder instanceof XlsxReadWorkbookHolder) && ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage() != null) {
            ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage().revert();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if ((readWorkbookHolder instanceof XlsReadWorkbookHolder) && ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem() != null) {
            ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    // https://github.com/alibaba/easyexcel/issues/2309
    try {
        if ((readWorkbookHolder instanceof CsvReadWorkbookHolder) && ((CsvReadWorkbookHolder) readWorkbookHolder).getCsvParser() != null) {
            ((CsvReadWorkbookHolder) readWorkbookHolder).getCsvParser().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if (analysisContext.readWorkbookHolder().getAutoCloseStream() && readWorkbookHolder.getInputStream() != null) {
            readWorkbookHolder.getInputStream().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if (readWorkbookHolder.getTempFile() != null) {
            FileUtils.delete(readWorkbookHolder.getTempFile());
        }
    } catch (Throwable t) {
        throwable = t;
    }
    clearEncrypt03();
    removeThreadLocalCache();
    if (throwable != null) {
        throw new ExcelAnalysisException("Can not close IO.", throwable);
    }
}
Also used : CsvReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder) ReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder) XlsxReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder) CsvReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder) XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) XlsxReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Aggregations

ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)12 IOException (java.io.IOException)5 ExcelDataConvertException (com.alibaba.excel.exception.ExcelDataConvertException)3 ConstraintViolation (javax.validation.ConstraintViolation)3 ConstraintViolationException (javax.validation.ConstraintViolationException)3 XlsReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder)2 EventWorkbookBuilder (org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder)2 FormatTrackingHSSFListener (org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener)2 HSSFEventFactory (org.apache.poi.hssf.eventusermodel.HSSFEventFactory)2 HSSFRequest (org.apache.poi.hssf.eventusermodel.HSSFRequest)2 MissingRecordAwareHSSFListener (org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener)2 SyncReadListener (com.alibaba.excel.event.SyncReadListener)1 ExcelAnalysisStopException (com.alibaba.excel.exception.ExcelAnalysisStopException)1 ExcelCommonException (com.alibaba.excel.exception.ExcelCommonException)1 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)1 ReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder)1 CsvReadSheetHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadSheetHolder)1 CsvReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder)1 XlsReadSheetHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder)1 XlsxReadSheetHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder)1