Browse Source

Merge branch 'refs/heads/master' into developing

# Conflicts:
#	pom.xml
#	update.md
pull/3838/head
Jiaju Zhuang 5 months ago
parent
commit
d3ca6ca085
  1. 49
      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. 8
      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

49
README.md

@ -1,9 +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
@ -13,15 +7,18 @@ EasyExcel
[![License](http://img.shields.io/:license-apache-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.html)
[![](https://img.shields.io/badge/EasyExcel-%E6%9F%A5%E7%9C%8B%E8%B4%A1%E7%8C%AE%E6%8E%92%E8%A1%8C%E6%A6%9C-orange)](https://opensource.alibaba.com/contribution_leaderboard/details?projectValue=easyexcel)
# 新手必读
* 官方网站:[https://easyexcel.opensource.alibaba.com/](https://easyexcel.opensource.alibaba.com/)
* github地址:[https://github.com/alibaba/easyexcel](https://github.com/alibaba/easyexcel)
* gitee地址:[https://gitee.com/easyexcel/easyexcel](https://gitee.com/easyexcel/easyexcel)
# JAVA解析Excel工具
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模式,在上层做了模型转换的封装,让使用者更加简单方便
# 网站
* 官方网站:[https://easyexcel.opensource.alibaba.com/](https://easyexcel.opensource.alibaba.com/)
* github地址:[https://github.com/alibaba/easyexcel](https://github.com/alibaba/easyexcel)
* gitee地址:[https://gitee.com/easyexcel/easyexcel](https://gitee.com/easyexcel/easyexcel)
# 16M内存23秒读取75M(46W行25列)的Excel(3.2.1+版本)
@ -36,7 +33,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>
```
@ -64,11 +61,31 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
不管`github`、`gitee`都会定期有人回答您的问题,比较紧急可以在提完`issue`以后在钉钉群艾特群主并发送`issue`地址帮忙解决。
`QQ` 公司不让用,有时候也会去看,但是核心肯定还是在钉钉。
## 关注作者:程序员小獭
可以加群交流
![qrcode_for_gh_c43212c8d0ed_258](https://github.com/alibaba/easyexcel/assets/22975773/13ceff34-d547-421b-b9a9-04f388792099)
## 也可以加入钉钉&QQ群来解决问题
加入钉钉或QQ群,看完公告可以获得帮助 。
比较推荐钉钉群,`QQ` 公司不让用,当然QQ群也会有热心网友帮忙解决。
[QQ1群(已满): 662022184](https://jq.qq.com/?_wv=1027&k=1T21jJxh)
[QQ2群(已满): 1097936804](https://jq.qq.com/?_wv=1027&k=j5zEy6Xl)
[QQ3群(已满): 453928496](https://qm.qq.com/cgi-bin/qm/qr?k=e2ULsA5A0GldhV2CXJ8sIbAyu9I6qqs7&jump_from=webapi)
[QQ4群(已满): 496594404](https://qm.qq.com/cgi-bin/qm/qr?k=e_aVG1Q7gi0PJUBkbrUGAgbeO3kUEInK&jump_from=webapi)
[QQ5群(已满): 451925680](https://jq.qq.com/?_wv=1027&k=6VHhvxyf)
[QQ6群(已满): 784741035](https://jq.qq.com/?_wv=1027&k=BbLBIo9P)
[QQ7群(已满): 667889383](http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=XdTLw3Z3pr63VT0IkyoY-2t25TG7WxbG&authKey=gQKvTXipsjfUO1aNfL9zdHTfOmkqC6E%2BQ2zDg2jym8h3qXuQ7RtkpeAHeg9I4UhL&noverify=0&group_code=667889383)
[QQ8群: 113968681](https://qm.qq.com/q/qwfl5RRBAG)
[钉钉1群(已满): 21960511](https://qr.dingtalk.com/action/joingroup?code=v1,k1,cchz6k12ci9B08NNqhNRFGXocNVHrZtW0kaOtTKg/Rk=&_dt_no_comment=1&origin=11)
[钉钉2群(已满): 32796397](https://qr.dingtalk.com/action/joingroup?code=v1,k1,jyU9GtEuNU5S0QTyklqYcYJ8qDZtUuTPMM7uPZTS8Hs=&_dt_no_comment=1&origin=11)
[钉钉3群(已满): 33797247](https://qr.dingtalk.com/action/joingroup?code=v1,k1,3UGlEScTGQaHpW2cIRo+gkxJ9EVZ5fz26M6nW3uFP30=&_dt_no_comment=1&origin=11)
[钉钉4群(已满): 33491624](https://qr.dingtalk.com/action/joingroup?code=v1,k1,V14Pb65Too70rQkEaJ9ohb6lZBZbtp6jIL/q9EWh9vA=&_dt_no_comment=1&origin=11)
[钉钉5群(已满): 32134498](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingb9fa1325d9dccc3ecac589edd02f1650&5233a=71a83&cbdbhh=qwertyuiop)
[钉钉6群(已满): 34707941](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingcf68008a1d443ac012d5427bdb061b7a&6ae36c3d-0c80-4=22398493-6c2a-4&cbdbhh=qwertyuiop)
[钉钉7群(已满): 35235427](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=ding532b9018c06c7fc8660273c4b78e6440&167fb=ed003&cbdbhh=qwertyuiop)
[钉钉8群(已满): 44752220](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dingea96808beee421693fd4ba7542d6e5da&0380092a-fa46=a6a40905-7951&cbdbhh=qwertyuiop)
[钉钉9群(已满): 11045002277](https://h5.dingtalk.com/circle/healthCheckin.html?dtaction=os&corpId=dinge338d2215891c0459c13cd6b2cb108a6&6972d=b92f9&cbdbhh=qwertyuiop)
[钉钉10群(已满): 27360019755](https://qr.dingtalk.com/action/joingroup?code=v1,k1,v25LHn2liWmrWUKlkhIzOTcK7s7onp/sZP8mO5oIYSs=&_dt_no_comment=1&origin=11)
[钉钉11群(已满):24330026964](https://qr.dingtalk.com/action/joingroup?code=v1,k1,63PjvTncu81oQ3X6XmGEJqnwQHCQxi/jaVlbUStq79o=&_dt_no_comment=1&origin=11)
[钉钉12群(已满):27210038956](https://qr.dingtalk.com/action/joingroup?code=v1,k1,3mKi7VTGlYO+IsDX5n7sYYm2Qrlm220kMBPsJFzKRis=&_dt_no_comment=1&origin=11)
[钉钉13群:83695000992](https://qr.dingtalk.com/action/joingroup?code=v1,k1,2JFUbWfxD1fGiq7LRW+mYjcK7s7onp/s1ZqOvfzkFGE=&_dt_no_comment=1&origin=11)
# 维护者

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

8
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;
@ -254,10 +256,16 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
for (ReadSheet readSheet : sheetList) {
readSheet = SheetUtils.match(readSheet, xlsxReadContext);
if (readSheet != null) {
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