Browse Source

新增支持接收批注

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
2c58b5e5fc
  1. 2
      .travis.yml
  2. 1
      README.md
  3. 8
      pom.xml
  4. 37
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  5. 6
      src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java
  6. 4
      src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java
  7. 64
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  8. 5
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java
  9. 19
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
  10. 6
      src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java
  11. 12
      src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java
  12. 6
      src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java
  13. 5
      src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java
  14. 5
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java
  15. 13
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
  16. 5
      src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java
  17. 5
      src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java
  18. 39
      src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java
  19. 2
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  20. 16
      src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java
  21. 9
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  22. 11
      src/main/java/com/alibaba/excel/context/XlsReadContext.java
  23. 13
      src/main/java/com/alibaba/excel/enums/ExtraReadEnum.java
  24. 5
      src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java
  25. 18
      src/main/java/com/alibaba/excel/metadata/CellExtra.java
  26. 16
      src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java
  27. 32
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java
  28. 24
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java
  29. 26
      src/test/java/com/alibaba/easyexcel/test/demo/read/DemoCellCommentsListener.java
  30. 34
      src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java
  31. 2
      update.md

2
.travis.yml

@ -6,5 +6,5 @@ cache:
before_install:
- chmod +x mvnw
install:
- ./mvnw install -Dgpg.skip -B -V -Dmaven.test.skip=true
- ./mvnw install -B -V -Dmaven.test.skip=true
- ./mvnw javadoc:javadoc

1
README.md

@ -19,6 +19,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
* [快速使用](https://alibaba-easyexcel.github.io/)
* [关于软件](/abouteasyexcel.md)
* [更新记事](/update.md)
* [贡献代码](https://alibaba-easyexcel.github.io/support/contribute.html)
## 维护者
玉霄、庄家钜、怀宇

8
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.4</version>
<version>2.2.0-beta1</version>
<packaging>jar</packaging>
<name>easyexcel</name>
@ -15,9 +15,10 @@
<properties>
<!--<gpg.skip>true</gpg.skip>-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.6</jdk.version>
<gpg.skip>true</gpg.skip>
<maven.javadoc.skip>true</maven.javadoc.skip>
</properties>
<scm>
@ -159,6 +160,8 @@
</rulesets>
<excludes>
<exclude>com/alibaba/excel/event/AnalysisEventListener.java</exclude>
<exclude>com/alibaba/excel/metadata/DataFormatter.java</exclude>
<exclude>com/alibaba/excel/util/DateUtils.java</exclude>
</excludes>
</configuration>
<executions>
@ -201,7 +204,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>

37
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -15,6 +15,8 @@ import com.alibaba.excel.analysis.v03.XlsSaxAnalyser;
import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.context.DefaultXlsReadContext;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.read.metadata.ReadSheet;
@ -43,8 +45,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
public ExcelAnalyserImpl(ReadWorkbook readWorkbook) {
try {
analysisContext = new AnalysisContextImpl(readWorkbook);
choiceExcelExecutor();
choiceExcelExecutor(readWorkbook);
} catch (RuntimeException e) {
finish();
throw e;
@ -54,28 +55,25 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
}
}
private void choiceExcelExecutor() throws Exception {
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
ExcelTypeEnum excelType = readWorkbookHolder.getExcelType();
if (excelType == null) {
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null);
return;
}
private void choiceExcelExecutor(ReadWorkbook readWorkbook) throws Exception {
ExcelTypeEnum excelType =
ExcelTypeEnum.valueOf(readWorkbook.getFile(), readWorkbook.getInputStream(), readWorkbook.getExcelType());
readWorkbook.setExcelType(excelType);
switch (excelType) {
case XLS:
POIFSFileSystem poifsFileSystem;
if (readWorkbookHolder.getFile() != null) {
poifsFileSystem = new POIFSFileSystem(readWorkbookHolder.getFile());
if (readWorkbook.getFile() != null) {
poifsFileSystem = new POIFSFileSystem(readWorkbook.getFile());
} else {
poifsFileSystem = new POIFSFileSystem(readWorkbookHolder.getInputStream());
poifsFileSystem = new POIFSFileSystem(readWorkbook.getInputStream());
}
// So in encrypted excel, it looks like XLS but it's actually XLSX
if (poifsFileSystem.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
InputStream decryptedStream = null;
try {
decryptedStream =
DocumentFactoryHelper.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(),
analysisContext.readWorkbookHolder().getPassword());
decryptedStream = DocumentFactoryHelper
.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword());
analysisContext = new AnalysisContextImpl(readWorkbook);
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream);
return;
} finally {
@ -85,12 +83,15 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
poifsFileSystem.close();
}
}
if (analysisContext.readWorkbookHolder().getPassword() != null) {
Biff8EncryptionKey.setCurrentUserPassword(analysisContext.readWorkbookHolder().getPassword());
if (readWorkbook.getPassword() != null) {
Biff8EncryptionKey.setCurrentUserPassword(readWorkbook.getPassword());
}
excelReadExecutor = new XlsSaxAnalyser(analysisContext, poifsFileSystem);
XlsReadContext xlsReadContext = new DefaultXlsReadContext(readWorkbook);
analysisContext = xlsReadContext;
excelReadExecutor = new XlsSaxAnalyser(xlsReadContext, poifsFileSystem);
break;
case XLSX:
analysisContext = new AnalysisContextImpl(readWorkbook);
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null);
break;
default:

6
src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java

@ -1,5 +1,6 @@
package com.alibaba.excel.analysis.v03;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.metadata.CellData;
/**
@ -10,6 +11,11 @@ public abstract class AbstractXlsRecordHandler implements XlsRecordHandler {
protected int row = -1;
protected int column = -1;
protected CellData cellData;
protected XlsReadContext analysisContext;
public AbstractXlsRecordHandler(XlsReadContext analysisContext) {
this.analysisContext = analysisContext;
}
@Override
public int getRow() {

4
src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java

@ -14,7 +14,7 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet;
@ -28,7 +28,7 @@ public class XlsListSheetListener implements HSSFListener {
private List<ReadSheet> sheetList;
private BofRecordHandler bofRecordHandler;
public XlsListSheetListener(AnalysisContext analysisContext, POIFSFileSystem poifsFileSystem) {
public XlsListSheetListener(XlsReadContext analysisContext, POIFSFileSystem poifsFileSystem) {
this.poifsFileSystem = poifsFileSystem;
sheetList = new ArrayList<ReadSheet>();
bofRecordHandler = new BofRecordHandler(analysisContext, sheetList, false, false);

64
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java

@ -3,12 +3,10 @@ package com.alibaba.excel.analysis.v03;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.util.StringUtils;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
@ -21,11 +19,6 @@ import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -40,7 +33,8 @@ import com.alibaba.excel.analysis.v03.handlers.NoteRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.NumberRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.RkRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.SstRecordHandler;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.analysis.v03.handlers.TextObjectRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.CellData;
@ -72,6 +66,8 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private List<ReadSheet> readSheetList;
private int lastRowNumber;
private int lastColumnNumber;
private int ii = 0;
/**
* For parsing Formulas
*/
@ -81,19 +77,12 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private List<ReadSheet> sheets;
private HSSFWorkbook stubWorkbook;
private List<XlsRecordHandler> recordHandlers = new ArrayList<XlsRecordHandler>();
private AnalysisContext analysisContext;
private Workbook poiWorkbook;
private Map<Integer, String> rowComments;
private XlsReadContext analysisContext;
public XlsSaxAnalyser(AnalysisContext context, POIFSFileSystem poifsFileSystem) {
public XlsSaxAnalyser(XlsReadContext context, POIFSFileSystem poifsFileSystem) {
this.analysisContext = context;
this.records = new LinkedHashMap<Integer, CellData>();
this.poifsFileSystem = poifsFileSystem;
try {
this.poiWorkbook = WorkbookFactory.create(poifsFileSystem);
} catch (IOException e) {
e.printStackTrace();
}
analysisContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem);
}
@ -154,7 +143,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
thisRow = handler.getRow();
thisColumn = handler.getColumn();
cellData = handler.getCellData();
handleComments(thisRow, thisColumn);
if (cellData != null) {
cellData.checkEmpty();
if (CellDataTypeEnum.EMPTY != cellData.getType()) {
@ -186,26 +174,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
processLastCellOfRow(record);
}
public void handleComments(int row, int col) {
if (null == this.poiWorkbook || null == analysisContext.readSheetHolder() || row < 0 || col < 0) {
return;
}
Sheet currentSheet = poiWorkbook.getSheetAt(analysisContext.readSheetHolder().getSheetNo());
Map<CellAddress, ? extends Comment> cellComments = currentSheet.getCellComments();
if (CollectionUtils.isEmpty(cellComments)) {
return;
}
Comment comment = cellComments.get(new CellAddress(row, col));
if (null == comment) {
return;
}
String commentsStr = comment.getString().toString();
if (!StringUtils.isEmpty(commentsStr)) {
rowComments = rowComments == null ? new HashMap<Integer, String>(8) : rowComments;
rowComments.put(col, commentsStr);
}
}
private boolean ignoreRecord(Record record) {
return analysisContext.readWorkbookHolder().getIgnoreRecord03() && record.getSid() != BoundSheetRecord.sid
&& record.getSid() != BOFRecord.sid;
@ -224,17 +192,14 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
}
analysisContext.readRowHolder(
new ReadRowHolder(lastRowNumber, analysisContext.readSheetHolder().getGlobalConfiguration()));
if (!CollectionUtils.isEmpty(rowComments)) {
analysisContext.readRowHolder().setRowComments(rowComments);
}
analysisContext.readSheetHolder().notifyEndOneRow(new EachRowAnalysisFinishEvent(records), analysisContext);
records = new HashMap<Integer, CellData>();
this.records = new LinkedHashMap<Integer, CellData>();
lastColumnNumber = -1;
}
private void buildXlsRecordHandlers() {
if (CollectionUtils.isEmpty(recordHandlers)) {
recordHandlers.add(new BlankOrErrorRecordHandler());
recordHandlers.add(new BlankOrErrorRecordHandler(analysisContext));
// The table has been counted and there are no duplicate statistics
if (sheets == null) {
sheets = new ArrayList<ReadSheet>();
@ -242,14 +207,15 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
} else {
recordHandlers.add(new BofRecordHandler(analysisContext, sheets, true, true));
}
recordHandlers.add(new FormulaRecordHandler(stubWorkbook, formatListener));
recordHandlers.add(new LabelRecordHandler());
recordHandlers.add(new NoteRecordHandler());
recordHandlers.add(new FormulaRecordHandler(analysisContext, stubWorkbook, formatListener));
recordHandlers.add(new LabelRecordHandler(analysisContext));
recordHandlers.add(new NoteRecordHandler(analysisContext));
recordHandlers.add(new NumberRecordHandler(analysisContext, formatListener));
recordHandlers.add(new RkRecordHandler());
recordHandlers.add(new SstRecordHandler());
recordHandlers.add(new MissingCellDummyRecordHandler());
recordHandlers.add(new RkRecordHandler(analysisContext));
recordHandlers.add(new SstRecordHandler(analysisContext));
recordHandlers.add(new MissingCellDummyRecordHandler(analysisContext));
recordHandlers.add(new IndexRecordHandler(analysisContext));
recordHandlers.add(new TextObjectRecordHandler(analysisContext));
Collections.sort(recordHandlers);
}

5
src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java

@ -5,6 +5,7 @@ import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
@ -15,6 +16,10 @@ import com.alibaba.excel.metadata.CellData;
*/
public class BlankOrErrorRecordHandler extends AbstractXlsRecordHandler {
public BlankOrErrorRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return BlankRecord.sid == record.getSid() || BoolErrRecord.sid == record.getSid();

19
src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java

@ -8,7 +8,7 @@ import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.SheetUtils;
@ -24,13 +24,12 @@ public class BofRecordHandler extends AbstractXlsRecordHandler {
private List<ReadSheet> sheets;
private Boolean readAll;
private List<ReadSheet> readSheetList;
private AnalysisContext context;
private boolean alreadyInit;
private boolean needInitSheet;
public BofRecordHandler(AnalysisContext context, List<ReadSheet> sheets, boolean alreadyInit,
public BofRecordHandler(XlsReadContext analysisContext, List<ReadSheet> sheets, boolean alreadyInit,
boolean needInitSheet) {
this.context = context;
super(analysisContext);
this.sheets = sheets;
this.alreadyInit = alreadyInit;
this.needInitSheet = needInitSheet;
@ -68,17 +67,17 @@ public class BofRecordHandler extends AbstractXlsRecordHandler {
}
}
assert readSheet != null : "Can't find the sheet.";
context.readWorkbookHolder().setIgnoreRecord03(Boolean.TRUE);
analysisContext.readWorkbookHolder().setIgnoreRecord03(Boolean.TRUE);
// Copy the parameter to the current sheet
readSheet = SheetUtils.match(readSheet, readSheetList, readAll,
context.readWorkbookHolder().getGlobalConfiguration());
analysisContext.readWorkbookHolder().getGlobalConfiguration());
if (readSheet != null) {
if (readSheet.getSheetNo() != 0 && context.readSheetHolder() != null) {
if (readSheet.getSheetNo() != 0 && analysisContext.readSheetHolder() != null) {
// Prompt for the end of the previous form read
context.readSheetHolder().notifyAfterAllAnalysed(context);
analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext);
}
context.currentSheet(readSheet);
context.readWorkbookHolder().setIgnoreRecord03(Boolean.FALSE);
analysisContext.currentSheet(readSheet);
analysisContext.readWorkbookHolder().setIgnoreRecord03(Boolean.FALSE);
}
}
sheetIndex++;

6
src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java

@ -13,6 +13,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
@ -32,7 +33,9 @@ public class FormulaRecordHandler extends AbstractXlsRecordHandler {
private FormatTrackingHSSFListener formatListener;
private HSSFWorkbook stubWorkbook;
public FormulaRecordHandler(HSSFWorkbook stubWorkbook, FormatTrackingHSSFListener formatListener) {
public FormulaRecordHandler(XlsReadContext analysisContext, HSSFWorkbook stubWorkbook,
FormatTrackingHSSFListener formatListener) {
super(analysisContext);
this.stubWorkbook = stubWorkbook;
this.formatListener = formatListener;
}
@ -46,7 +49,6 @@ public class FormulaRecordHandler extends AbstractXlsRecordHandler {
public void processRecord(Record record) {
if (record.getSid() == FormulaRecord.sid) {
FormulaRecord frec = (FormulaRecord)record;
this.row = frec.getRow();
this.column = frec.getColumn();
CellType cellType = CellType.forInt(frec.getCachedResultType());

12
src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.IndexRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.XlsReadContext;
/**
* Record handler
@ -13,10 +13,8 @@ import com.alibaba.excel.context.AnalysisContext;
*/
public class IndexRecordHandler extends AbstractXlsRecordHandler {
private AnalysisContext context;
public IndexRecordHandler(AnalysisContext context) {
this.context = context;
public IndexRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
@ -29,10 +27,10 @@ public class IndexRecordHandler extends AbstractXlsRecordHandler {
@Override
public void processRecord(Record record) {
if (context.readSheetHolder() == null) {
if (analysisContext.readSheetHolder() == null) {
return;
}
context.readSheetHolder().setApproximateTotalRowNumber(((IndexRecord)record).getLastRowAdd1());
analysisContext.readSheetHolder().setApproximateTotalRowNumber(((IndexRecord)record).getLastRowAdd1());
}
@Override

6
src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java

@ -4,6 +4,7 @@ import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.metadata.CellData;
/**
@ -12,6 +13,11 @@ import com.alibaba.excel.metadata.CellData;
* @author Dan Zheng
*/
public class LabelRecordHandler extends AbstractXlsRecordHandler {
public LabelRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return LabelRecord.sid == record.getSid();

5
src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java

@ -4,6 +4,7 @@ import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
@ -13,6 +14,10 @@ import com.alibaba.excel.metadata.CellData;
* @author Dan Zheng
*/
public class MissingCellDummyRecordHandler extends AbstractXlsRecordHandler {
public MissingCellDummyRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return record instanceof MissingCellDummyRecord;

5
src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java

@ -4,6 +4,7 @@ import org.apache.poi.hssf.record.NoteRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
@ -13,6 +14,10 @@ import com.alibaba.excel.metadata.CellData;
* @author Dan Zheng
*/
public class NoteRecordHandler extends AbstractXlsRecordHandler {
public NoteRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return NoteRecord.sid == record.getSid();

13
src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java

@ -8,7 +8,7 @@ import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.metadata.CellData;
/**
@ -19,10 +19,8 @@ import com.alibaba.excel.metadata.CellData;
public class NumberRecordHandler extends AbstractXlsRecordHandler {
private FormatTrackingHSSFListener formatListener;
private AnalysisContext context;
public NumberRecordHandler(AnalysisContext context, FormatTrackingHSSFListener formatListener) {
this.context = context;
public NumberRecordHandler(XlsReadContext analysisContext, FormatTrackingHSSFListener formatListener) {
super(analysisContext);
this.formatListener = formatListener;
}
@ -39,8 +37,9 @@ public class NumberRecordHandler extends AbstractXlsRecordHandler {
this.cellData = new CellData(BigDecimal.valueOf(numrec.getValue()));
int dataFormat = formatListener.getFormatIndex(numrec);
this.cellData.setDataFormat(dataFormat);
this.cellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
formatListener.getFormatString(numrec), context.readSheetHolder().getGlobalConfiguration().getLocale()));
this.cellData
.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, formatListener.getFormatString(numrec),
analysisContext.readSheetHolder().getGlobalConfiguration().getLocale()));
}
@Override

5
src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java

@ -4,6 +4,7 @@ import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
@ -13,6 +14,10 @@ import com.alibaba.excel.metadata.CellData;
* @author Dan Zheng
*/
public class RkRecordHandler extends AbstractXlsRecordHandler {
public RkRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return RKRecord.sid == record.getSid();

5
src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java

@ -5,6 +5,7 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SSTRecord;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
@ -16,6 +17,10 @@ import com.alibaba.excel.metadata.CellData;
public class SstRecordHandler extends AbstractXlsRecordHandler {
private SSTRecord sstRecord;
public SstRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return SSTRecord.sid == record.getSid() || LabelSSTRecord.sid == record.getSid();

39
src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java

@ -0,0 +1,39 @@
package com.alibaba.excel.analysis.v03.handlers;
import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.TextObjectRecord;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
/**
* Record handler
*
* @author Jiaju Zhuang
*/
public class TextObjectRecordHandler extends AbstractXlsRecordHandler {
public TextObjectRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return TextObjectRecord.sid == record.getSid() || ObjRecord.sid == record.getSid();
}
@Override
public void processRecord(Record record) {
System.out.println(record);
}
@Override
public void init() {
}
@Override
public int getOrder() {
return 0;
}
}

2
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java

@ -159,7 +159,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override
public void handleComments(String comment) {
analysisContext.readRowHolder().addComments(curCol, comment);
// analysisContext.readRowHolder().addComments(curCol, comment);
}
@Override

16
src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java

@ -0,0 +1,16 @@
package com.alibaba.excel.context;
import com.alibaba.excel.read.metadata.ReadWorkbook;
/**
*
* A context is the main anchorage point of a ls xls reader.
*
* @author Jiaju Zhuang
*/
public class DefaultXlsReadContext extends AnalysisContextImpl implements XlsReadContext {
public DefaultXlsReadContext(ReadWorkbook readWorkbook) {
super(readWorkbook);
}
}

9
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -282,7 +282,6 @@ public class WriteContextImpl implements WriteContext {
throwable = t;
}
}
if (!isOutputStreamEncrypt) {
try {
if (writeExcel) {
@ -293,7 +292,6 @@ public class WriteContextImpl implements WriteContext {
throwable = t;
}
}
try {
Workbook workbook = writeWorkbookHolder.getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
@ -302,7 +300,6 @@ public class WriteContextImpl implements WriteContext {
} catch (Throwable t) {
throwable = t;
}
try {
if (writeWorkbookHolder.getAutoCloseStream() && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close();
@ -310,7 +307,6 @@ public class WriteContextImpl implements WriteContext {
} catch (Throwable t) {
throwable = t;
}
if (writeExcel && !isOutputStreamEncrypt) {
try {
doFileEncrypt07();
@ -318,7 +314,6 @@ public class WriteContextImpl implements WriteContext {
throwable = t;
}
}
try {
if (writeWorkbookHolder.getTempTemplateInputStream() != null) {
writeWorkbookHolder.getTempTemplateInputStream().close();
@ -326,15 +321,11 @@ public class WriteContextImpl implements WriteContext {
} catch (Throwable t) {
throwable = t;
}
clearEncrypt03();
removeThreadLocalCache();
if (throwable != null) {
throw new ExcelGenerateException("Can not close IO.", throwable);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Finished write.");
}

11
src/main/java/com/alibaba/excel/context/XlsReadContext.java

@ -0,0 +1,11 @@
package com.alibaba.excel.context;
/**
*
* A context is the main anchorage point of a ls xls reader.
*
* @author Jiaju Zhuang
*/
public interface XlsReadContext extends AnalysisContext {
}

13
src/main/java/com/alibaba/excel/enums/ExtraReadEnum.java

@ -0,0 +1,13 @@
package com.alibaba.excel.enums;
/**
* Read some extra data
*
* @author Jiaju Zhuang
**/
public enum ExtraReadEnum {
/**
* Read the comment
*/
COMMENT,;
}

5
src/main/java/com/alibaba/excel/metadata/AbstractParameterBuilder.java

@ -89,5 +89,10 @@ public abstract class AbstractParameterBuilder<T extends AbstractParameterBuilde
return (T)this;
}
/**
* Get parameter
*
* @return
*/
protected abstract C parameter();
}

18
src/main/java/com/alibaba/excel/metadata/CellExtra.java

@ -0,0 +1,18 @@
package com.alibaba.excel.metadata;
/**
* Cell extra information.
*
* @author Jiaju Zhuang
*/
public class CellExtra {
private String comment;
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}

16
src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java

@ -2,12 +2,14 @@ package com.alibaba.excel.read.metadata;
import java.io.File;
import java.io.InputStream;
import java.util.Set;
import javax.xml.parsers.SAXParserFactory;
import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.cache.selector.ReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.ExtraReadEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ModelBuildEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
@ -83,6 +85,12 @@ public class ReadWorkbook extends ReadBasicParameter {
* The {@link ModelBuildEventListener} is loaded by default to convert the object.
*/
private Boolean useDefaultListener;
/**
* Read some additional fields. None are read by default.
*
* @see ExtraReadEnum
*/
private Set<ExtraReadEnum> extraReadSet;
/**
* The default is all excel objects.Default is true.
* <p>
@ -212,4 +220,12 @@ public class ReadWorkbook extends ReadBasicParameter {
public void setUseDefaultListener(Boolean useDefaultListener) {
this.useDefaultListener = useDefaultListener;
}
public Set<ExtraReadEnum> getExtraReadSet() {
return extraReadSet;
}
public void setExtraReadSet(Set<ExtraReadEnum> extraReadSet) {
this.extraReadSet = extraReadSet;
}
}

32
src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java

@ -3,10 +3,6 @@ package com.alibaba.excel.read.metadata.holder;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.util.CollectionUtils;
import java.util.HashMap;
import java.util.Map;
/**
* sheet holder
@ -28,34 +24,6 @@ public class ReadRowHolder implements Holder {
*/
private GlobalConfiguration globalConfiguration;
/**
* Return row comments
* key: col index
* value: comments
*/
private Map<Integer, String> rowComments;
public Map<Integer, String> getRowComments() {
return rowComments;
}
public void setRowComments(Map<Integer, String> rowComments) {
this.rowComments = rowComments;
}
public void addComments(Integer index, String comments) {
this.rowComments = this.rowComments == null ? new HashMap<Integer, String>(8) : this.rowComments;
this.rowComments.put(index, comments);
}
public String getComments(Integer index) {
if (CollectionUtils.isEmpty(rowComments)) {
return null;
} else {
return rowComments.get(index);
}
}
public ReadRowHolder(Integer rowIndex, GlobalConfiguration globalConfiguration) {
this.rowIndex = rowIndex;
this.globalConfiguration = globalConfiguration;

24
src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java

@ -16,6 +16,7 @@ import com.alibaba.excel.cache.selector.EternalReadCacheSelector;
import com.alibaba.excel.cache.selector.ReadCacheSelector;
import com.alibaba.excel.cache.selector.SimpleReadCacheSelector;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.ExtraReadEnum;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
@ -96,6 +97,12 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
* @see SAXParserFactory#newInstance(String, ClassLoader)
*/
private String xlsxSAXParserFactoryName;
/**
* Read some additional fields. None are read by default.
*
* @see ExtraReadEnum
*/
private Set<ExtraReadEnum> extraReadSet;
/**
* The default is all excel objects.if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a
* field. if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed.
@ -155,9 +162,7 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.autoCloseStream = readWorkbook.getAutoCloseStream();
}
// The type of excel is read according to the judgment.Because encrypted XLSX needs to be specified as XLS to
// properly parse.
this.excelType = ExcelTypeEnum.valueOf(file, inputStream, readWorkbook.getExcelType());
this.excelType = readWorkbook.getExcelType();
if (ExcelTypeEnum.XLS == excelType && getGlobalConfiguration().getUse1904windowing() == null) {
getGlobalConfiguration().setUse1904windowing(Boolean.FALSE);
@ -186,6 +191,11 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.defaultReturnMap = readWorkbook.getDefaultReturnMap();
}
this.xlsxSAXParserFactoryName = readWorkbook.getXlsxSAXParserFactoryName();
if (readWorkbook.getExtraReadSet() == null) {
this.extraReadSet = new HashSet<ExtraReadEnum>();
} else {
this.extraReadSet = readWorkbook.getExtraReadSet();
}
this.hasReadSheet = new HashSet<Integer>();
this.ignoreRecord03 = Boolean.FALSE;
this.password = readWorkbook.getPassword();
@ -343,6 +353,14 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.xlsxSAXParserFactoryName = xlsxSAXParserFactoryName;
}
public Set<ExtraReadEnum> getExtraReadSet() {
return extraReadSet;
}
public void setExtraReadSet(Set<ExtraReadEnum> extraReadSet) {
this.extraReadSet = extraReadSet;
}
@Override
public HolderEnum holderType() {
return HolderEnum.WORKBOOK;

26
src/test/java/com/alibaba/easyexcel/test/demo/read/DemoCellCommentsListener.java

@ -1,16 +1,16 @@
package com.alibaba.easyexcel.test.demo.read;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 读取单元格的批注
@ -37,7 +37,7 @@ public class DemoCellCommentsListener extends AnalysisEventListener<DemoData> {
public void onException(Exception exception, AnalysisContext context) {
LOGGER.error("解析失败,但是继续解析下一行:{}", exception.getMessage());
if (exception instanceof ExcelDataConvertException) {
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception;
LOGGER.error("第{}行,第{}列解析异常", excelDataConvertException.getRowIndex(),
excelDataConvertException.getColumnIndex());
}
@ -50,15 +50,7 @@ public class DemoCellCommentsListener extends AnalysisEventListener<DemoData> {
* @param context
*/
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
Map<Integer, String> rowComments = context.readRowHolder().getRowComments();
LOGGER.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
if (!CollectionUtils.isEmpty(rowComments)) {
for (Integer i : rowComments.keySet()) {
LOGGER.info("解析到头数据低{}列包含批注:{}", i, rowComments.get(i));
}
}
}
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {}
@Override
public void invoke(DemoData data, AnalysisContext context) {

34
src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java

@ -0,0 +1,34 @@
package com.alibaba.easyexcel.test.temp.read;
import java.io.File;
import java.util.List;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.fastjson.JSON;
/**
* 临时测试
*
* @author Jiaju Zhuang
**/
@Ignore
public class CommentTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CommentTest.class);
@Test
public void comment() throws Exception {
File file = new File("D:\\test\\comment.xls");
List<Map<Integer, CellData>> datas = EasyExcel.read(file).sheet(0).doReadSync();
for (Map<Integer, CellData> data : datas) {
LOGGER.info("数据:{}", JSON.toJSONString(data.get(0)));
}
}
}

2
update.md

@ -3,6 +3,8 @@
* 降低Ehcache版本 3.7.1(jkd7) -> 3.4.0(jdk6)
* 修复xls 用Map接收时多次接收会是同一个对象的bug
* 修复浮点型数据导入到excel 会丢失精度的bug
* 新增支持接收批注
# 2.1.4
* 新增参数`useDefaultListener` 可以排除默认对象转换

Loading…
Cancel
Save