Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix20240311 #3720

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
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;
Expand Down Expand Up @@ -69,12 +71,18 @@ public void execute() {
if (readSheet == null) {
continue;
}
csvReadContext.currentSheet(readSheet);

int rowIndex = 0;

for (CSVRecord record : csvParser) {
dealRecord(record, rowIndex++);
try {
csvReadContext.currentSheet(readSheet);

int rowIndex = 0;

for (CSVRecord record : csvParser) {
dealRecord(record, rowIndex++);
}
} catch (ExcelAnalysisStopSheetException e) {
if (log.isDebugEnabled()) {
log.debug("Custom stop!", e);
}
}

// The last sheet is read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -57,6 +58,7 @@
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;

Expand All @@ -70,10 +72,13 @@
* <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);
Expand Down Expand Up @@ -158,7 +163,16 @@ public void processRecord(Record record) {
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);
xlsReadContext.xlsReadWorkbookHolder().setCurrentSheetStopped(Boolean.TRUE);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void processRecord(XlsReadContext xlsReadContext, Record record) {
} else {
xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.TRUE);
}
xlsReadContext.xlsReadWorkbookHolder().setCurrentSheetStopped(Boolean.FALSE);
// Go read the next one
xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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;
Expand Down Expand Up @@ -256,10 +258,16 @@ public void execute() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

/**
* 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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
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;
Expand All @@ -17,6 +21,10 @@
*
* @author Jiaju Zhuang
*/
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
public class XlsReadWorkbookHolder extends ReadWorkbookHolder {
/**
* File System
Expand Down Expand Up @@ -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>();
Expand All @@ -56,61 +69,6 @@ public XlsReadWorkbookHolder(ReadWorkbook readWorkbook) {
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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Loading