|
|
@ -2,6 +2,7 @@ package com.alibaba.excel.util; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
|
import org.apache.poi.ss.usermodel.Row; |
|
|
|
import org.apache.poi.ss.usermodel.Row; |
|
|
@ -24,66 +25,82 @@ public class WriteHandlerUtils { |
|
|
|
|
|
|
|
|
|
|
|
private WriteHandlerUtils() {} |
|
|
|
private WriteHandlerUtils() {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void beforeWorkbookCreate(WriteContext writeContext) { |
|
|
|
public static void beforeWorkbookCreate(WriteContext writeContext) { |
|
|
|
List<WriteHandler> handlerList = |
|
|
|
beforeWorkbookCreate(writeContext, false); |
|
|
|
writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void beforeWorkbookCreate(WriteContext writeContext, boolean runOwn) { |
|
|
|
|
|
|
|
List<WriteHandler> handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof WorkbookWriteHandler) { |
|
|
|
if (writeHandler instanceof WorkbookWriteHandler) { |
|
|
|
((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(); |
|
|
|
((WorkbookWriteHandler) writeHandler).beforeWorkbookCreate(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void afterWorkbookCreate(WriteContext writeContext) { |
|
|
|
public static void afterWorkbookCreate(WriteContext writeContext) { |
|
|
|
List<WriteHandler> handlerList = |
|
|
|
afterWorkbookCreate(writeContext, false); |
|
|
|
writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void afterWorkbookCreate(WriteContext writeContext, boolean runOwn) { |
|
|
|
|
|
|
|
List<WriteHandler> handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof WorkbookWriteHandler) { |
|
|
|
if (writeHandler instanceof WorkbookWriteHandler) { |
|
|
|
((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(writeContext.writeWorkbookHolder()); |
|
|
|
((WorkbookWriteHandler) writeHandler).afterWorkbookCreate(writeContext.writeWorkbookHolder()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void afterWorkbookDispose(WriteContext writeContext) { |
|
|
|
public static void afterWorkbookDispose(WriteContext writeContext) { |
|
|
|
List<WriteHandler> handlerList = |
|
|
|
List<WriteHandler> handlerList = |
|
|
|
writeContext.writeWorkbookHolder().writeHandlerMap().get(WorkbookWriteHandler.class); |
|
|
|
writeContext.currentWriteHolder().writeHandlerMap().get(WorkbookWriteHandler.class); |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof WorkbookWriteHandler) { |
|
|
|
if (writeHandler instanceof WorkbookWriteHandler) { |
|
|
|
((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(writeContext.writeWorkbookHolder()); |
|
|
|
((WorkbookWriteHandler) writeHandler).afterWorkbookDispose(writeContext.writeWorkbookHolder()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void beforeSheetCreate(WriteContext writeContext) { |
|
|
|
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()) { |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof SheetWriteHandler) { |
|
|
|
if (writeHandler instanceof SheetWriteHandler) { |
|
|
|
((SheetWriteHandler)writeHandler).beforeSheetCreate(writeContext.writeWorkbookHolder(), |
|
|
|
((SheetWriteHandler) writeHandler).beforeSheetCreate(writeContext.writeWorkbookHolder(), |
|
|
|
writeContext.writeSheetHolder()); |
|
|
|
writeContext.writeSheetHolder()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void afterSheetCreate(WriteContext writeContext) { |
|
|
|
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()) { |
|
|
|
if (handlerList == null || handlerList.isEmpty()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof SheetWriteHandler) { |
|
|
|
if (writeHandler instanceof SheetWriteHandler) { |
|
|
|
((SheetWriteHandler)writeHandler).afterSheetCreate(writeContext.writeWorkbookHolder(), |
|
|
|
((SheetWriteHandler) writeHandler).afterSheetCreate(writeContext.writeWorkbookHolder(), |
|
|
|
writeContext.writeSheetHolder()); |
|
|
|
writeContext.writeSheetHolder()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -102,7 +119,7 @@ public class WriteHandlerUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
((CellWriteHandler)writeHandler).beforeCellCreate(writeContext.writeSheetHolder(), |
|
|
|
((CellWriteHandler) writeHandler).beforeCellCreate(writeContext.writeSheetHolder(), |
|
|
|
writeContext.writeTableHolder(), row, head, columnIndex, relativeRowIndex, isHead); |
|
|
|
writeContext.writeTableHolder(), row, head, columnIndex, relativeRowIndex, isHead); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -117,7 +134,7 @@ public class WriteHandlerUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
((CellWriteHandler)writeHandler).afterCellCreate(writeContext.writeSheetHolder(), |
|
|
|
((CellWriteHandler) writeHandler).afterCellCreate(writeContext.writeSheetHolder(), |
|
|
|
writeContext.writeTableHolder(), cell, head, relativeRowIndex, isHead); |
|
|
|
writeContext.writeTableHolder(), cell, head, relativeRowIndex, isHead); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -132,7 +149,7 @@ public class WriteHandlerUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
((CellWriteHandler)writeHandler).afterCellDataConverted(writeContext.writeSheetHolder(), |
|
|
|
((CellWriteHandler) writeHandler).afterCellDataConverted(writeContext.writeSheetHolder(), |
|
|
|
writeContext.writeTableHolder(), cellData, cell, head, relativeRowIndex, isHead); |
|
|
|
writeContext.writeTableHolder(), cellData, cell, head, relativeRowIndex, isHead); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -156,7 +173,7 @@ public class WriteHandlerUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
if (writeHandler instanceof CellWriteHandler) { |
|
|
|
((CellWriteHandler)writeHandler).afterCellDispose(writeContext.writeSheetHolder(), |
|
|
|
((CellWriteHandler) writeHandler).afterCellDispose(writeContext.writeSheetHolder(), |
|
|
|
writeContext.writeTableHolder(), cellDataList, cell, head, relativeRowIndex, isHead); |
|
|
|
writeContext.writeTableHolder(), cellDataList, cell, head, relativeRowIndex, isHead); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -173,7 +190,7 @@ public class WriteHandlerUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof RowWriteHandler) { |
|
|
|
if (writeHandler instanceof RowWriteHandler) { |
|
|
|
((RowWriteHandler)writeHandler).beforeRowCreate(writeContext.writeSheetHolder(), |
|
|
|
((RowWriteHandler) writeHandler).beforeRowCreate(writeContext.writeSheetHolder(), |
|
|
|
writeContext.writeTableHolder(), rowIndex, relativeRowIndex, isHead); |
|
|
|
writeContext.writeTableHolder(), rowIndex, relativeRowIndex, isHead); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -186,7 +203,7 @@ public class WriteHandlerUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof RowWriteHandler) { |
|
|
|
if (writeHandler instanceof RowWriteHandler) { |
|
|
|
((RowWriteHandler)writeHandler).afterRowCreate(writeContext.writeSheetHolder(), |
|
|
|
((RowWriteHandler) writeHandler).afterRowCreate(writeContext.writeSheetHolder(), |
|
|
|
writeContext.writeTableHolder(), row, relativeRowIndex, isHead); |
|
|
|
writeContext.writeTableHolder(), row, relativeRowIndex, isHead); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -200,7 +217,7 @@ public class WriteHandlerUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
for (WriteHandler writeHandler : handlerList) { |
|
|
|
if (writeHandler instanceof RowWriteHandler) { |
|
|
|
if (writeHandler instanceof RowWriteHandler) { |
|
|
|
((RowWriteHandler)writeHandler).afterRowDispose(writeContext.writeSheetHolder(), |
|
|
|
((RowWriteHandler) writeHandler).afterRowDispose(writeContext.writeSheetHolder(), |
|
|
|
writeContext.writeTableHolder(), row, relativeRowIndex, isHead); |
|
|
|
writeContext.writeTableHolder(), row, relativeRowIndex, isHead); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -208,4 +225,15 @@ public class WriteHandlerUtils { |
|
|
|
writeContext.writeWorkbookHolder().getWriteWorkbook().getWriteHandler().row(row.getRowNum(), row); |
|
|
|
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); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|