Skip to content

Commit

Permalink
修复tablesheet中创建的拦截器不执行workbook事件的bug #1202
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Apr 24, 2020
1 parent 68f5303 commit 29a36b1
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 39 deletions.
23 changes: 19 additions & 4 deletions src/main/java/com/alibaba/excel/context/WriteContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ public void currentSheet(WriteSheet writeSheet, WriteTypeEnum writeType) {
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);
}
Expand Down Expand Up @@ -201,8 +208,8 @@ public void initHead(ExcelWriteHeadProperty excelWriteHeadProperty) {
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);
Expand All @@ -227,7 +234,7 @@ private void addOneRowOfHeadDataToExcel(Row row, Map<Integer, Head> headMap, int
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);
}
}

Expand All @@ -251,7 +258,15 @@ public void currentTable(WriteTable writeTable) {
}
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());
}

Expand Down Expand Up @@ -322,7 +337,7 @@ public void finish(boolean onException) {
try {
Workbook workbook = writeWorkbookHolder.getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
((SXSSFWorkbook)workbook).dispose();
((SXSSFWorkbook) workbook).dispose();
}
} catch (Throwable t) {
throwable = t;
Expand Down
66 changes: 47 additions & 19 deletions src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;
Expand All @@ -24,66 +25,82 @@ public class WriteHandlerUtils {

private WriteHandlerUtils() {}


public static void beforeWorkbookCreate(WriteContext writeContext) {
List<WriteHandler> handlerList =
writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
beforeWorkbookCreate(writeContext, false);
}

public static void beforeWorkbookCreate(WriteContext writeContext, boolean runOwn) {
List<WriteHandler> 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<WriteHandler> handlerList =
writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class);
afterWorkbookCreate(writeContext, false);
}

public static void afterWorkbookCreate(WriteContext writeContext, boolean runOwn) {
List<WriteHandler> 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<WriteHandler> 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<WriteHandler> handlerList = writeContext.writeSheetHolder().writeHandlerMap().get(SheetWriteHandler.class);
beforeSheetCreate(writeContext, false);
}

public static void beforeSheetCreate(WriteContext writeContext, boolean runOwn) {
List<WriteHandler> 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<WriteHandler> handlerList = writeContext.writeSheetHolder().writeHandlerMap().get(SheetWriteHandler.class);
afterSheetCreate(writeContext, false);
}

public static void afterSheetCreate(WriteContext writeContext, boolean runOwn) {
List<WriteHandler> 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());
}
}
Expand All @@ -102,7 +119,7 @@ public static void beforeCellCreate(WriteContext writeContext, Row row, Head hea
}
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);
}
}
Expand All @@ -117,7 +134,7 @@ public static void afterCellCreate(WriteContext writeContext, Cell cell, Head he
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellCreate(writeContext.writeSheetHolder(),
((CellWriteHandler) writeHandler).afterCellCreate(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), cell, head, relativeRowIndex, isHead);
}
}
Expand All @@ -132,7 +149,7 @@ public static void afterCellDataConverted(WriteContext writeContext, CellData ce
}
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);
}
}
Expand All @@ -156,7 +173,7 @@ public static void afterCellDispose(WriteContext writeContext, List<CellData> ce
}
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);
}
}
Expand All @@ -173,7 +190,7 @@ public static void beforeRowCreate(WriteContext writeContext, Integer rowIndex,
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).beforeRowCreate(writeContext.writeSheetHolder(),
((RowWriteHandler) writeHandler).beforeRowCreate(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), rowIndex, relativeRowIndex, isHead);
}
}
Expand All @@ -186,7 +203,7 @@ public static void afterRowCreate(WriteContext writeContext, Row row, Integer re
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).afterRowCreate(writeContext.writeSheetHolder(),
((RowWriteHandler) writeHandler).afterRowCreate(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), row, relativeRowIndex, isHead);
}
}
Expand All @@ -200,12 +217,23 @@ public static void afterRowDispose(WriteContext writeContext, Row row, Integer r
}
for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).afterRowDispose(writeContext.writeSheetHolder(),
((RowWriteHandler) writeHandler).afterRowDispose(writeContext.writeSheetHolder(),
writeContext.writeTableHolder(), row, relativeRowIndex, isHead);
}
}
if (null != writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler()) {
writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler().row(row.getRowNum(), row);
}
}

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(WorkbookWriteHandler.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
*/
private ExcelWriteHeadProperty excelWriteHeadProperty;
/**
* Write handler for workbook
* 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.
*/
Expand Down Expand Up @@ -177,21 +182,22 @@ public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWrit
List<WriteHandler> handlerList = new ArrayList<WriteHandler>();

// Initialization Annotation
initAnnotationConfig(handlerList);
initAnnotationConfig(handlerList, writeBasicParameter);

if (writeBasicParameter.getCustomWriteHandlerList() != null
&& !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) {
handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList());
}

this.ownWriteHandlerMap = sortAndClearUpHandler(handlerList);

Map<Class<? extends WriteHandler>, List<WriteHandler>> 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) {
Expand All @@ -215,13 +221,13 @@ private void compatibleOldCode(WriteBasicParameter writeBasicParameter) {
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:
}
Expand Down Expand Up @@ -279,10 +285,13 @@ protected Integer columnWidth(Head head, Integer columnIndex) {
});
}

protected void initAnnotationConfig(List<WriteHandler> handlerList) {
protected void initAnnotationConfig(List<WriteHandler> handlerList, WriteBasicParameter writeBasicParameter) {
if (!HeadKindEnum.CLASS.equals(getExcelWriteHeadProperty().getHeadKind())) {
return;
}
if (writeBasicParameter.getClazz() == null) {
return;
}
Map<Integer, Head> headMap = getExcelWriteHeadProperty().getHeadMap();
boolean hasColumnWidth = false;
boolean hasStyle = false;
Expand All @@ -308,7 +317,6 @@ protected void initAnnotationConfig(List<WriteHandler> handlerList) {

dealRowHigh(handlerList);
dealOnceAbsoluteMerge(handlerList);

}

private void dealStyle(List<WriteHandler> handlerList) {
Expand Down Expand Up @@ -376,23 +384,27 @@ protected Integer columnWidth(Head head, Integer columnIndex) {
handlerList.add(columnWidthStyleStrategy);
}

protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpHandler(
List<WriteHandler> handlerList, Map<Class<? extends WriteHandler>, List<WriteHandler>> parentHandlerMap) {

protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpAllHandler(
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
Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = new TreeMap<Integer, List<WriteHandler>>();
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);
Expand All @@ -408,7 +420,7 @@ protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpH
for (Map.Entry<Integer, List<WriteHandler>> 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;
}
Expand Down Expand Up @@ -480,6 +492,15 @@ public void setWriteHandlerMap(Map<Class<? extends WriteHandler>, List<WriteHand
this.writeHandlerMap = writeHandlerMap;
}

public Map<Class<? extends WriteHandler>, List<WriteHandler>> getOwnWriteHandlerMap() {
return ownWriteHandlerMap;
}

public void setOwnWriteHandlerMap(
Map<Class<? extends WriteHandler>, List<WriteHandler>> ownWriteHandlerMap) {
this.ownWriteHandlerMap = ownWriteHandlerMap;
}

public ExcelWriteHeadProperty getExcelWriteHeadProperty() {
return excelWriteHeadProperty;
}
Expand Down Expand Up @@ -554,6 +575,11 @@ public Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap()
return getWriteHandlerMap();
}

@Override
public Map<Class<? extends WriteHandler>, List<WriteHandler>> ownWriteHandlerMap() {
return getOwnWriteHandlerMap();
}

@Override
public boolean needHead() {
return getNeedHead();
Expand Down
Loading

0 comments on commit 29a36b1

Please sign in to comment.