mirror of https://github.com/alibaba/easyexcel
Jiaju Zhuang
5 months ago
15 changed files with 284 additions and 119 deletions
@ -0,0 +1,30 @@
|
||||
package com.alibaba.excel.exception; |
||||
|
||||
/** |
||||
* Throw the exception when you need to stop |
||||
* This exception will only stop the parsing of the current sheet. If you want to stop the entire excel parsing, please |
||||
* use ExcelAnalysisStopException. |
||||
* |
||||
* The com.alibaba.excel.read.listener.ReadListener#doAfterAllAnalysed(com.alibaba.excel.context.AnalysisContext) method |
||||
* is called after the call is stopped. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
* @see ExcelAnalysisStopException |
||||
* @since 3.3.4 |
||||
*/ |
||||
public class ExcelAnalysisStopSheetException extends ExcelAnalysisException { |
||||
|
||||
public ExcelAnalysisStopSheetException() {} |
||||
|
||||
public ExcelAnalysisStopSheetException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public ExcelAnalysisStopSheetException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
public ExcelAnalysisStopSheetException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
@ -0,0 +1,52 @@
|
||||
package com.alibaba.easyexcel.test.core.exception; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.exception.ExcelAnalysisStopException; |
||||
import com.alibaba.excel.exception.ExcelAnalysisStopSheetException; |
||||
import com.alibaba.excel.util.ListUtils; |
||||
import com.alibaba.excel.util.MapUtils; |
||||
import com.alibaba.fastjson2.JSON; |
||||
|
||||
import lombok.Getter; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.collections4.CollectionUtils; |
||||
import org.apache.commons.collections4.SetUtils; |
||||
import org.assertj.core.internal.Maps; |
||||
import org.junit.jupiter.api.Assertions; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Getter |
||||
@Slf4j |
||||
public class ExcelAnalysisStopSheetExceptionDataListener extends AnalysisEventListener<ExceptionData> { |
||||
|
||||
private Map<Integer, List<String>> dataMap = MapUtils.newHashMap(); |
||||
|
||||
|
||||
@Override |
||||
public void invoke(ExceptionData data, AnalysisContext context) { |
||||
List<String> sheetDataList = dataMap.computeIfAbsent(context.readSheetHolder().getSheetNo(), |
||||
key -> ListUtils.newArrayList()); |
||||
sheetDataList.add(data.getName()); |
||||
if (sheetDataList.size() >= 5) { |
||||
throw new ExcelAnalysisStopSheetException(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
List<String> sheetDataList = dataMap.get(context.readSheetHolder().getSheetNo()); |
||||
Assertions.assertNotNull(sheetDataList); |
||||
Assertions.assertEquals(5, sheetDataList.size()); |
||||
} |
||||
} |
Loading…
Reference in new issue