Browse Source

* 支持停止单个`sheet`以后继续读取其他`sheet`,使用`ExcelAnalysisStopSheetException`

pull/3720/head
是仪 2 months ago
parent
commit
4cd56ad278
  1. 2
      README.md
  2. 17
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  3. 6
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java
  4. 16
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  5. 3
      easyexcel-core/src/main/java/com/alibaba/excel/exception/ExcelAnalysisStopException.java
  6. 30
      easyexcel-core/src/main/java/com/alibaba/excel/exception/ExcelAnalysisStopSheetException.java
  7. 52
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExcelAnalysisStopSheetExceptionDataListener.java
  8. 60
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java
  9. 2
      pom.xml
  10. 4
      update.md

2
README.md

@ -28,7 +28,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.3</version>
<version>3.3.4</version>
</dependency>
```

17
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java

@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
@ -57,6 +58,7 @@ import com.alibaba.excel.analysis.v03.handlers.TextObjectRecordHandler;
import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.exception.ExcelAnalysisStopSheetException;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
@ -70,10 +72,13 @@ import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
* <p>
* * To turn an excel file into a CSV or similar, then see * the XLS2CSVmra example *
* </p>
* * * @see <a href= "http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java">XLS2CSVmra</a>
* * * @see <a href=
* "http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra
* .java">XLS2CSVmra</a>
*
* @author jipengfei
*/
@Slf4j
public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class);
@ -158,7 +163,15 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
if (!handler.support(xlsReadContext, record)) {
return;
}
handler.processRecord(xlsReadContext, record);
try {
handler.processRecord(xlsReadContext, record);
} catch (ExcelAnalysisStopSheetException e) {
if (log.isDebugEnabled()) {
log.debug("Custom stop!", e);
}
xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.TRUE);
}
}
}

6
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java

@ -10,13 +10,14 @@ import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.util.BooleanUtils;
/**
* Record handler
*
* @author Dan Zheng
*/
public class EofRecordHandler extends AbstractXlsRecordHandler implements IgnorableXlsRecordHandler {
public class EofRecordHandler extends AbstractXlsRecordHandler {
@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
@ -24,7 +25,8 @@ public class EofRecordHandler extends AbstractXlsRecordHandler implements Ignora
return;
}
// Sometimes tables lack the end record of the last column
if (!xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) {
if (BooleanUtils.isNotTrue(xlsReadContext.xlsReadWorkbookHolder().getIgnoreRecord())
&& !xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) {
XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
// Forge a termination data
xlsReadContext.readRowHolder(new ReadRowHolder(xlsReadContext.xlsReadSheetHolder().getRowIndex() + 1,

16
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -22,6 +22,8 @@ import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.context.xlsx.XlsxReadContext;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.exception.ExcelAnalysisStopSheetException;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
@ -256,10 +258,16 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
for (ReadSheet readSheet : sheetList) {
readSheet = SheetUtils.match(readSheet, xlsxReadContext);
if (readSheet != null) {
xlsxReadContext.currentSheet(readSheet);
parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext));
// Read comments
readComments(readSheet);
try {
xlsxReadContext.currentSheet(readSheet);
parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext));
// Read comments
readComments(readSheet);
} catch (ExcelAnalysisStopSheetException e) {
if (log.isDebugEnabled()) {
log.debug("Custom stop!", e);
}
}
// The last sheet is read
xlsxReadContext.analysisEventProcessor().endSheet(xlsxReadContext);
}

3
easyexcel-core/src/main/java/com/alibaba/excel/exception/ExcelAnalysisStopException.java

@ -2,8 +2,11 @@ package com.alibaba.excel.exception;
/**
* Throw the exception when you need to stop
* This exception will stop the entire excel parsing. If you only want to stop the parsing of a certain sheet, please
* use ExcelAnalysisStopSheetException.
*
* @author Jiaju Zhuang
* @see ExcelAnalysisStopException
*/
public class ExcelAnalysisStopException extends ExcelAnalysisException {

30
easyexcel-core/src/main/java/com/alibaba/excel/exception/ExcelAnalysisStopSheetException.java

@ -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);
}
}

52
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExcelAnalysisStopSheetExceptionDataListener.java

@ -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());
}
}

60
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java

@ -5,10 +5,15 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
@ -23,15 +28,20 @@ public class ExceptionDataTest {
private static File file07;
private static File file03;
private static File fileCsv;
private static File fileExcelAnalysisStopSheetException07;
private static File fileExcelAnalysisStopSheetException03;
private static File fileExcelAnalysisStopSheetExceptionCsv;
private static File fileException07;
private static File fileException03;
private static File fileCsv;
@BeforeAll
public static void init() {
file07 = TestFileUtil.createNewFile("exception.xlsx");
file03 = TestFileUtil.createNewFile("exception.xls");
fileCsv = TestFileUtil.createNewFile("exception.csv");
fileExcelAnalysisStopSheetException07 = TestFileUtil.createNewFile("excelAnalysisStopSheetException.xlsx");
fileExcelAnalysisStopSheetException03 = TestFileUtil.createNewFile("excelAnalysisStopSheetException.xls");
fileException07 = TestFileUtil.createNewFile("exceptionThrow.xlsx");
fileException03 = TestFileUtil.createNewFile("exceptionThrow.xls");
}
@ -61,6 +71,44 @@ public class ExceptionDataTest {
readAndWriteException(fileException03);
}
@Test
public void t21ReadAndWrite07() throws Exception {
readAndWriteExcelAnalysisStopSheetException(fileExcelAnalysisStopSheetException07);
}
@Test
public void t22ReadAndWrite03() throws Exception {
readAndWriteExcelAnalysisStopSheetException(fileExcelAnalysisStopSheetException03);
}
private void readAndWriteExcelAnalysisStopSheetException(File file) throws Exception {
try (ExcelWriter excelWriter = EasyExcel.write(file, ExceptionData.class).build()) {
for (int i = 0; i < 5; i++) {
String sheetName = "sheet" + i;
WriteSheet writeSheet = EasyExcel.writerSheet(i, sheetName).build();
List<ExceptionData> data = data(sheetName);
excelWriter.write(data, writeSheet);
}
}
ExcelAnalysisStopSheetExceptionDataListener excelAnalysisStopSheetExceptionDataListener
= new ExcelAnalysisStopSheetExceptionDataListener();
EasyExcel.read(file, ExceptionData.class, excelAnalysisStopSheetExceptionDataListener).doReadAll();
Map<Integer, List<String>> dataMap = excelAnalysisStopSheetExceptionDataListener.getDataMap();
Assertions.assertEquals(5, dataMap.size());
for (int i = 0; i < 5; i++) {
List<String> sheetDataList = dataMap.get(i);
Assertions.assertNotNull(sheetDataList);
Assertions.assertEquals(5, sheetDataList.size());
String sheetName = "sheet" + i;
for (String sheetData : sheetDataList) {
Assertions.assertTrue(sheetData.startsWith(sheetName));
}
}
}
private void readAndWriteException(File file) throws Exception {
EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data());
ArithmeticException exception = Assertions.assertThrows(ArithmeticException.class, () -> EasyExcel.read(
@ -83,4 +131,14 @@ public class ExceptionDataTest {
}
return list;
}
private List<ExceptionData> data(String prefix) {
List<ExceptionData> list = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
ExceptionData simpleData = new ExceptionData();
simpleData.setName(prefix + "-姓名" + i);
list.add(simpleData);
}
return list;
}
}

2
pom.xml

@ -20,7 +20,7 @@
<properties>
<revision>3.3.3</revision>
<revision>3.3.4</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version>
<gpg.skip>true</gpg.skip>

4
update.md

@ -1,3 +1,7 @@
# 3.3.4
* 支持停止单个`sheet`以后继续读取其他`sheet`,使用`ExcelAnalysisStopSheetException`
# 3.3.3
* 兼容某些特殊的xls: 缺少每个sheet的终止符

Loading…
Cancel
Save