Browse Source

Merge branch 'refs/heads/master' into image

# Conflicts:
#	easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
#	pom.xml
#	update.md
image
Jiaju Zhuang 8 months ago
parent
commit
5d31cb6041
  1. 21
      README.md
  2. 8
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java
  3. 29
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  4. 1
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
  5. 13
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java
  6. 12
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  7. 3
      easyexcel-core/src/main/java/com/alibaba/excel/exception/ExcelAnalysisStopException.java
  8. 30
      easyexcel-core/src/main/java/com/alibaba/excel/exception/ExcelAnalysisStopSheetException.java
  9. 5
      easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java
  10. 34
      easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadSheetHolder.java
  11. 70
      easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadWorkbookHolder.java
  12. 11
      easyexcel-core/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java
  13. 52
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExcelAnalysisStopSheetExceptionDataListener.java
  14. 60
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java
  15. 8
      update.md

21
README.md

@ -1,11 +1,3 @@
# 🔥作者最新开源项目[Chat2DB](https://github.com/chat2db/Chat2DB)
- GitHub地址 [https://github.com/chat2db/Chat2DB](https://github.com/chat2db/Chat2DB)
- 官 网 地 址 [https://chat2db.ai](https://chat2db.ai)
<a href= "https://github.com/chat2db/Chat2DB" ><img width="1720" alt="crete datasource" src="https://github.com/chat2db/Chat2DB/assets/22975773/16050747-0f6c-4e98-ba91-323033584eec"></a>
EasyExcel
======================
[![Build Status](https://github.com/alibaba/easyexcel/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/alibaba/easyexcel/actions/workflows/ci.yml?query=branch%3Amaster)
@ -18,6 +10,17 @@ EasyExcel
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。
easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便
# 推荐 Chat2DB
AI 驱动的数据库管理、数据分析工具,支持Mysql、pg、oracle、sqlserver、redis等10多种数据库
* Github 地址: [https://github.com/chat2db/Chat2DB](https://github.com/chat2db/Chat2DB)
* 官网:[https://chat2db-ai.com](https://chat2db-ai.com)
<p align="center">
<a href="https://chat2db.ai/" target="_blank">
<img src="https://chat2db-cdn.oss-us-west-1.aliyuncs.com/website/img/cover.png" alt="Chat2DB" />
</a>
</p>
# 网站
* 官方网站:[https://easyexcel.opensource.alibaba.com/](https://easyexcel.opensource.alibaba.com/)
* github地址:[https://github.com/alibaba/easyexcel](https://github.com/alibaba/easyexcel)
@ -36,7 +39,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.2</version>
<version>3.3.4</version>
</dependency>
```

8
easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java

@ -16,6 +16,8 @@ import com.alibaba.excel.enums.ByteOrderMarkEnum;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.exception.ExcelAnalysisStopSheetException;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.metadata.ReadSheet;
@ -69,6 +71,7 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor {
if (readSheet == null) {
continue;
}
try {
csvReadContext.currentSheet(readSheet);
int rowIndex = 0;
@ -76,6 +79,11 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor {
for (CSVRecord record : csvParser) {
dealRecord(record, rowIndex++);
}
} catch (ExcelAnalysisStopSheetException e) {
if (log.isDebugEnabled()) {
log.debug("Custom stop!", e);
}
}
// The last sheet is read
csvReadContext.analysisEventProcessor().endSheet(csvReadContext);

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

@ -5,12 +5,14 @@ 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;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
@ -56,23 +58,26 @@ 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;
/**
* /** * A text extractor for Excel files. *
* A text extractor for Excel files.
* <p>
* * Returns the textual content of the file, suitable for * indexing by something like Lucene, but not really *
* intended for display to the user. *
* Returns the textual content of the file, suitable for indexing by something like Lucene, but not really intended for
* display to the user.
* </p>
* *
*
* <p>
* * To turn an excel file into a CSV or similar, then see * the XLS2CSVmra example *
* 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>
*
*
* @author jipengfei
* @see <a href="http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/hssf/eventusermodel/examples/XLS2CSVmra.java">XLS2CSVmra</a>
*/
@Slf4j
public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class);
@ -137,6 +142,9 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
} catch (IOException e) {
throw new ExcelAnalysisException(e);
}
// There are some special xls that do not have the terminator "[EOF]", so an additional
xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext);
}
@Override
@ -154,7 +162,16 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
if (!handler.support(xlsReadContext, record)) {
return;
}
try {
handler.processRecord(xlsReadContext, record);
} catch (ExcelAnalysisStopSheetException e) {
if (log.isDebugEnabled()) {
log.debug("Custom stop!", e);
}
xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.TRUE);
xlsReadContext.xlsReadWorkbookHolder().setCurrentSheetStopped(Boolean.TRUE);
}
}
}

1
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java

@ -49,6 +49,7 @@ public class BofRecordHandler extends AbstractXlsRecordHandler {
} else {
xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.TRUE);
}
xlsReadContext.xlsReadWorkbookHolder().setCurrentSheetStopped(Boolean.FALSE);
// Go read the next one
xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1);
}

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

@ -10,19 +10,30 @@ 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) {
if (xlsReadContext.readSheetHolder() == null) {
return;
}
//Represents the current sheet does not need to be read or the user manually stopped reading the sheet.
if (BooleanUtils.isTrue(xlsReadContext.xlsReadWorkbookHolder().getIgnoreRecord())) {
// When the user manually stops reading the sheet, the method to end the sheet needs to be called.
if (BooleanUtils.isTrue(xlsReadContext.xlsReadWorkbookHolder().getCurrentSheetStopped())) {
xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext);
}
return;
}
// Sometimes tables lack the end record of the last column
if (!xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) {
XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();

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

@ -23,6 +23,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;
@ -279,6 +281,16 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
readComments(readSheet);
// Read image
readImages(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);
}
}

5
easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java

@ -67,6 +67,11 @@ public class ReadSheetHolder extends AbstractReadHolder {
*/
private Integer maxNotEmptyDataHeadSize;
/**
* Reading this sheet has ended.
*/
private Boolean ended;
public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) {
super(readSheet, readWorkbookHolder);
this.readSheet = readSheet;

34
easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadSheetHolder.java

@ -8,11 +8,20 @@ import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* sheet holder
*
* @author Jiaju Zhuang
*/
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
public class XlsReadSheetHolder extends ReadSheetHolder {
/**
* Row type.Temporary storage, last set in <code>ReadRowHolder</code>.
@ -32,29 +41,4 @@ public class XlsReadSheetHolder extends ReadSheetHolder {
tempRowType = RowTypeEnum.EMPTY;
objectCacheMap = new HashMap<Integer, String>(16);
}
public RowTypeEnum getTempRowType() {
return tempRowType;
}
public void setTempRowType(RowTypeEnum tempRowType) {
this.tempRowType = tempRowType;
}
public Integer getTempObjectIndex() {
return tempObjectIndex;
}
public void setTempObjectIndex(Integer tempObjectIndex) {
this.tempObjectIndex = tempObjectIndex;
}
public Map<Integer, String> getObjectCacheMap() {
return objectCacheMap;
}
public void setObjectCacheMap(Map<Integer, String> objectCacheMap) {
this.objectCacheMap = objectCacheMap;
}
}

70
easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadWorkbookHolder.java

@ -3,6 +3,10 @@ package com.alibaba.excel.read.metadata.holder.xls;
import java.util.ArrayList;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -17,6 +21,10 @@ import com.alibaba.excel.support.ExcelTypeEnum;
*
* @author Jiaju Zhuang
*/
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
public class XlsReadWorkbookHolder extends ReadWorkbookHolder {
/**
* File System
@ -47,6 +55,11 @@ public class XlsReadWorkbookHolder extends ReadWorkbookHolder {
*/
private Boolean ignoreRecord;
/**
* Has the current sheet already stopped
*/
private Boolean currentSheetStopped;
public XlsReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook);
this.boundSheetRecordList = new ArrayList<BoundSheetRecord>();
@ -56,61 +69,6 @@ public class XlsReadWorkbookHolder extends ReadWorkbookHolder {
getGlobalConfiguration().setUse1904windowing(Boolean.FALSE);
}
ignoreRecord = Boolean.FALSE;
}
public POIFSFileSystem getPoifsFileSystem() {
return poifsFileSystem;
}
public void setPoifsFileSystem(POIFSFileSystem poifsFileSystem) {
this.poifsFileSystem = poifsFileSystem;
}
public FormatTrackingHSSFListener getFormatTrackingHSSFListener() {
return formatTrackingHSSFListener;
}
public void setFormatTrackingHSSFListener(FormatTrackingHSSFListener formatTrackingHSSFListener) {
this.formatTrackingHSSFListener = formatTrackingHSSFListener;
}
public HSSFWorkbook getHssfWorkbook() {
return hssfWorkbook;
}
public void setHssfWorkbook(HSSFWorkbook hssfWorkbook) {
this.hssfWorkbook = hssfWorkbook;
}
public List<BoundSheetRecord> getBoundSheetRecordList() {
return boundSheetRecordList;
}
public void setBoundSheetRecordList(List<BoundSheetRecord> boundSheetRecordList) {
this.boundSheetRecordList = boundSheetRecordList;
}
public Boolean getNeedReadSheet() {
return needReadSheet;
}
public void setNeedReadSheet(Boolean needReadSheet) {
this.needReadSheet = needReadSheet;
}
public Integer getReadSheetIndex() {
return readSheetIndex;
}
public void setReadSheetIndex(Integer readSheetIndex) {
this.readSheetIndex = readSheetIndex;
}
public Boolean getIgnoreRecord() {
return ignoreRecord;
}
public void setIgnoreRecord(Boolean ignoreRecord) {
this.ignoreRecord = ignoreRecord;
currentSheetStopped = Boolean.TRUE;
}
}

11
easyexcel-core/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java

@ -3,8 +3,6 @@ package com.alibaba.excel.read.processor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
@ -16,11 +14,12 @@ import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.ConverterUtils;
import com.alibaba.excel.util.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -53,6 +52,12 @@ public class DefaultAnalysisEventProcessor implements AnalysisEventProcessor {
@Override
public void endSheet(AnalysisContext analysisContext) {
ReadSheetHolder readSheetHolder = analysisContext.readSheetHolder();
if (BooleanUtils.isTrue(readSheetHolder.getEnded())) {
return;
}
readSheetHolder.setEnded(Boolean.TRUE);
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
readListener.doAfterAllAnalysed(analysisContext);
}

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

8
update.md

@ -5,6 +5,14 @@
* `slf4j-api`由`1.7.32`升级到`1.7.36`
* `ehcache`由`3.9.9`升级到`3.10.8`
# 3.3.4
* 支持停止单个`sheet`以后继续读取其他`sheet`,使用`ExcelAnalysisStopSheetException`
# 3.3.3
* 兼容某些特殊的xls: 缺少每个sheet的终止符
# 3.3.2
* 修复`includeColumnIndexes`和`includeColumnFieldNames`在sheet后面失效的问题

Loading…
Cancel
Save