diff --git a/pom.xml b/pom.xml
index 51f6323..e50cf45 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
4.0.0
com.alibaba
easyexcel
- 2.2.0-beta2
+ 2.2.1
jar
easyexcel
diff --git a/src/main/java/com/alibaba/excel/ExcelReader.java b/src/main/java/com/alibaba/excel/ExcelReader.java
index 6cd0f78..7af49eb 100644
--- a/src/main/java/com/alibaba/excel/ExcelReader.java
+++ b/src/main/java/com/alibaba/excel/ExcelReader.java
@@ -136,6 +136,8 @@ public class ExcelReader {
readWorkbook.setReadCache(new MapCache());
readWorkbook.setConvertAllFiled(Boolean.FALSE);
readWorkbook.setDefaultReturnMap(Boolean.FALSE);
+ // The previous logic was that Article 0 started reading
+ readWorkbook.setHeadRowNumber(0);
excelAnalyser = new ExcelAnalyserImpl(readWorkbook);
}
diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
index 2b92253..bda4f53 100644
--- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
+++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
@@ -138,11 +138,6 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
} catch (IOException e) {
throw new ExcelAnalysisException(e);
}
- // Sometimes tables lack the end record of the last column
- if (!xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) {
- // Forge a termination data
- processRecord(new LastCellOfRowDummyRecord(xlsReadContext.xlsReadSheetHolder().getRowIndex() + 1, -1));
- }
}
@Override
diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java
index 0ca7313..32b5b88 100644
--- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java
+++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java
@@ -1,9 +1,16 @@
package com.alibaba.excel.analysis.v03.handlers;
+import java.util.LinkedHashMap;
+
+import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.xls.XlsReadContext;
+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;
/**
* Record handler
@@ -14,8 +21,21 @@ public class EofRecordHandler extends AbstractXlsRecordHandler implements Ignora
@Override
public void processRecord(XlsReadContext xlsReadContext, Record record) {
- if (xlsReadContext.readSheetHolder() != null) {
- xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext);
+ if (xlsReadContext.readSheetHolder() == null) {
+ return;
+ }
+ // Sometimes tables lack the end record of the last column
+ if (!xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) {
+ XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
+ // Forge a termination data
+ xlsReadContext.readRowHolder(new ReadRowHolder(xlsReadContext.xlsReadSheetHolder().getRowIndex() + 1,
+ xlsReadSheetHolder.getTempRowType(),
+ xlsReadContext.readSheetHolder().getGlobalConfiguration(), xlsReadSheetHolder.getCellMap()));
+ xlsReadContext.analysisEventProcessor().endRow(xlsReadContext);
+ xlsReadSheetHolder.setCellMap(new LinkedHashMap());
+ xlsReadSheetHolder.setTempRowType(RowTypeEnum.EMPTY);
}
+
+ xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext);
}
}
diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java
index 61f3873..329b224 100644
--- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java
+++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/RowTagHandler.java
@@ -25,15 +25,13 @@ public class RowTagHandler extends AbstractXlsxTagHandler {
int rowIndex = PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.ATTRIBUTE_R),
xlsxReadSheetHolder.getRowIndex());
Integer lastRowIndex = xlsxReadContext.readSheetHolder().getRowIndex();
- if (lastRowIndex != null) {
- while (lastRowIndex + 1 < rowIndex) {
- xlsxReadContext.readRowHolder(new ReadRowHolder(lastRowIndex + 1, RowTypeEnum.EMPTY,
- xlsxReadSheetHolder.getGlobalConfiguration(), new LinkedHashMap()));
- xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
- xlsxReadSheetHolder.setColumnIndex(null);
- xlsxReadSheetHolder.setCellMap(new LinkedHashMap());
- lastRowIndex++;
- }
+ while (lastRowIndex + 1 < rowIndex) {
+ xlsxReadContext.readRowHolder(new ReadRowHolder(lastRowIndex + 1, RowTypeEnum.EMPTY,
+ xlsxReadSheetHolder.getGlobalConfiguration(), new LinkedHashMap()));
+ xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
+ xlsxReadSheetHolder.setColumnIndex(null);
+ xlsxReadSheetHolder.setCellMap(new LinkedHashMap());
+ lastRowIndex++;
}
xlsxReadSheetHolder.setRowIndex(rowIndex);
}
diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
index 8465cbd..0af631c 100644
--- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
+++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java
@@ -111,7 +111,14 @@ public class WriteContextImpl implements WriteContext {
if (selectSheetFromCache(writeSheet)) {
return;
}
+
+
initCurrentSheetHolder(writeSheet);
+
+ // Workbook handler need to supplementary execution
+ WriteHandlerUtils.beforeWorkbookCreate(this, true);
+ WriteHandlerUtils.afterWorkbookCreate(this, true);
+
// Initialization current sheet
initSheet(writeType);
}
@@ -201,8 +208,8 @@ public class WriteContextImpl implements WriteContext {
if (currentWriteHolder.automaticMergeHead()) {
addMergedRegionToCurrentSheet(excelWriteHeadProperty, newRowIndex);
}
- for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber() + newRowIndex;
- i++, relativeRowIndex++) {
+ for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber()
+ + newRowIndex; i++, relativeRowIndex++) {
WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE);
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE);
@@ -227,7 +234,7 @@ public class WriteContextImpl implements WriteContext {
Cell cell = row.createCell(columnIndex);
WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE);
cell.setCellValue(head.getHeadNameList().get(relativeRowIndex));
- WriteHandlerUtils.afterCellDispose(this, (CellData)null, cell, head, relativeRowIndex, Boolean.TRUE);
+ WriteHandlerUtils.afterCellDispose(this, (CellData) null, cell, head, relativeRowIndex, Boolean.TRUE);
}
}
@@ -251,7 +258,15 @@ public class WriteContextImpl implements WriteContext {
}
return;
}
+
initCurrentTableHolder(writeTable);
+
+ // Workbook and sheet handler need to supplementary execution
+ WriteHandlerUtils.beforeWorkbookCreate(this, true);
+ WriteHandlerUtils.afterWorkbookCreate(this, true);
+ WriteHandlerUtils.beforeSheetCreate(this, true);
+ WriteHandlerUtils.afterSheetCreate(this, true);
+
initHead(writeTableHolder.excelWriteHeadProperty());
}
@@ -322,7 +337,7 @@ public class WriteContextImpl implements WriteContext {
try {
Workbook workbook = writeWorkbookHolder.getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
- ((SXSSFWorkbook)workbook).dispose();
+ ((SXSSFWorkbook) workbook).dispose();
}
} catch (Throwable t) {
throwable = t;
diff --git a/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java b/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java
new file mode 100644
index 0000000..43b1cb2
--- /dev/null
+++ b/src/main/java/com/alibaba/excel/converters/NullableObjectConverter.java
@@ -0,0 +1,11 @@
+package com.alibaba.excel.converters;
+
+/**
+ * When implementing convertToExcelData
method, pay attention to the reference value
may be
+ * null
+ *
+ * @author JiaJu Zhuang
+ **/
+public interface NullableObjectConverter extends Converter {
+
+}
diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java
index 87185dc..7a5272a 100644
--- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java
+++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java
@@ -15,6 +15,7 @@ import com.alibaba.excel.read.metadata.ReadSheet;
* @author Jiaju Zhuang
*/
public class ReadSheetHolder extends AbstractReadHolder {
+
/**
* current param
*/
@@ -59,6 +60,7 @@ public class ReadSheetHolder extends AbstractReadHolder {
this.sheetNo = readSheet.getSheetNo();
this.sheetName = readSheet.getSheetName();
this.cellMap = new LinkedHashMap();
+ this.rowIndex = -1;
}
public ReadSheet getReadSheet() {
diff --git a/src/main/java/com/alibaba/excel/util/ClassUtils.java b/src/main/java/com/alibaba/excel/util/ClassUtils.java
index 64bc29d..20b30e3 100644
--- a/src/main/java/com/alibaba/excel/util/ClassUtils.java
+++ b/src/main/java/com/alibaba/excel/util/ClassUtils.java
@@ -109,11 +109,12 @@ public class ClassUtils {
+ "' and '" + field.getName() + "' must be inconsistent");
}
customFiledMap.put(excelProperty.index(), field);
- allFieldList.add(field);
}
+ List allWriteFieldList = new ArrayList(customFiledMap.values());
+ allWriteFieldList.addAll(allFieldList);
FIELD_CACHE.put(clazz,
- new SoftReference(new FieldCache(defaultFieldList, customFiledMap, allFieldList, ignoreMap)));
+ new SoftReference(new FieldCache(defaultFieldList, customFiledMap, allWriteFieldList, ignoreMap)));
}
private static class FieldCache {
diff --git a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
index 06dcc72..57f0c49 100644
--- a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
+++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
@@ -2,6 +2,7 @@ package com.alibaba.excel.util;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
@@ -24,66 +25,82 @@ public class WriteHandlerUtils {
private WriteHandlerUtils() {}
+
public static void beforeWorkbookCreate(WriteContext writeContext) {
- List handlerList =
- writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
+ beforeWorkbookCreate(writeContext, false);
+ }
+
+ public static void beforeWorkbookCreate(WriteContext writeContext, boolean runOwn) {
+ List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
- ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate();
+ ((WorkbookWriteHandler) writeHandler).beforeWorkbookCreate();
}
}
}
public static void afterWorkbookCreate(WriteContext writeContext) {
- List handlerList =
- writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
+ afterWorkbookCreate(writeContext, false);
+ }
+
+ public static void afterWorkbookCreate(WriteContext writeContext, boolean runOwn) {
+ List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
- ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(writeContext.writeWorkbookHolder());
+ ((WorkbookWriteHandler) writeHandler).afterWorkbookCreate(writeContext.writeWorkbookHolder());
}
}
}
public static void afterWorkbookDispose(WriteContext writeContext) {
List handlerList =
- writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
+ writeContext.currentWriteHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
- ((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(writeContext.writeWorkbookHolder());
+ ((WorkbookWriteHandler) writeHandler).afterWorkbookDispose(writeContext.writeWorkbookHolder());
}
}
}
public static void beforeSheetCreate(WriteContext writeContext) {
- List handlerList = writeContext.writeSheetHolder().writeHandlerMap().get(SheetWriteHandler.class);
+ beforeSheetCreate(writeContext, false);
+ }
+
+ public static void beforeSheetCreate(WriteContext writeContext, boolean runOwn) {
+ List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
- ((SheetWriteHandler)writeHandler).beforeSheetCreate(writeContext.writeWorkbookHolder(),
+ ((SheetWriteHandler) writeHandler).beforeSheetCreate(writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder());
}
}
}
+
public static void afterSheetCreate(WriteContext writeContext) {
- List handlerList = writeContext.writeSheetHolder().writeHandlerMap().get(SheetWriteHandler.class);
+ afterSheetCreate(writeContext, false);
+ }
+
+ public static void afterSheetCreate(WriteContext writeContext, boolean runOwn) {
+ List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
- ((SheetWriteHandler)writeHandler).afterSheetCreate(writeContext.writeWorkbookHolder(),
+ ((SheetWriteHandler) writeHandler).afterSheetCreate(writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder());
}
}
@@ -102,7 +119,7 @@ public class WriteHandlerUtils {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
- ((CellWriteHandler)writeHandler).beforeCellCreate(writeContext.writeSheetHolder(),
+ ((CellWriteHandler) writeHandler).beforeCellCreate(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), row, head, columnIndex, relativeRowIndex, isHead);
}
}
@@ -117,7 +134,7 @@ public class WriteHandlerUtils {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
- ((CellWriteHandler)writeHandler).afterCellCreate(writeContext.writeSheetHolder(),
+ ((CellWriteHandler) writeHandler).afterCellCreate(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), cell, head, relativeRowIndex, isHead);
}
}
@@ -132,7 +149,7 @@ public class WriteHandlerUtils {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
- ((CellWriteHandler)writeHandler).afterCellDataConverted(writeContext.writeSheetHolder(),
+ ((CellWriteHandler) writeHandler).afterCellDataConverted(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), cellData, cell, head, relativeRowIndex, isHead);
}
}
@@ -156,7 +173,7 @@ public class WriteHandlerUtils {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
- ((CellWriteHandler)writeHandler).afterCellDispose(writeContext.writeSheetHolder(),
+ ((CellWriteHandler) writeHandler).afterCellDispose(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), cellDataList, cell, head, relativeRowIndex, isHead);
}
}
@@ -173,7 +190,7 @@ public class WriteHandlerUtils {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
- ((RowWriteHandler)writeHandler).beforeRowCreate(writeContext.writeSheetHolder(),
+ ((RowWriteHandler) writeHandler).beforeRowCreate(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), rowIndex, relativeRowIndex, isHead);
}
}
@@ -186,7 +203,7 @@ public class WriteHandlerUtils {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
- ((RowWriteHandler)writeHandler).afterRowCreate(writeContext.writeSheetHolder(),
+ ((RowWriteHandler) writeHandler).afterRowCreate(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), row, relativeRowIndex, isHead);
}
}
@@ -200,7 +217,7 @@ public class WriteHandlerUtils {
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
- ((RowWriteHandler)writeHandler).afterRowDispose(writeContext.writeSheetHolder(),
+ ((RowWriteHandler) writeHandler).afterRowDispose(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), row, relativeRowIndex, isHead);
}
}
@@ -208,4 +225,15 @@ public class WriteHandlerUtils {
writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler().row(row.getRowNum(), row);
}
}
+
+ private static List getHandlerList(WriteContext writeContext, Class extends WriteHandler> clazz,
+ boolean runOwn) {
+ Map, List> writeHandlerMap;
+ if (runOwn) {
+ writeHandlerMap = writeContext.currentWriteHolder().ownWriteHandlerMap();
+ } else {
+ writeHandlerMap = writeContext.currentWriteHolder().writeHandlerMap();
+ }
+ return writeHandlerMap.get(WorkbookWriteHandler.class);
+ }
}
diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
index 4eb115c..12250f1 100644
--- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
@@ -10,6 +10,7 @@ import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild;
+import com.alibaba.excel.converters.NullableObjectConverter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData;
@@ -32,11 +33,10 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
protected CellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty, Head head, Integer relativeRowIndex) {
- if (value == null) {
- return new CellData(CellDataTypeEnum.EMPTY);
- }
- if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
- value = ((String)value).trim();
+ boolean needTrim =
+ value != null && (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim());
+ if (needTrim) {
+ value = ((String) value).trim();
}
CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty);
if (cellData.getFormula() != null && cellData.getFormula()) {
@@ -70,9 +70,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
protected CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty) {
- if (value == null) {
- return new CellData(CellDataTypeEnum.EMPTY);
- }
// This means that the user has defined the data.
if (value instanceof CellData) {
CellData cellDataValue = (CellData)value;
@@ -110,6 +107,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
new CellData(CellDataTypeEnum.EMPTY), excelContentProperty,
"Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
}
+ if (value == null && !(converter instanceof NullableObjectConverter)) {
+ return new CellData(CellDataTypeEnum.EMPTY);
+ }
CellData cellData;
try {
cellData =
diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
index 51d7d3a..1a42fde 100644
--- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
@@ -48,28 +48,28 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
/**
* Fields to replace in the template
*/
- private Map> templateAnalysisCache = new HashMap>(8);
+ private final Map> templateAnalysisCache = new HashMap>(8);
/**
* Collection fields to replace in the template
*/
- private Map> templateCollectionAnalysisCache =
+ private final Map> templateCollectionAnalysisCache =
new HashMap>(8);
/**
* Style cache for collection fields
*/
- private Map> collectionFieldStyleCache =
+ private final Map> collectionFieldStyleCache =
new HashMap>(8);
/**
* Row height cache for collection
*/
- private Map collectionRowHeightCache = new HashMap(8);
+ private final Map collectionRowHeightCache = new HashMap(8);
/**
* Last index cache for collection fields
*/
- private Map> collectionLastIndexCache =
+ private final Map> collectionLastIndexCache =
new HashMap>(8);
- private Map relativeRowIndexMap = new HashMap(8);
+ private final Map relativeRowIndexMap = new HashMap(8);
/**
* The data prefix that is populated this time
*/
@@ -94,7 +94,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
Object realData;
if (data instanceof FillWrapper) {
- FillWrapper fillWrapper = (FillWrapper)data;
+ FillWrapper fillWrapper = (FillWrapper) data;
currentDataPrefix = fillWrapper.getName();
realData = fillWrapper.getCollectionData();
} else {
@@ -106,7 +106,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
// processing data
if (realData instanceof Collection) {
List analysisCellList = readTemplateData(templateCollectionAnalysisCache);
- Collection collectionData = (Collection)realData;
+ Collection collectionData = (Collection) realData;
if (CollectionUtils.isEmpty(collectionData)) {
return;
}
@@ -156,9 +156,23 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
return;
}
sheet.shiftRows(maxRowIndex + 1, lastRowIndex, number, true, false);
- for (AnalysisCell analysisCell : templateAnalysisCache.get(currentUniqueDataFlag)) {
- if (analysisCell.getRowIndex() > maxRowIndex) {
- analysisCell.setRowIndex(analysisCell.getRowIndex() + number);
+
+ // The current data is greater than unity rowindex increase
+ String tablePrefix = tablePrefix(currentUniqueDataFlag);
+ increaseRowIndex(templateAnalysisCache, number, maxRowIndex, tablePrefix);
+ increaseRowIndex(templateCollectionAnalysisCache, number, maxRowIndex, tablePrefix);
+ }
+
+ private void increaseRowIndex(Map> templateAnalysisCache, int number, int maxRowIndex,
+ String tablePrefix) {
+ for (Map.Entry> entry : templateAnalysisCache.entrySet()) {
+ if (!tablePrefix.equals(tablePrefix(entry.getKey()))) {
+ continue;
+ }
+ for (AnalysisCell analysisCell : entry.getValue()) {
+ if (analysisCell.getRowIndex() > maxRowIndex) {
+ analysisCell.setRowIndex(analysisCell.getRowIndex() + number);
+ }
}
}
}
@@ -167,7 +181,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
Integer relativeRowIndex) {
Map dataMap;
if (oneRowData instanceof Map) {
- dataMap = (Map)oneRowData;
+ dataMap = (Map) oneRowData;
} else {
dataMap = BeanMap.create(oneRowData);
}
@@ -384,8 +398,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
int startIndex = 0;
int length = value.length();
int lastPrepareDataIndex = 0;
- out:
- while (startIndex < length) {
+ out: while (startIndex < length) {
int prefixIndex = value.indexOf(FILL_PREFIX, startIndex);
if (prefixIndex < 0) {
break out;
@@ -515,4 +528,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
return prefix + "-" + wrapperName;
}
+ private String tablePrefix(String uniqueDataFlag) {
+ return uniqueDataFlag.substring(0, uniqueDataFlag.indexOf("-") + 1);
+ }
+
}
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
index 00f2ef7..77d5d06 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
@@ -64,9 +64,14 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
*/
private ExcelWriteHeadProperty excelWriteHeadProperty;
/**
- * Write handler for workbook
+ * Write handler
*/
private Map, List> writeHandlerMap;
+ /**
+ * Own write handler.Created in the sheet in the workbook interceptors will not be executed because the workbook to
+ * create an event long past. So when initializing sheet, supplementary workbook event.
+ */
+ private Map, List> ownWriteHandlerMap;
/**
* Use the default style.Default is true.
*/
@@ -177,21 +182,22 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
List handlerList = new ArrayList();
// Initialization Annotation
- initAnnotationConfig(handlerList);
+ initAnnotationConfig(handlerList, writeBasicParameter);
if (writeBasicParameter.getCustomWriteHandlerList() != null
&& !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) {
handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList());
}
+ this.ownWriteHandlerMap = sortAndClearUpHandler(handlerList);
+
Map, List> parentWriteHandlerMap = null;
if (parentAbstractWriteHolder != null) {
parentWriteHandlerMap = parentAbstractWriteHolder.getWriteHandlerMap();
} else {
handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle));
}
-
- this.writeHandlerMap = sortAndClearUpHandler(handlerList, parentWriteHandlerMap);
+ this.writeHandlerMap = sortAndClearUpAllHandler(handlerList, parentWriteHandlerMap);
// Set converterMap
if (parentAbstractWriteHolder == null) {
@@ -215,13 +221,13 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
switch (holderType()) {
case SHEET:
compatibleOldCodeCreateRowCellStyleStrategy(writeBasicParameter,
- ((WriteSheet)writeBasicParameter).getTableStyle());
+ ((WriteSheet) writeBasicParameter).getTableStyle());
compatibleOldCodeCreateHeadColumnWidthStyleStrategy(writeBasicParameter,
- ((WriteSheet)writeBasicParameter).getColumnWidthMap());
+ ((WriteSheet) writeBasicParameter).getColumnWidthMap());
return;
case TABLE:
compatibleOldCodeCreateRowCellStyleStrategy(writeBasicParameter,
- ((WriteTable)writeBasicParameter).getTableStyle());
+ ((WriteTable) writeBasicParameter).getTableStyle());
return;
default:
}
@@ -279,10 +285,13 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
});
}
- protected void initAnnotationConfig(List handlerList) {
+ protected void initAnnotationConfig(List handlerList, WriteBasicParameter writeBasicParameter) {
if (!HeadKindEnum.CLASS.equals(getExcelWriteHeadProperty().getHeadKind())) {
return;
}
+ if (writeBasicParameter.getClazz() == null) {
+ return;
+ }
Map headMap = getExcelWriteHeadProperty().getHeadMap();
boolean hasColumnWidth = false;
boolean hasStyle = false;
@@ -308,7 +317,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
dealRowHigh(handlerList);
dealOnceAbsoluteMerge(handlerList);
-
}
private void dealStyle(List handlerList) {
@@ -376,9 +384,9 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
handlerList.add(columnWidthStyleStrategy);
}
- protected Map, List> sortAndClearUpHandler(
- List handlerList, Map, List> parentHandlerMap) {
+ protected Map, List> sortAndClearUpAllHandler(
+ List handlerList, Map, List> parentHandlerMap) {
// add
if (parentHandlerMap != null) {
List parentWriteHandler = parentHandlerMap.get(WriteHandler.class);
@@ -386,13 +394,17 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
handlerList.addAll(parentWriteHandler);
}
}
+ return sortAndClearUpHandler(handlerList);
+ }
+ protected Map, List> sortAndClearUpHandler(
+ List handlerList) {
// sort
Map> orderExcelWriteHandlerMap = new TreeMap>();
for (WriteHandler handler : handlerList) {
int order = Integer.MIN_VALUE;
if (handler instanceof Order) {
- order = ((Order)handler).order();
+ order = ((Order) handler).order();
}
if (orderExcelWriteHandlerMap.containsKey(order)) {
orderExcelWriteHandlerMap.get(order).add(handler);
@@ -408,7 +420,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
for (Map.Entry> entry : orderExcelWriteHandlerMap.entrySet()) {
for (WriteHandler handler : entry.getValue()) {
if (handler instanceof NotRepeatExecutor) {
- String uniqueValue = ((NotRepeatExecutor)handler).uniqueValue();
+ String uniqueValue = ((NotRepeatExecutor) handler).uniqueValue();
if (alreadyExistedHandlerSet.contains(uniqueValue)) {
continue;
}
@@ -480,6 +492,15 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
this.writeHandlerMap = writeHandlerMap;
}
+ public Map, List> getOwnWriteHandlerMap() {
+ return ownWriteHandlerMap;
+ }
+
+ public void setOwnWriteHandlerMap(
+ Map, List> ownWriteHandlerMap) {
+ this.ownWriteHandlerMap = ownWriteHandlerMap;
+ }
+
public ExcelWriteHeadProperty getExcelWriteHeadProperty() {
return excelWriteHeadProperty;
}
@@ -554,6 +575,11 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
return getWriteHandlerMap();
}
+ @Override
+ public Map, List> ownWriteHandlerMap() {
+ return getOwnWriteHandlerMap();
+ }
+
@Override
public boolean needHead() {
return getNeedHead();
diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java
index 2aa2066..4896b03 100644
--- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java
+++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java
@@ -28,6 +28,13 @@ public interface WriteHolder extends ConfigurationHolder {
*/
Map, List> writeHandlerMap();
+ /**
+ * create your own write handler.
+ *
+ * @return
+ */
+ Map, List> ownWriteHandlerMap();
+
/**
* Is to determine if a field needs to be ignored
*
diff --git a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java
index 4790ab1..06a596f 100644
--- a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java
+++ b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java
@@ -11,6 +11,7 @@ import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
+import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
@@ -20,7 +21,7 @@ import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
*
* @author Jiaju Zhuang
*/
-public abstract class AbstractCellStyleStrategy implements CellWriteHandler, SheetWriteHandler, NotRepeatExecutor {
+public abstract class AbstractCellStyleStrategy implements CellWriteHandler, WorkbookWriteHandler, NotRepeatExecutor {
boolean hasInitialized = false;
@Override
@@ -60,16 +61,21 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, She
}
@Override
- public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
+ public void beforeWorkbookCreate() {
}
@Override
- public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
+ public void afterWorkbookCreate(WriteWorkbookHolder writeWorkbookHolder) {
initCellStyle(writeWorkbookHolder.getWorkbook());
hasInitialized = true;
}
+ @Override
+ public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) {
+
+ }
+
/**
* Initialization cell style
*
diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
index 6c9bd8d..c801077 100644
--- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
+++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
@@ -32,7 +32,7 @@ public class Lock2Test {
@Test
public void test() throws Exception {
- File file = new File("D:\\test\\headt1.xlsx");
+ File file = TestFileUtil.readUserHomeFile("test/t3.xls");
List