Browse Source

优化读写性能

pull/2159/head
Jiaju Zhuang 3 years ago
parent
commit
031fefde92
  1. 18
      src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java
  2. 7
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  3. 4
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  4. 48
      src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java
  5. 61
      src/main/java/com/alibaba/excel/util/PositionUtils.java
  6. 203
      src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
  7. 6
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  8. 10
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  9. 63
      src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java
  10. 56
      src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java
  11. 48
      src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java
  12. 57
      src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java
  13. 9
      src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java
  14. 8
      src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java
  15. 12
      src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java
  16. 13
      src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java
  17. 173
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  18. 18
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java
  19. 11
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  20. 17
      src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

18
src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java

@ -2,19 +2,15 @@ package com.alibaba.excel.analysis.v07.handlers;
import java.math.BigDecimal; import java.math.BigDecimal;
import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.constant.ExcelXmlConstants; import com.alibaba.excel.constant.ExcelXmlConstants;
import com.alibaba.excel.context.xlsx.XlsxReadContext; import com.alibaba.excel.context.xlsx.XlsxReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.DataFormatData;
import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
/** /**
@ -51,17 +47,9 @@ public class CellTagHandler extends AbstractXlsxTagHandler {
} else { } else {
dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); dateFormatIndexInteger = Integer.parseInt(dateFormatIndex);
} }
StylesTable stylesTable = xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable();
if (stylesTable == null) { xlsxReadSheetHolder.getTempCellData().setDataFormatData(
return; xlsxReadContext.xlsxReadWorkbookHolder().dataFormatData(dateFormatIndexInteger));
}
XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger);
short dataFormat = xssfCellStyle.getDataFormat();
DataFormatData dataFormatData = new DataFormatData();
dataFormatData.setIndex(dataFormat);
dataFormatData.setFormat(BuiltinFormats.getBuiltinFormat(dataFormat,
xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale()));
xlsxReadSheetHolder.getTempCellData().setDataFormatData(dataFormatData);
} }
@Override @Override

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

@ -127,7 +127,7 @@ public class WriteContextImpl implements WriteContext {
// Workbook handler need to supplementary execution // Workbook handler need to supplementary execution
WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
this, true); this);
WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true);
WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true);
@ -310,12 +310,11 @@ public class WriteContextImpl implements WriteContext {
// Workbook and sheet handler need to supplementary execution // Workbook and sheet handler need to supplementary execution
WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
this, true); this);
WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true);
WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true);
SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this, SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this);
true);
WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext, true); WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext, true);
WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext, true); WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext, true);

4
src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java

@ -91,7 +91,6 @@ public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCe
"Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e); "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
} }
Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap(); Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
Map<String, Object> map = MapUtils.newHashMapWithExpectedSize(headMap.size());
BeanMap dataMap = BeanMapUtils.create(resultModel); BeanMap dataMap = BeanMapUtils.create(resultModel);
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) { for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
Integer index = entry.getKey(); Integer index = entry.getKey();
@ -105,10 +104,9 @@ public class ModelBuildEventListener implements ReadListener<Map<Integer, ReadCe
ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(), ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(),
fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index); fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index);
if (value != null) { if (value != null) {
map.put(fieldName, value); dataMap.put(fieldName, value);
} }
} }
dataMap.putAll(map);
return resultModel; return resultModel;
} }

48
src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java

@ -1,19 +1,26 @@
package com.alibaba.excel.read.metadata.holder.xlsx; package com.alibaba.excel.read.metadata.holder.xlsx;
import javax.xml.parsers.SAXParserFactory; import java.util.Map;
import org.apache.poi.openxml4j.opc.OPCPackage; import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.xssf.model.StylesTable;
import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.metadata.data.DataFormatData;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.Data;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
/** /**
* Workbook holder * Workbook holder
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@Data
public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { public class XlsxReadWorkbookHolder extends ReadWorkbookHolder {
/** /**
* Package * Package
@ -34,6 +41,10 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder {
* Current style information * Current style information
*/ */
private StylesTable stylesTable; private StylesTable stylesTable;
/**
* cache data format
*/
private Map<Integer, DataFormatData> dataFormatDataCache;
public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) { public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook); super(readWorkbook);
@ -41,27 +52,18 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder {
setExcelType(ExcelTypeEnum.XLSX); setExcelType(ExcelTypeEnum.XLSX);
} }
public OPCPackage getOpcPackage() { public DataFormatData dataFormatData(int dateFormatIndexInteger) {
return opcPackage; return dataFormatDataCache.computeIfAbsent(dateFormatIndexInteger, key -> {
DataFormatData dataFormatData = new DataFormatData();
if (stylesTable == null) {
return null;
} }
XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger);
public void setOpcPackage(OPCPackage opcPackage) { dataFormatData.setIndex(xssfCellStyle.getDataFormat());
this.opcPackage = opcPackage; dataFormatData.setFormat(BuiltinFormats.getBuiltinFormat(dataFormatData.getIndex(),
} xssfCellStyle.getDataFormatString(), globalConfiguration().getLocale()));
return dataFormatData;
public String getSaxParserFactoryName() { });
return saxParserFactoryName;
}
public void setSaxParserFactoryName(String saxParserFactoryName) {
this.saxParserFactoryName = saxParserFactoryName;
}
public StylesTable getStylesTable() {
return stylesTable;
} }
public void setStylesTable(StylesTable stylesTable) {
this.stylesTable = stylesTable;
}
} }

61
src/main/java/com/alibaba/excel/util/PositionUtils.java

@ -1,11 +1,7 @@
package com.alibaba.excel.util; package com.alibaba.excel.util;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.poi.ss.util.CellReference;
/** /**
* @author jipengfei * @author jipengfei
*/ */
@ -18,14 +14,6 @@ public class PositionUtils {
private PositionUtils() {} private PositionUtils() {}
public static int getRowByRowTagt(String rowTagt) {
int row = 0;
if (rowTagt != null) {
row = Integer.parseInt(rowTagt) - 1;
}
return row;
}
public static int getRowByRowTagt(String rowTagt, Integer before) { public static int getRowByRowTagt(String rowTagt, Integer before) {
int row; int row;
if (rowTagt != null) { if (rowTagt != null) {
@ -37,47 +25,40 @@ public class PositionUtils {
} }
return before + 1; return before + 1;
} }
} }
public static int getRow(String currentCellIndex) { public static int getRow(String currentCellIndex) {
if (currentCellIndex != null) { if (currentCellIndex == null) {
int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER);
String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT);
Matcher matcher = CELL_REF_PATTERN.matcher(cell);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex);
}
String row = matcher.group(2);
return Integer.parseInt(row) - 1;
}
return -1; return -1;
} }
int firstNumber = currentCellIndex.length() - 1;
public static int getCol(String currentCellIndex, Integer before) { for (; firstNumber >= 0; firstNumber--) {
if (currentCellIndex != null) { char c = currentCellIndex.charAt(firstNumber);
int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER); if (c < '0' || c > '9') {
String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT); break;
Matcher matcher = CELL_REF_PATTERN.matcher(cell);
if (!matcher.matches()) {
throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex);
} }
String col = matcher.group(1);
if (col.length() > 0 && col.charAt(0) == REDUNDANT_CHARACTERS) {
col = col.substring(1);
} }
if (col.length() == 0) { return Integer.parseUnsignedInt(currentCellIndex.substring(firstNumber + 1)) - 1;
return -1;
} else {
return CellReference.convertColStringToIndex(col);
} }
} else {
public static int getCol(String currentCellIndex, Integer before) {
if (currentCellIndex == null) {
if (before == null) { if (before == null) {
before = -1; before = -1;
} }
return before + 1; return before + 1;
} }
int firstNumber = currentCellIndex.charAt(0) == REDUNDANT_CHARACTERS ? 1 : 0;
int col = 0;
for (; firstNumber < currentCellIndex.length(); firstNumber++) {
char c = currentCellIndex.charAt(firstNumber);
boolean isNotLetter = c == REDUNDANT_CHARACTERS || (c >= '0' && c <= '9');
if (isNotLetter) {
break;
}
col = col * 26 + Character.toUpperCase(c) - 'A' + 1;
}
return col - 1;
} }
} }

203
src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java

@ -1,22 +1,19 @@
package com.alibaba.excel.util; package com.alibaba.excel.util;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.chain.CellHandlerExecutionChain;
import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.chain.RowHandlerExecutionChain;
import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.handler.chain.SheetHandlerExecutionChain;
import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.chain.WorkbookHandlerExecutionChain;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder;
import org.apache.commons.collections4.CollectionUtils; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
/** /**
@ -24,23 +21,14 @@ import org.apache.poi.ss.usermodel.Row;
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@Slf4j
public class WriteHandlerUtils { public class WriteHandlerUtils {
private WriteHandlerUtils() {} private WriteHandlerUtils() {}
public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext) { public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext) {
return createWorkbookWriteHandlerContext(writeContext, false); WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext,
} writeContext.writeWorkbookHolder());
public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext,
boolean runOwn) {
List<WriteHandler> handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn);
WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null, null, null);
if (runOwn) {
context.setOwnHandlerList(handlerList);
} else {
context.setHandlerList(handlerList);
}
writeContext.writeWorkbookHolder().setWorkbookWriteHandlerContext(context); writeContext.writeWorkbookHolder().setWorkbookWriteHandlerContext(context);
return context; return context;
} }
@ -50,16 +38,9 @@ public class WriteHandlerUtils {
} }
public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList; WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn);
if (runOwn) { if (workbookHandlerExecutionChain != null) {
handlerList = context.getOwnHandlerList(); workbookHandlerExecutionChain.beforeWorkbookCreate(context);
} else {
handlerList = context.getHandlerList();
}
if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) {
((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(context);
}
} }
} }
@ -68,42 +49,32 @@ public class WriteHandlerUtils {
} }
public static void afterWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { public static void afterWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList; WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn);
if (runOwn) { if (workbookHandlerExecutionChain != null) {
handlerList = context.getOwnHandlerList(); workbookHandlerExecutionChain.afterWorkbookCreate(context);
} else {
handlerList = context.getHandlerList();
} }
if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) {
((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(context);
} }
private static WorkbookHandlerExecutionChain getWorkbookHandlerExecutionChain(WorkbookWriteHandlerContext context,
boolean runOwn) {
AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder();
if (runOwn) {
return abstractWriteHolder.getOwnWorkbookHandlerExecutionChain();
} else {
return abstractWriteHolder.getWorkbookHandlerExecutionChain();
} }
} }
public static void afterWorkbookDispose(WorkbookWriteHandlerContext context) { public static void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, false);
if (CollectionUtils.isNotEmpty(handlerList)) { if (workbookHandlerExecutionChain != null) {
for (WriteHandler writeHandler : handlerList) { workbookHandlerExecutionChain.afterWorkbookDispose(context);
((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(context);
}
} }
} }
public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext) { public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext) {
return createSheetWriteHandlerContext(writeContext, false); return new SheetWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
} writeContext.writeSheetHolder());
public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext, boolean runOwn) {
List<WriteHandler> handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn);
SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext,
writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), null, null);
if (runOwn) {
context.setOwnHandlerList(handlerList);
} else {
context.setHandlerList(handlerList);
}
return context;
} }
public static void beforeSheetCreate(SheetWriteHandlerContext context) { public static void beforeSheetCreate(SheetWriteHandlerContext context) {
@ -111,16 +82,9 @@ public class WriteHandlerUtils {
} }
public static void beforeSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { public static void beforeSheetCreate(SheetWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList; SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn);
if (runOwn) { if (sheetHandlerExecutionChain != null) {
handlerList = context.getOwnHandlerList(); sheetHandlerExecutionChain.beforeSheetCreate(context);
} else {
handlerList = context.getHandlerList();
}
if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) {
((SheetWriteHandler)writeHandler).beforeSheetCreate(context);
}
} }
} }
@ -129,111 +93,90 @@ public class WriteHandlerUtils {
} }
public static void afterSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { public static void afterSheetCreate(SheetWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList; SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn);
if (runOwn) { if (sheetHandlerExecutionChain != null) {
handlerList = context.getOwnHandlerList(); sheetHandlerExecutionChain.afterSheetCreate(context);
} else {
handlerList = context.getHandlerList();
} }
if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) {
((SheetWriteHandler)writeHandler).afterSheetCreate(context);
} }
private static SheetHandlerExecutionChain getSheetHandlerExecutionChain(SheetWriteHandlerContext context,
boolean runOwn) {
AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder();
if (runOwn) {
return abstractWriteHolder.getOwnSheetHandlerExecutionChain();
} else {
return abstractWriteHolder.getSheetHandlerExecutionChain();
} }
} }
public static CellWriteHandlerContext createCellWriteHandlerContext(WriteContext writeContext, Row row, public static CellWriteHandlerContext createCellWriteHandlerContext(WriteContext writeContext, Row row,
Integer rowIndex, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, Integer rowIndex, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead,
ExcelContentProperty excelContentProperty) { ExcelContentProperty excelContentProperty) {
List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(
CellWriteHandler.class);
return new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), return new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, rowIndex, null, columnIndex, writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, rowIndex, null, columnIndex,
relativeRowIndex, head, null, null, isHead, excelContentProperty, handlerList); relativeRowIndex, head, null, null, isHead, excelContentProperty);
} }
public static void beforeCellCreate(CellWriteHandlerContext context) { public static void beforeCellCreate(CellWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext()
if (CollectionUtils.isNotEmpty(handlerList)) { .currentWriteHolder()).getCellHandlerExecutionChain();
for (WriteHandler writeHandler : handlerList) { if (cellHandlerExecutionChain != null) {
((CellWriteHandler)writeHandler).beforeCellCreate(context); cellHandlerExecutionChain.beforeCellCreate(context);
}
} }
} }
public static void afterCellCreate(CellWriteHandlerContext context) { public static void afterCellCreate(CellWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext()
if (CollectionUtils.isNotEmpty(handlerList)) { .currentWriteHolder()).getCellHandlerExecutionChain();
for (WriteHandler writeHandler : handlerList) { if (cellHandlerExecutionChain != null) {
((CellWriteHandler)writeHandler).afterCellCreate(context); cellHandlerExecutionChain.afterCellCreate(context);
}
} }
} }
public static void afterCellDataConverted(CellWriteHandlerContext context) { public static void afterCellDataConverted(CellWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext()
if (CollectionUtils.isNotEmpty(handlerList)) { .currentWriteHolder()).getCellHandlerExecutionChain();
for (WriteHandler writeHandler : handlerList) { if (cellHandlerExecutionChain != null) {
((CellWriteHandler)writeHandler).afterCellDataConverted(context); cellHandlerExecutionChain.afterCellDataConverted(context);
}
} }
} }
public static void afterCellDispose(CellWriteHandlerContext context) { public static void afterCellDispose(CellWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext()
if (CollectionUtils.isNotEmpty(handlerList)) { .currentWriteHolder()).getCellHandlerExecutionChain();
for (WriteHandler writeHandler : handlerList) { if (cellHandlerExecutionChain != null) {
((CellWriteHandler)writeHandler).afterCellDispose(context); cellHandlerExecutionChain.afterCellDispose(context);
}
} }
} }
public static RowWriteHandlerContext createRowWriteHandlerContext(WriteContext writeContext, Integer rowIndex, public static RowWriteHandlerContext createRowWriteHandlerContext(WriteContext writeContext, Integer rowIndex,
Integer relativeRowIndex, Boolean isHead) { Integer relativeRowIndex, Boolean isHead) {
List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get( return new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
RowWriteHandler.class); writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, isHead);
return new RowWriteHandlerContext(writeContext,
writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex,
isHead, handlerList);
} }
public static void beforeRowCreate(RowWriteHandlerContext context) { public static void beforeRowCreate(RowWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext()
if (CollectionUtils.isNotEmpty(handlerList)) { .currentWriteHolder()).getRowHandlerExecutionChain();
for (WriteHandler writeHandler : handlerList) { if (rowHandlerExecutionChain != null) {
((RowWriteHandler)writeHandler).beforeRowCreate(context); rowHandlerExecutionChain.beforeRowCreate(context);
}
} }
} }
public static void afterRowCreate(RowWriteHandlerContext context) { public static void afterRowCreate(RowWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext()
if (CollectionUtils.isNotEmpty(handlerList)) { .currentWriteHolder()).getRowHandlerExecutionChain();
for (WriteHandler writeHandler : handlerList) { if (rowHandlerExecutionChain != null) {
((RowWriteHandler)writeHandler).afterRowCreate(context); rowHandlerExecutionChain.afterRowCreate(context);
}
} }
} }
public static void afterRowDispose(RowWriteHandlerContext context) { public static void afterRowDispose(RowWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList(); RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext()
if (CollectionUtils.isNotEmpty(handlerList)) { .currentWriteHolder()).getRowHandlerExecutionChain();
for (WriteHandler writeHandler : handlerList) { if (rowHandlerExecutionChain != null) {
((RowWriteHandler)writeHandler).afterRowDispose(context); rowHandlerExecutionChain.afterRowDispose(context);
}
} }
} }
private static List<WriteHandler> getHandlerList(WriteContext writeContext, Class<? extends
WriteHandler> clazz,
boolean runOwn) {
Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap;
if (runOwn) {
writeHandlerMap = writeContext.currentWriteHolder().ownWriteHandlerMap();
} else {
writeHandlerMap = writeContext.currentWriteHolder().writeHandlerMap();
}
return writeHandlerMap.get(clazz);
}
} }

6
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -142,6 +142,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
Map<Integer, Field> sortedAllFiledMap) { Map<Integer, Field> sortedAllFiledMap) {
WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
BeanMap beanMap = BeanMapUtils.create(oneRowData); BeanMap beanMap = BeanMapUtils.create(oneRowData);
// Bean the contains of the Map Key method with poor performance,So to create a keySet here
Set<String> beanKeySet = new HashSet<>(beanMap.keySet());
Set<String> beanMapHandledSet = new HashSet<>(); Set<String> beanMapHandledSet = new HashSet<>();
int maxCellIndex = -1; int maxCellIndex = -1;
// If it's a class it needs to be cast by type // If it's a class it needs to be cast by type
@ -151,7 +153,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
int columnIndex = entry.getKey(); int columnIndex = entry.getKey();
Head head = entry.getValue(); Head head = entry.getValue();
String name = head.getFieldName(); String name = head.getFieldName();
if (!beanMap.containsKey(name)) { if (!beanKeySet.contains(name)) {
continue; continue;
} }
@ -188,7 +190,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
for (Map.Entry<Integer, Field> entry : sortedAllFiledMap.entrySet()) { for (Map.Entry<Integer, Field> entry : sortedAllFiledMap.entrySet()) {
Field field = entry.getValue(); Field field = entry.getValue();
String filedName = FieldUtils.resolveCglibFieldName(field); String filedName = FieldUtils.resolveCglibFieldName(field);
boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName) boolean uselessData = !beanKeySet.contains(filedName) || beanMapHandledSet.contains(filedName)
|| ignoreMap.containsKey(filedName); || ignoreMap.containsKey(filedName);
if (uselessData) { if (uselessData) {
continue; continue;

10
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -185,12 +185,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) { if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) {
return; return;
} }
Map<?, ?> dataMap; Map dataMap;
if (oneRowData instanceof Map) { if (oneRowData instanceof Map) {
dataMap = (Map<?, ?>)oneRowData; dataMap = (Map)oneRowData;
} else { } else {
dataMap = BeanMapUtils.create(oneRowData); dataMap = BeanMapUtils.create(oneRowData);
} }
Set<String> dataKeySet = new HashSet<String>(dataMap.keySet());
for (AnalysisCell analysisCell : analysisCellList) { for (AnalysisCell analysisCell : analysisCellList) {
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(
writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(), writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(),
@ -198,7 +200,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
if (analysisCell.getOnlyOneVariable()) { if (analysisCell.getOnlyOneVariable()) {
String variable = analysisCell.getVariableList().get(0); String variable = analysisCell.getVariableList().get(0);
if (!dataMap.containsKey(variable)) { if (!dataKeySet.contains(variable)) {
continue; continue;
} }
Object value = dataMap.get(variable); Object value = dataMap.get(variable);
@ -231,7 +233,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
for (String variable : analysisCell.getVariableList()) { for (String variable : analysisCell.getVariableList()) {
cellValueBuild.append(analysisCell.getPrepareDataList().get(index++)); cellValueBuild.append(analysisCell.getPrepareDataList().get(index++));
if (!dataMap.containsKey(variable)) { if (!dataKeySet.contains(variable)) {
continue; continue;
} }
Object value = dataMap.get(variable); Object value = dataMap.get(variable);

63
src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java

@ -0,0 +1,63 @@
package com.alibaba.excel.write.handler.chain;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import lombok.Data;
/**
* Execute the cell handler chain
*
* @author Jiaju Zhuang
*/
@Data
public class CellHandlerExecutionChain {
/**
* next chain
*/
private CellHandlerExecutionChain next;
/**
* handler
*/
private CellWriteHandler handler;
public CellHandlerExecutionChain(CellWriteHandler handler) {
this.handler = handler;
}
public void beforeCellCreate(CellWriteHandlerContext context) {
this.handler.beforeCellCreate(context);
if (this.next != null) {
this.next.beforeCellCreate(context);
}
}
public void afterCellCreate(CellWriteHandlerContext context) {
this.handler.afterCellCreate(context);
if (this.next != null) {
this.next.afterCellCreate(context);
}
}
public void afterCellDataConverted(CellWriteHandlerContext context) {
this.handler.afterCellDataConverted(context);
if (this.next != null) {
this.next.afterCellDataConverted(context);
}
}
public void afterCellDispose(CellWriteHandlerContext context) {
this.handler.afterCellDispose(context);
if (this.next != null) {
this.next.afterCellDispose(context);
}
}
public void addLast(CellWriteHandler handler) {
CellHandlerExecutionChain context = this;
while (context.next != null) {
context = context.next;
}
context.next = new CellHandlerExecutionChain(handler);
}
}

56
src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java

@ -0,0 +1,56 @@
package com.alibaba.excel.write.handler.chain;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import lombok.Data;
/**
* Execute the row handler chain
*
* @author Jiaju Zhuang
*/
@Data
public class RowHandlerExecutionChain {
/**
* next chain
*/
private RowHandlerExecutionChain next;
/**
* handler
*/
private RowWriteHandler handler;
public RowHandlerExecutionChain(RowWriteHandler handler) {
this.handler = handler;
}
public void beforeRowCreate(RowWriteHandlerContext context) {
this.handler.beforeRowCreate(context);
if (this.next != null) {
this.next.beforeRowCreate(context);
}
}
public void afterRowCreate(RowWriteHandlerContext context) {
this.handler.afterRowCreate(context);
if (this.next != null) {
this.next.afterRowCreate(context);
}
}
public void afterRowDispose(RowWriteHandlerContext context) {
this.handler.afterRowDispose(context);
if (this.next != null) {
this.next.afterRowDispose(context);
}
}
public void addLast(RowWriteHandler handler) {
RowHandlerExecutionChain context = this;
while (context.next != null) {
context = context.next;
}
context.next = new RowHandlerExecutionChain(handler);
}
}

48
src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java

@ -0,0 +1,48 @@
package com.alibaba.excel.write.handler.chain;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import lombok.Data;
/**
* Execute the sheet handler chain
*
* @author Jiaju Zhuang
*/
@Data
public class SheetHandlerExecutionChain {
/**
* next chain
*/
private SheetHandlerExecutionChain next;
/**
* handler
*/
private SheetWriteHandler handler;
public SheetHandlerExecutionChain(SheetWriteHandler handler) {
this.handler = handler;
}
public void beforeSheetCreate(SheetWriteHandlerContext context) {
this.handler.beforeSheetCreate(context);
if (this.next != null) {
this.next.beforeSheetCreate(context);
}
}
public void afterSheetCreate(SheetWriteHandlerContext context) {
this.handler.afterSheetCreate(context);
if (this.next != null) {
this.next.afterSheetCreate(context);
}
}
public void addLast(SheetWriteHandler handler) {
SheetHandlerExecutionChain context = this;
while (context.next != null) {
context = context.next;
}
context.next = new SheetHandlerExecutionChain(handler);
}
}

57
src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java

@ -0,0 +1,57 @@
package com.alibaba.excel.write.handler.chain;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import lombok.Data;
/**
* Execute the workbook handler chain
*
* @author Jiaju Zhuang
*/
@Data
public class WorkbookHandlerExecutionChain {
/**
* next chain
*/
private WorkbookHandlerExecutionChain next;
/**
* handler
*/
private WorkbookWriteHandler handler;
public WorkbookHandlerExecutionChain(WorkbookWriteHandler handler) {
this.handler = handler;
}
public void beforeWorkbookCreate(WorkbookWriteHandlerContext context) {
this.handler.beforeWorkbookCreate(context);
if (this.next != null) {
this.next.beforeWorkbookCreate(context);
}
}
public void afterWorkbookCreate(WorkbookWriteHandlerContext context) {
this.handler.afterWorkbookCreate(context);
if (this.next != null) {
this.next.afterWorkbookCreate(context);
}
}
public void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
this.handler.afterWorkbookDispose(context);
if (this.next != null) {
this.next.afterWorkbookDispose(context);
}
}
public void addLast(WorkbookWriteHandler handler) {
WorkbookHandlerExecutionChain context = this;
while (context.next != null) {
context = context.next;
}
context.next = new WorkbookHandlerExecutionChain(handler);
}
}

9
src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java

@ -7,7 +7,6 @@ import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler; import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
@ -106,16 +105,11 @@ public class CellWriteHandlerContext {
*/ */
private Boolean ignoreFillStyle; private Boolean ignoreFillStyle;
/**
* handler
*/
private List<WriteHandler> handlerList;
public CellWriteHandlerContext(WriteContext writeContext, public CellWriteHandlerContext(WriteContext writeContext,
WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder, WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Cell cell, Integer columnIndex, WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Cell cell, Integer columnIndex,
Integer relativeRowIndex, Head headData, List<WriteCellData<?>> cellDataList, WriteCellData<?> firstCellData, Integer relativeRowIndex, Head headData, List<WriteCellData<?>> cellDataList, WriteCellData<?> firstCellData,
Boolean head, ExcelContentProperty excelContentProperty, List<WriteHandler> handlerList) { Boolean head, ExcelContentProperty excelContentProperty) {
this.writeContext = writeContext; this.writeContext = writeContext;
this.writeWorkbookHolder = writeWorkbookHolder; this.writeWorkbookHolder = writeWorkbookHolder;
this.writeSheetHolder = writeSheetHolder; this.writeSheetHolder = writeSheetHolder;
@ -130,6 +124,5 @@ public class CellWriteHandlerContext {
this.firstCellData = firstCellData; this.firstCellData = firstCellData;
this.head = head; this.head = head;
this.excelContentProperty = excelContentProperty; this.excelContentProperty = excelContentProperty;
this.handlerList = handlerList;
} }
} }

8
src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java

@ -1,9 +1,6 @@
package com.alibaba.excel.write.handler.context; package com.alibaba.excel.write.handler.context;
import java.util.List;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
@ -52,9 +49,4 @@ public class RowWriteHandlerContext {
* Nullable.It is null in the case of fill data. * Nullable.It is null in the case of fill data.
*/ */
private Boolean head; private Boolean head;
/**
* handler
*/
private List<WriteHandler> handlerList;
} }

12
src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java

@ -1,9 +1,6 @@
package com.alibaba.excel.write.handler.context; package com.alibaba.excel.write.handler.context;
import java.util.List;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
@ -30,13 +27,4 @@ public class SheetWriteHandlerContext {
* sheet * sheet
*/ */
private WriteSheetHolder writeSheetHolder; private WriteSheetHolder writeSheetHolder;
/**
* handler
*/
private List<WriteHandler> handlerList;
/**
* handler
*/
private List<WriteHandler> ownHandlerList;
} }

13
src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java

@ -1,9 +1,6 @@
package com.alibaba.excel.write.handler.context; package com.alibaba.excel.write.handler.context;
import java.util.List;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -25,14 +22,4 @@ public class WorkbookWriteHandlerContext {
* workbook * workbook
*/ */
private WriteWorkbookHolder writeWorkbookHolder; private WriteWorkbookHolder writeWorkbookHolder;
/**
* handler
*/
private List<WriteHandler> handlerList;
/**
* handler
*/
private List<WriteHandler> ownHandlerList;
} }

173
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -15,7 +15,6 @@ import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.converters.DefaultConverterLoader;
import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.event.NotRepeatExecutor;
import com.alibaba.excel.event.Order;
import com.alibaba.excel.metadata.AbstractHolder; import com.alibaba.excel.metadata.AbstractHolder;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
@ -28,6 +27,10 @@ import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.handler.chain.CellHandlerExecutionChain;
import com.alibaba.excel.write.handler.chain.RowHandlerExecutionChain;
import com.alibaba.excel.write.handler.chain.SheetHandlerExecutionChain;
import com.alibaba.excel.write.handler.chain.WorkbookHandlerExecutionChain;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
@ -62,15 +65,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
* Excel head property * Excel head property
*/ */
private ExcelWriteHeadProperty excelWriteHeadProperty; private ExcelWriteHeadProperty excelWriteHeadProperty;
/**
* Write handler
*/
private Map<Class<? extends WriteHandler>, List<WriteHandler>> 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<Class<? extends WriteHandler>, List<WriteHandler>> ownWriteHandlerMap;
/** /**
* Use the default style.Default is true. * Use the default style.Default is true.
*/ */
@ -97,6 +91,43 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
*/ */
private Collection<String> includeColumnFieldNames; private Collection<String> includeColumnFieldNames;
/**
* Write handler
*/
private List<WriteHandler> writeHandlerList;
/**
* Execute the workbook handler chain
* 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.
*/
public WorkbookHandlerExecutionChain ownWorkbookHandlerExecutionChain;
/**
* Execute the sheet handler chain
* 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.
*/
public SheetHandlerExecutionChain ownSheetHandlerExecutionChain;
/**
* Execute the workbook handler chain
*/
public WorkbookHandlerExecutionChain workbookHandlerExecutionChain;
/**
* Execute the sheet handler chain
*/
public SheetHandlerExecutionChain sheetHandlerExecutionChain;
/**
* Execute the row handler chain
*/
public RowHandlerExecutionChain rowHandlerExecutionChain;
/**
* Execute the cell handler chain
*/
public CellHandlerExecutionChain cellHandlerExecutionChain;
public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder) { public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder) {
super(writeBasicParameter, parentAbstractWriteHolder); super(writeBasicParameter, parentAbstractWriteHolder);
@ -169,7 +200,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead()); this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead());
// Set writeHandlerMap // Set writeHandlerMap
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); List<WriteHandler> handlerList = new ArrayList<>();
// Initialization Annotation // Initialization Annotation
initAnnotationConfig(handlerList, writeBasicParameter); initAnnotationConfig(handlerList, writeBasicParameter);
@ -178,16 +209,16 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
&& !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) { && !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) {
handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList()); handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList());
} }
sortAndClearUpHandler(handlerList, true);
this.ownWriteHandlerMap = sortAndClearUpHandler(handlerList);
Map<Class<? extends WriteHandler>, List<WriteHandler>> parentWriteHandlerMap = null;
if (parentAbstractWriteHolder != null) { if (parentAbstractWriteHolder != null) {
parentWriteHandlerMap = parentAbstractWriteHolder.getWriteHandlerMap(); if (CollectionUtils.isNotEmpty(parentAbstractWriteHolder.getWriteHandlerList())) {
handlerList.addAll(parentAbstractWriteHolder.getWriteHandlerList());
}
} else { } else {
handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle)); handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle));
} }
this.writeHandlerMap = sortAndClearUpAllHandler(handlerList, parentWriteHandlerMap); sortAndClearUpHandler(handlerList, false);
// Set converterMap // Set converterMap
if (parentAbstractWriteHolder == null) { if (parentAbstractWriteHolder == null) {
@ -310,38 +341,22 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
handlerList.add(columnWidthStyleStrategy); handlerList.add(columnWidthStyleStrategy);
} }
protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpAllHandler( protected void sortAndClearUpHandler(List<WriteHandler> handlerList, boolean runOwn) {
List<WriteHandler> handlerList, Map<Class<? extends WriteHandler>, List<WriteHandler>> parentHandlerMap) {
// add
if (parentHandlerMap != null) {
List<WriteHandler> parentWriteHandler = parentHandlerMap.get(WriteHandler.class);
if (!CollectionUtils.isEmpty(parentWriteHandler)) {
handlerList.addAll(parentWriteHandler);
}
}
return sortAndClearUpHandler(handlerList);
}
protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpHandler(
List<WriteHandler> handlerList) {
// sort // sort
Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = new TreeMap<Integer, List<WriteHandler>>(); Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = new TreeMap<>();
for (WriteHandler handler : handlerList) { for (WriteHandler handler : handlerList) {
int order = Integer.MIN_VALUE; int order = handler.order();
if (handler instanceof Order) {
order = ((Order)handler).order();
}
if (orderExcelWriteHandlerMap.containsKey(order)) { if (orderExcelWriteHandlerMap.containsKey(order)) {
orderExcelWriteHandlerMap.get(order).add(handler); orderExcelWriteHandlerMap.get(order).add(handler);
} else { } else {
List<WriteHandler> tempHandlerList = new ArrayList<WriteHandler>(); List<WriteHandler> tempHandlerList = new ArrayList<>();
tempHandlerList.add(handler); tempHandlerList.add(handler);
orderExcelWriteHandlerMap.put(order, tempHandlerList); orderExcelWriteHandlerMap.put(order, tempHandlerList);
} }
} }
// clean up // clean up
Set<String> alreadyExistedHandlerSet = new HashSet<String>(); Set<String> alreadyExistedHandlerSet = new HashSet<>();
List<WriteHandler> cleanUpHandlerList = new ArrayList<WriteHandler>(); List<WriteHandler> cleanUpHandlerList = new ArrayList<>();
for (Map.Entry<Integer, List<WriteHandler>> entry : orderExcelWriteHandlerMap.entrySet()) { for (Map.Entry<Integer, List<WriteHandler>> entry : orderExcelWriteHandlerMap.entrySet()) {
for (WriteHandler handler : entry.getValue()) { for (WriteHandler handler : entry.getValue()) {
if (handler instanceof NotRepeatExecutor) { if (handler instanceof NotRepeatExecutor) {
@ -354,30 +369,74 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
cleanUpHandlerList.add(handler); cleanUpHandlerList.add(handler);
} }
} }
// classify
Map<Class<? extends WriteHandler>, List<WriteHandler>> result = // build chain
new HashMap<Class<? extends WriteHandler>, List<WriteHandler>>(16); if (!runOwn) {
result.put(WriteHandler.class, new ArrayList<WriteHandler>()); this.writeHandlerList = new ArrayList<>();
result.put(WorkbookWriteHandler.class, new ArrayList<WriteHandler>()); }
result.put(SheetWriteHandler.class, new ArrayList<WriteHandler>());
result.put(RowWriteHandler.class, new ArrayList<WriteHandler>());
result.put(CellWriteHandler.class, new ArrayList<WriteHandler>());
for (WriteHandler writeHandler : cleanUpHandlerList) { for (WriteHandler writeHandler : cleanUpHandlerList) {
buildChain(writeHandler, runOwn);
}
}
protected void buildChain(WriteHandler writeHandler, boolean runOwn) {
if (writeHandler instanceof CellWriteHandler) { if (writeHandler instanceof CellWriteHandler) {
result.get(CellWriteHandler.class).add(writeHandler); if (!runOwn) {
if (cellHandlerExecutionChain == null) {
cellHandlerExecutionChain = new CellHandlerExecutionChain((CellWriteHandler)writeHandler);
} else {
cellHandlerExecutionChain.addLast((CellWriteHandler)writeHandler);
}
this.writeHandlerList.add(writeHandler);
}
return;
} }
if (writeHandler instanceof RowWriteHandler) { if (writeHandler instanceof RowWriteHandler) {
result.get(RowWriteHandler.class).add(writeHandler); if (!runOwn) {
if (rowHandlerExecutionChain == null) {
rowHandlerExecutionChain = new RowHandlerExecutionChain((RowWriteHandler)writeHandler);
} else {
rowHandlerExecutionChain.addLast((RowWriteHandler)writeHandler);
}
this.writeHandlerList.add(writeHandler);
}
return;
} }
if (writeHandler instanceof SheetWriteHandler) { if (writeHandler instanceof SheetWriteHandler) {
result.get(SheetWriteHandler.class).add(writeHandler); if (!runOwn) {
if (sheetHandlerExecutionChain == null) {
sheetHandlerExecutionChain = new SheetHandlerExecutionChain((SheetWriteHandler)writeHandler);
} else {
sheetHandlerExecutionChain.addLast((SheetWriteHandler)writeHandler);
}
this.writeHandlerList.add(writeHandler);
} else {
if (ownSheetHandlerExecutionChain == null) {
ownSheetHandlerExecutionChain = new SheetHandlerExecutionChain((SheetWriteHandler)writeHandler);
} else {
ownSheetHandlerExecutionChain.addLast((SheetWriteHandler)writeHandler);
}
}
return;
} }
if (writeHandler instanceof WorkbookWriteHandler) { if (writeHandler instanceof WorkbookWriteHandler) {
result.get(WorkbookWriteHandler.class).add(writeHandler); if (!runOwn) {
if (workbookHandlerExecutionChain == null) {
workbookHandlerExecutionChain = new WorkbookHandlerExecutionChain(
(WorkbookWriteHandler)writeHandler);
} else {
workbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler);
}
this.writeHandlerList.add(writeHandler);
} else {
if (ownWorkbookHandlerExecutionChain == null) {
ownWorkbookHandlerExecutionChain = new WorkbookHandlerExecutionChain(
(WorkbookWriteHandler)writeHandler);
} else {
ownWorkbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler);
}
} }
result.get(WriteHandler.class).add(writeHandler);
} }
return result;
} }
@Override @Override
@ -406,16 +465,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
return getExcelWriteHeadProperty(); return getExcelWriteHeadProperty();
} }
@Override
public Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap() {
return getWriteHandlerMap();
}
@Override
public Map<Class<? extends WriteHandler>, List<WriteHandler>> ownWriteHandlerMap() {
return getOwnWriteHandlerMap();
}
@Override @Override
public boolean needHead() { public boolean needHead() {
return getNeedHead(); return getNeedHead();

18
src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java

@ -1,10 +1,6 @@
package com.alibaba.excel.write.metadata.holder; package com.alibaba.excel.write.metadata.holder;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.metadata.ConfigurationHolder; import com.alibaba.excel.metadata.ConfigurationHolder;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.property.ExcelWriteHeadProperty; import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
/** /**
@ -21,20 +17,6 @@ public interface WriteHolder extends ConfigurationHolder {
*/ */
ExcelWriteHeadProperty excelWriteHeadProperty(); ExcelWriteHeadProperty excelWriteHeadProperty();
/**
* What handler does the currently operated cell need to execute
*
* @return
*/
Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap();
/**
* create your own write handler.
*
* @return
*/
Map<Class<? extends WriteHandler>, List<WriteHandler>> ownWriteHandlerMap();
/** /**
* Is to determine if a field needs to be ignored * Is to determine if a field needs to be ignored
* *

11
src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

@ -259,8 +259,6 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
if (writeCellStyle == null) { if (writeCellStyle == null) {
return originCellStyle; return originCellStyle;
} }
WriteCellStyle tempWriteCellStyle = new WriteCellStyle();
WriteCellStyle.merge(writeCellStyle, tempWriteCellStyle);
short styleIndex = -1; short styleIndex = -1;
Font originFont = null; Font originFont = null;
@ -277,14 +275,17 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
Map<WriteCellStyle, CellStyle> cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex, Map<WriteCellStyle, CellStyle> cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex,
key -> MapUtils.newHashMap()); key -> MapUtils.newHashMap());
CellStyle cellStyle = cellStyleMap.get(tempWriteCellStyle); CellStyle cellStyle = cellStyleMap.get(writeCellStyle);
if (cellStyle != null) { if (cellStyle != null) {
return cellStyle; return cellStyle;
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.info("create new style:{},{}", tempWriteCellStyle, originCellStyle); log.info("create new style:{},{}", writeCellStyle, originCellStyle);
} }
cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle); WriteCellStyle tempWriteCellStyle = new WriteCellStyle();
WriteCellStyle.merge(writeCellStyle, tempWriteCellStyle);
cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, tempWriteCellStyle);
Short dataFormat = createDataFormat(tempWriteCellStyle.getDataFormatData(), useCache); Short dataFormat = createDataFormat(tempWriteCellStyle.getDataFormatData(), useCache);
if (dataFormat != null) { if (dataFormat != null) {
cellStyle.setDataFormat(dataFormat); cellStyle.setDataFormat(dataFormat);

17
src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -8,6 +8,7 @@ import java.util.List;
import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
@ -147,4 +148,20 @@ public class Lock2Test {
Thread.sleep(500 * 1000); Thread.sleep(500 * 1000);
} }
@Test
public void test335() throws Exception {
LOGGER.info("reslut:{}", PositionUtils.getCol("A10",null));
LOGGER.info("reslut:{}", PositionUtils.getRow("A10"));
LOGGER.info("reslut:{}", PositionUtils.getCol("AB10",null));
LOGGER.info("reslut:{}", PositionUtils.getRow("AB10"));
//LOGGER.info("reslut:{}", PositionUtils2.getCol("A10",null));
//LOGGER.info("reslut:{}", PositionUtils2.getRow("A10"));
//LOGGER.info("reslut:{}", PositionUtils2.getCol("AB10",null));
//LOGGER.info("reslut:{}", PositionUtils2.getRow("AB10"));
}
} }

Loading…
Cancel
Save