diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 1c3727bf..7ee3b2c9 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -15,10 +15,15 @@ import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.FileUtils; +import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.NumberDataFormatterUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; +import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteWorkbook; @@ -83,13 +88,16 @@ public class WriteContextImpl implements WriteContext { LOGGER.debug("Begin to Initialization 'WriteContextImpl'"); } initCurrentWorkbookHolder(writeWorkbook); - WriteHandlerUtils.beforeWorkbookCreate(this); + + WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( + this); + WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext); try { WorkBookUtil.createWorkBook(writeWorkbookHolder); } catch (Exception e) { throw new ExcelGenerateException("Create workbook failure", e); } - WriteHandlerUtils.afterWorkbookCreate(this); + WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Initialization 'WriteContextImpl' complete"); } @@ -118,8 +126,10 @@ public class WriteContextImpl implements WriteContext { initCurrentSheetHolder(writeSheet); // Workbook handler need to supplementary execution - WriteHandlerUtils.beforeWorkbookCreate(this, true); - WriteHandlerUtils.afterWorkbookCreate(this, true); + WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( + this, true); + WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); + WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); // Initialization current sheet initSheet(writeType); @@ -162,7 +172,8 @@ public class WriteContextImpl implements WriteContext { } private void initSheet(WriteTypeEnum writeType) { - WriteHandlerUtils.beforeSheetCreate(this); + SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this); + WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext); Sheet currentSheet; try { if (writeSheetHolder.getSheetNo() != null) { @@ -192,7 +203,7 @@ public class WriteContextImpl implements WriteContext { currentSheet = createSheet(); } writeSheetHolder.setSheet(currentSheet); - WriteHandlerUtils.afterSheetCreate(this); + WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext); if (WriteTypeEnum.ADD.equals(writeType)) { // Initialization head initHead(writeSheetHolder.excelWriteHeadProperty()); @@ -226,11 +237,17 @@ public class WriteContextImpl implements WriteContext { } for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber() + newRowIndex; i++, relativeRowIndex++) { - WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE); + + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(this, + newRowIndex, relativeRowIndex, Boolean.TRUE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i); - WriteHandlerUtils.afterRowCreate(this, row, i, relativeRowIndex, Boolean.TRUE); - addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex); - WriteHandlerUtils.afterRowDispose(this, row, i, relativeRowIndex, Boolean.TRUE); + rowWriteHandlerContext.setRow(row); + + WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext); + addOneRowOfHeadDataToExcel(row, i, excelWriteHeadProperty.getHeadMap(), relativeRowIndex); + WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext); } } @@ -242,26 +259,29 @@ public class WriteContextImpl implements WriteContext { } } - private void addOneRowOfHeadDataToExcel(Row row, Map headMap, int relativeRowIndex) { + private void addOneRowOfHeadDataToExcel(Row row, Integer rowIndex, Map headMap, + int relativeRowIndex) { for (Map.Entry entry : headMap.entrySet()) { Head head = entry.getValue(); int columnIndex = entry.getKey(); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName()); - WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row, + rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); Cell cell = row.createCell(columnIndex); + cellWriteHandlerContext.setCell(cell); - WriteHandlerUtils.afterCellCreate(this, cell, row, head, columnIndex, relativeRowIndex, Boolean.TRUE, - excelContentProperty); + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); WriteCellData writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex)); cell.setCellValue(writeCellData.getStringValue()); + cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(writeCellData)); + cellWriteHandlerContext.setFirstCellData(writeCellData); - WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, row, head, columnIndex, relativeRowIndex, - Boolean.TRUE, excelContentProperty); + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } } @@ -289,10 +309,15 @@ public class WriteContextImpl implements WriteContext { 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); + WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( + this, true); + WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); + WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); + + SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this, + true); + WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext, true); + WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext, true); initHead(writeTableHolder.excelWriteHeadProperty()); } @@ -332,7 +357,7 @@ public class WriteContextImpl implements WriteContext { return; } finished = true; - WriteHandlerUtils.afterWorkbookDispose(this); + WriteHandlerUtils.afterWorkbookDispose(writeWorkbookHolder.getWorkbookWriteHandlerContext()); if (writeWorkbookHolder == null) { return; } diff --git a/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java index f1068591..5fe8f0bc 100644 --- a/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java +++ b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java @@ -5,6 +5,9 @@ import java.util.Map; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.util.MapUtils; +import lombok.AllArgsConstructor; +import lombok.Data; + /** * Converter unique key.Consider that you can just use class as the key. * @@ -12,33 +15,35 @@ import com.alibaba.excel.util.MapUtils; */ public class ConverterKeyBuild { - private static final Map BOXING_MAP = MapUtils.newHashMap(); + private static final Map, Class> BOXING_MAP = MapUtils.newHashMap(); static { - BOXING_MAP.put(int.class.getName(), Integer.class.getName()); - BOXING_MAP.put(byte.class.getName(), Byte.class.getName()); - BOXING_MAP.put(long.class.getName(), Long.class.getName()); - BOXING_MAP.put(double.class.getName(), Double.class.getName()); - BOXING_MAP.put(float.class.getName(), Float.class.getName()); - BOXING_MAP.put(char.class.getName(), Character.class.getName()); - BOXING_MAP.put(short.class.getName(), Short.class.getName()); - BOXING_MAP.put(boolean.class.getName(), Boolean.class.getName()); + BOXING_MAP.put(int.class, Integer.class); + BOXING_MAP.put(byte.class, Byte.class); + BOXING_MAP.put(long.class, Long.class); + BOXING_MAP.put(double.class, Double.class); + BOXING_MAP.put(float.class, Float.class); + BOXING_MAP.put(char.class, Character.class); + BOXING_MAP.put(short.class, Short.class); + BOXING_MAP.put(boolean.class, Boolean.class); } - public static String buildKey(Class clazz) { - String className = clazz.getName(); - String boxingClassName = BOXING_MAP.get(clazz.getName()); - if (boxingClassName == null) { - return className; + public static ConverterKey buildKey(Class clazz) { + Class boxingClass = BOXING_MAP.get(clazz); + if (boxingClass != null) { + return new ConverterKey(boxingClass, null); } - return boxingClassName; + return new ConverterKey(clazz, null); } - public static String buildKey(Class clazz, CellDataTypeEnum cellDataTypeEnum) { - String key = buildKey(clazz); - if (cellDataTypeEnum == null) { - return key; - } - return key + "-" + cellDataTypeEnum; + public static ConverterKey buildKey(Class clazz, CellDataTypeEnum cellDataTypeEnum) { + return new ConverterKey(clazz, cellDataTypeEnum); + } + + @Data + @AllArgsConstructor + public static class ConverterKey { + private Class clazz; + private CellDataTypeEnum cellDataTypeEnum; } } diff --git a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index fda84b84..ac2dd8dc 100644 --- a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java @@ -2,6 +2,7 @@ package com.alibaba.excel.converters; import java.util.Map; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter; @@ -52,8 +53,8 @@ import com.alibaba.excel.util.MapUtils; * @author Jiaju Zhuang */ public class DefaultConverterLoader { - private static Map> defaultWriteConverter; - private static Map> allConverter; + private static Map> defaultWriteConverter; + private static Map> allConverter; static { initDefaultWriteConverter(); @@ -153,7 +154,7 @@ public class DefaultConverterLoader { * * @return */ - public static Map> loadDefaultWriteConverter() { + public static Map> loadDefaultWriteConverter() { return defaultWriteConverter; } @@ -171,7 +172,7 @@ public class DefaultConverterLoader { * * @return */ - public static Map> loadDefaultReadConverter() { + public static Map> loadDefaultReadConverter() { return loadAllConverter(); } @@ -180,7 +181,7 @@ public class DefaultConverterLoader { * * @return */ - public static Map> loadAllConverter() { + public static Map> loadAllConverter() { return allConverter; } diff --git a/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java b/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java index 1cb9c934..1e56a200 100644 --- a/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java +++ b/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java @@ -32,7 +32,7 @@ public class ExcelDataConvertException extends RuntimeException { */ private ExcelContentProperty excelContentProperty; - public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, + public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, ExcelContentProperty excelContentProperty, String message) { super(message); this.rowIndex = rowIndex; @@ -41,7 +41,7 @@ public class ExcelDataConvertException extends RuntimeException { this.excelContentProperty = excelContentProperty; } - public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, + public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, ExcelContentProperty excelContentProperty, String message, Throwable cause) { super(message, cause); this.rowIndex = rowIndex; diff --git a/src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java b/src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java new file mode 100644 index 00000000..9f9a2e4a --- /dev/null +++ b/src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java @@ -0,0 +1,32 @@ +package com.alibaba.excel.exception; + +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; + +import lombok.Data; + +/** + * Data convert exception + * + * @author Jiaju Zhuang + */ +@Data +public class ExcelWriteDataConvertException extends ExcelDataConvertException { + /** + * handler. + */ + private CellWriteHandlerContext cellWriteHandlerContext; + + public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message) { + super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(), + cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message); + this.cellWriteHandlerContext = cellWriteHandlerContext; + } + + public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message, + Throwable cause) { + super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(), + cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message, + cause); + this.cellWriteHandlerContext = cellWriteHandlerContext; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java index 735a0623..828f38e7 100644 --- a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java @@ -4,6 +4,7 @@ import java.util.List; import java.util.Map; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; import lombok.Data; import lombok.NoArgsConstructor; @@ -38,7 +39,7 @@ public abstract class AbstractHolder implements ConfigurationHolder { *

* Write key: */ - private Map> converterMap; + private Map> converterMap; public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) { this.newInitialization = Boolean.TRUE; @@ -81,7 +82,7 @@ public abstract class AbstractHolder implements ConfigurationHolder { } @Override - public Map> converterMap() { + public Map> converterMap() { return getConverterMap(); } diff --git a/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java b/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java index ee4d7edf..e842466d 100644 --- a/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java @@ -3,9 +3,9 @@ package com.alibaba.excel.metadata; import java.util.Map; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; /** - * * Get the corresponding holder * * @author Jiaju Zhuang @@ -13,7 +13,6 @@ import com.alibaba.excel.converters.Converter; public interface ConfigurationHolder extends Holder { /** - * * Record whether it's new or from cache * * @return Record whether it's new or from cache @@ -32,5 +31,5 @@ public interface ConfigurationHolder extends Holder { * * @return Converter */ - Map> converterMap(); + Map> converterMap(); } diff --git a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java index 5a3487bf..a988703a 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java @@ -8,7 +8,6 @@ import java.util.List; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.util.ListUtils; -import com.alibaba.excel.write.metadata.fill.AnalysisCell; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import lombok.Data; @@ -56,10 +55,6 @@ public class WriteCellData extends CellData { */ private CellStyle originCellStyle; - /** - * Only in the case of the fill is not null - */ - private AnalysisCell analysisCell; public WriteCellData(String stringValue) { this(CellDataTypeEnum.STRING, stringValue); diff --git a/src/main/java/com/alibaba/excel/util/ConverterUtils.java b/src/main/java/com/alibaba/excel/util/ConverterUtils.java index b666be6e..2a30c57c 100644 --- a/src/main/java/com/alibaba/excel/util/ConverterUtils.java +++ b/src/main/java/com/alibaba/excel/util/ConverterUtils.java @@ -8,6 +8,7 @@ import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKeyBuild; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.ReadConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; @@ -80,7 +81,7 @@ public class ConverterUtils { * @return */ public static Object convertToJavaObject(ReadCellData cellData, Field field, - ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, + ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) { Class clazz; if (field == null) { @@ -126,7 +127,7 @@ public class ConverterUtils { * @return */ private static Object doConvertToJavaObject(ReadCellData cellData, Class clazz, - ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, + ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) { Converter converter = null; if (contentProperty != null) { diff --git a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java index 1ea362b0..cb04b956 100644 --- a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java +++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java @@ -5,7 +5,6 @@ import java.util.Map; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler; @@ -18,7 +17,6 @@ import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import org.apache.commons.collections4.CollectionUtils; -import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; /** @@ -30,222 +28,205 @@ public class WriteHandlerUtils { private WriteHandlerUtils() {} - public static void beforeWorkbookCreate(WriteContext writeContext) { - beforeWorkbookCreate(writeContext, false); + public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext) { + return createWorkbookWriteHandlerContext(writeContext, false); } - public static void beforeWorkbookCreate(WriteContext writeContext, boolean runOwn) { + public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext, + boolean runOwn) { List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; + WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null, null, null); + if (runOwn) { + context.setOwnHandlerList(handlerList); + } else { + context.setHandlerList(handlerList); + } + writeContext.writeWorkbookHolder().setWorkbookWriteHandlerContext(context); + return context; + } + + public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context) { + beforeWorkbookCreate(context, false); + } + + public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { + List handlerList; + if (runOwn) { + handlerList = context.getOwnHandlerList(); + } else { + handlerList = context.getHandlerList(); } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(context); } } } - public static void afterWorkbookCreate(WriteContext writeContext) { - afterWorkbookCreate(writeContext, false); + public static void afterWorkbookCreate(WorkbookWriteHandlerContext context) { + afterWorkbookCreate(context, false); } - public static void afterWorkbookCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; + public static void afterWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { + List handlerList; + if (runOwn) { + handlerList = context.getOwnHandlerList(); + } else { + handlerList = context.getHandlerList(); } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(context); } } } - public static void afterWorkbookDispose(WriteContext writeContext) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(WorkbookWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { + public static void afterWorkbookDispose(WorkbookWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(context); } } } - public static void beforeSheetCreate(WriteContext writeContext) { - beforeSheetCreate(writeContext, false); + public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext) { + return createSheetWriteHandlerContext(writeContext, false); } - public static void beforeSheetCreate(WriteContext writeContext, boolean runOwn) { + public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext, boolean runOwn) { List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof SheetWriteHandler) { + writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), null, null); + if (runOwn) { + context.setOwnHandlerList(handlerList); + } else { + context.setHandlerList(handlerList); + } + return context; + } + + public static void beforeSheetCreate(SheetWriteHandlerContext context) { + beforeSheetCreate(context, false); + } + + public static void beforeSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { + List handlerList; + if (runOwn) { + handlerList = context.getOwnHandlerList(); + } else { + handlerList = context.getHandlerList(); + } + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((SheetWriteHandler)writeHandler).beforeSheetCreate(context); } } } - public static void afterSheetCreate(WriteContext writeContext) { - afterSheetCreate(writeContext, false); + public static void afterSheetCreate(SheetWriteHandlerContext context) { + afterSheetCreate(context, false); } - public static void afterSheetCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; + public static void afterSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { + List handlerList; + if (runOwn) { + handlerList = context.getOwnHandlerList(); + } else { + handlerList = context.getHandlerList(); } - SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof SheetWriteHandler) { + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((SheetWriteHandler)writeHandler).afterSheetCreate(context); } } } - public static void beforeCellCreate(WriteContext writeContext, Row row, Head head, Integer columnIndex, - Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, null, columnIndex, relativeRowIndex, - head, null, null, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { + public static CellWriteHandlerContext createCellWriteHandlerContext(WriteContext writeContext, Row row, + Integer rowIndex, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, + ExcelContentProperty excelContentProperty) { + List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get( + CellWriteHandler.class); + return new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, rowIndex, null, columnIndex, + relativeRowIndex, head, null, null, isHead, excelContentProperty, handlerList); + } + + public static void beforeCellCreate(CellWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((CellWriteHandler)writeHandler).beforeCellCreate(context); } } } - public static void afterCellCreate(WriteContext writeContext, Cell cell, Row row, Head head, - Integer columnIndex, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, columnIndex, relativeRowIndex, - head, null, null, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { + public static void afterCellCreate(CellWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((CellWriteHandler)writeHandler).afterCellCreate(context); } } } - public static void afterCellDataConverted(WriteContext writeContext, WriteCellData cellData, Cell cell, Row row, - Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, - ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, - columnIndex, relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { + public static void afterCellDataConverted(CellWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((CellWriteHandler)writeHandler).afterCellDataConverted(context); } } } - public static void afterCellDispose(WriteContext writeContext, WriteCellData cellData, Cell cell, Row row, - Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, - ExcelContentProperty excelContentProperty) { - List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - afterCellDispose(writeContext, cellDataList, cell, row, head, columnIndex, relativeRowIndex, isHead, - excelContentProperty); - } - - public static void afterCellDispose(WriteContext writeContext, List> cellDataList, Cell cell, - Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, - ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WriteCellData cellData = null; - if (CollectionUtils.isNotEmpty(cellDataList)) { - cellData = cellDataList.get(0); - } - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, cell, columnIndex, relativeRowIndex, - head, cellDataList, cellData, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { + public static void afterCellDispose(CellWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((CellWriteHandler)writeHandler).afterCellDispose(context); } } } - public static void beforeRowCreate(WriteContext writeContext, Integer rowIndex, Integer relativeRowIndex, - Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { + public static RowWriteHandlerContext createRowWriteHandlerContext(WriteContext writeContext, Integer rowIndex, + Integer relativeRowIndex, Boolean isHead) { + List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get( + RowWriteHandler.class); + return new RowWriteHandlerContext(writeContext, + writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, + isHead, handlerList); + } + + public static void beforeRowCreate(RowWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((RowWriteHandler)writeHandler).beforeRowCreate(context); } } } - public static void afterRowCreate(WriteContext writeContext, Row row, Integer rowIndex, Integer relativeRowIndex, - Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, row, relativeRowIndex, - isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { + public static void afterRowCreate(RowWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((RowWriteHandler)writeHandler).afterRowCreate(context); } } } - public static void afterRowDispose(WriteContext writeContext, Row row, Integer rowIndex, Integer relativeRowIndex, - Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, row, relativeRowIndex, isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { + public static void afterRowDispose(RowWriteHandlerContext context) { + List handlerList = context.getHandlerList(); + if (CollectionUtils.isNotEmpty(handlerList)) { + for (WriteHandler writeHandler : handlerList) { ((RowWriteHandler)writeHandler).afterRowDispose(context); } } } - private static List getHandlerList(WriteContext writeContext, Class clazz, + private static List getHandlerList(WriteContext writeContext, Class clazz, boolean runOwn) { Map, List> writeHandlerMap; if (runOwn) { 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 7c94e178..9d10ca1e 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -8,8 +8,7 @@ import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.exception.ExcelDataConvertException; -import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.exception.ExcelWriteDataConvertException; import com.alibaba.excel.metadata.data.CommentData; import com.alibaba.excel.metadata.data.FormulaData; import com.alibaba.excel.metadata.data.HyperlinkData; @@ -19,10 +18,11 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.FileTypeUtils; +import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.StyleUtil; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; -import com.alibaba.excel.write.metadata.holder.WriteHolder; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; @@ -32,8 +32,8 @@ import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Hyperlink; -import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; /** @@ -48,124 +48,140 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { this.writeContext = writeContext; } - protected WriteCellData converterAndSet(WriteHolder currentWriteHolder, Class clazz, - CellDataTypeEnum targetType, Cell cell, Row row, Object value, ExcelContentProperty excelContentProperty, - Head head, Integer relativeRowIndex, int rowIndex, int columnIndex) { - boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() - .getAutoTrim()); - if (needTrim) { - value = ((String)value).trim(); - } - WriteCellData cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); - WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, row, head, columnIndex, relativeRowIndex, - Boolean.FALSE, excelContentProperty); + /** + * Transform the data and then to set into the cell + * + * @param cellWriteHandlerContext context + * @return + */ + protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) { + + WriteCellData cellData = convert(cellWriteHandlerContext); + cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(cellData)); + cellWriteHandlerContext.setFirstCellData(cellData); + + WriteHandlerUtils.afterCellDataConverted(cellWriteHandlerContext); // Fill in picture information - fillImage(cell, cellData.getImageDataList()); + fillImage(cellWriteHandlerContext, cellData.getImageDataList()); // Fill in comment information - fillComment(cell, cellData.getCommentData()); + fillComment(cellWriteHandlerContext, cellData.getCommentData()); // Fill in hyper link information - fillHyperLink(cell, cellData.getHyperlinkData()); + fillHyperLink(cellWriteHandlerContext, cellData.getHyperlinkData()); // Fill in formula information - fillFormula(cell, cellData.getFormulaData()); + fillFormula(cellWriteHandlerContext, cellData.getFormulaData()); // Fill index - cellData.setRowIndex(rowIndex); - cellData.setColumnIndex(columnIndex); + cellData.setRowIndex(cellWriteHandlerContext.getRowIndex()); + cellData.setColumnIndex(cellWriteHandlerContext.getColumnIndex()); if (cellData.getType() == null) { cellData.setType(CellDataTypeEnum.EMPTY); } + Cell cell = cellWriteHandlerContext.getCell(); switch (cellData.getType()) { case STRING: cell.setCellValue(cellData.getStringValue()); - return cellData; + return; case BOOLEAN: cell.setCellValue(cellData.getBooleanValue()); - return cellData; + return; case NUMBER: cell.setCellValue(cellData.getNumberValue().doubleValue()); - return cellData; + return; case DATE: cell.setCellValue(cellData.getDateValue()); - return cellData; + return; case RICH_TEXT_STRING: cell.setCellValue(StyleUtil .buildRichTextString(writeContext.writeWorkbookHolder(), cellData.getRichTextStringDataValue())); - return cellData; + return; case EMPTY: - return cellData; + return; default: - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData, - excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType() - + "at row:" + cell.getRow().getRowNum()); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Not supported data:" + cellWriteHandlerContext.getOriginalValue() + " return type:" + + cellData.getType() + + "at row:" + cellWriteHandlerContext.getRowIndex()); } } - private void fillFormula(Cell cell, FormulaData formulaData) { + private void fillFormula(CellWriteHandlerContext cellWriteHandlerContext, FormulaData formulaData) { if (formulaData == null) { return; } + Cell cell = cellWriteHandlerContext.getCell(); if (formulaData.getFormulaValue() != null) { cell.setCellFormula(formulaData.getFormulaValue()); } } - private void fillHyperLink(Cell cell, HyperlinkData hyperlinkData) { + private void fillHyperLink(CellWriteHandlerContext cellWriteHandlerContext, HyperlinkData hyperlinkData) { if (hyperlinkData == null) { return; } - CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper(); + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); + Cell cell = cellWriteHandlerContext.getCell(); + + CreationHelper helper = workbook.getCreationHelper(); Hyperlink hyperlink = helper.createHyperlink(StyleUtil.getHyperlinkType(hyperlinkData.getHyperlinkType())); hyperlink.setAddress(hyperlinkData.getAddress()); - hyperlink.setFirstRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getFirstRowIndex(), + hyperlink.setFirstRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getFirstRowIndex(), hyperlinkData.getRelativeFirstRowIndex())); - hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getFirstColumnIndex(), + hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getFirstColumnIndex(), hyperlinkData.getRelativeFirstColumnIndex())); - hyperlink.setLastRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getLastRowIndex(), + hyperlink.setLastRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getLastRowIndex(), hyperlinkData.getRelativeLastRowIndex())); - hyperlink.setLastColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getLastColumnIndex(), + hyperlink.setLastColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getLastColumnIndex(), hyperlinkData.getRelativeLastColumnIndex())); cell.setHyperlink(hyperlink); } - private void fillComment(Cell cell, CommentData commentData) { + private void fillComment(CellWriteHandlerContext cellWriteHandlerContext, CommentData commentData) { if (commentData == null) { return; } ClientAnchor anchor; + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); + Cell cell = cellWriteHandlerContext.getCell(); + if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.XLSX) { anchor = new XSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getBottom()), - StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), + StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), commentData.getRelativeFirstColumnIndex()), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), commentData.getRelativeFirstRowIndex()), - StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), + StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), commentData.getRelativeLastColumnIndex()) + 1, - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), commentData.getRelativeLastRowIndex()) + 1); } else { anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getBottom()), - (short)StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), + (short)StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), commentData.getRelativeFirstColumnIndex()), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), commentData.getRelativeFirstRowIndex()), - (short)(StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), + (short)(StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), commentData.getRelativeLastColumnIndex()) + 1), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), commentData.getRelativeLastRowIndex()) + 1); } - Comment comment = cell.getSheet().createDrawingPatriarch().createCellComment(anchor); + + Comment comment = sheet.createDrawingPatriarch().createCellComment(anchor); if (commentData.getRichTextStringData() != null) { comment.setString( StyleUtil.buildRichTextString(writeContext.writeWorkbookHolder(), commentData.getRichTextStringData())); @@ -176,18 +192,22 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { cell.setCellComment(comment); } - protected void fillImage(Cell cell, List imageDataList) { + protected void fillImage(CellWriteHandlerContext cellWriteHandlerContext, List imageDataList) { if (CollectionUtils.isEmpty(imageDataList)) { return; } - Sheet sheet = cell.getSheet(); + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); + Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); + Drawing drawing = sheet.getDrawingPatriarch(); if (drawing == null) { drawing = sheet.createDrawingPatriarch(); } CreationHelper helper = sheet.getWorkbook().getCreationHelper(); for (ImageData imageData : imageDataList) { - int index = sheet.getWorkbook().addPicture(imageData.getImage(), + int index = workbook.addPicture(imageData.getImage(), FileTypeUtils.getImageTypeFormat(imageData.getImage())); ClientAnchor anchor = helper.createClientAnchor(); if (imageData.getTop() != null) { @@ -202,13 +222,13 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { if (imageData.getLeft() != null) { anchor.setDx1(StyleUtil.getCoordinate(imageData.getLeft())); } - anchor.setRow1(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getFirstRowIndex(), + anchor.setRow1(StyleUtil.getCellCoordinate(rowIndex, imageData.getFirstRowIndex(), imageData.getRelativeFirstRowIndex())); - anchor.setCol1(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getFirstColumnIndex(), + anchor.setCol1(StyleUtil.getCellCoordinate(columnIndex, imageData.getFirstColumnIndex(), imageData.getRelativeFirstColumnIndex())); - anchor.setRow2(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getLastRowIndex(), + anchor.setRow2(StyleUtil.getCellCoordinate(rowIndex, imageData.getLastRowIndex(), imageData.getRelativeLastRowIndex()) + 1); - anchor.setCol2(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getLastColumnIndex(), + anchor.setCol2(StyleUtil.getCellCoordinate(columnIndex, imageData.getLastColumnIndex(), imageData.getRelativeLastColumnIndex()) + 1); if (imageData.getAnchorType() != null) { anchor.setAnchorType(imageData.getAnchorType().getValue()); @@ -217,17 +237,16 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } } - protected WriteCellData convert(WriteHolder currentWriteHolder, Class clazz, CellDataTypeEnum targetType, - Cell cell, Object value, ExcelContentProperty excelContentProperty) { + protected WriteCellData convert(CellWriteHandlerContext cellWriteHandlerContext) { // This means that the user has defined the data. - if (clazz == WriteCellData.class) { - if (value == null) { + if (cellWriteHandlerContext.getOriginalFieldClass() == WriteCellData.class) { + if (cellWriteHandlerContext.getOriginalValue() == null) { return new WriteCellData<>(CellDataTypeEnum.EMPTY); } - WriteCellData cellDataValue = (WriteCellData)value; + WriteCellData cellDataValue = (WriteCellData)cellWriteHandlerContext.getOriginalValue(); if (cellDataValue.getType() != null) { // Configuration information may not be read here - fillProperty(cellDataValue, excelContentProperty); + fillProperty(cellDataValue, cellWriteHandlerContext.getExcelContentProperty()); return cellDataValue; } else { @@ -236,8 +255,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { return cellDataValue; } } - WriteCellData cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), - targetType, cell, cellDataValue.getData(), excelContentProperty); + WriteCellData cellDataReturn = doConvert(cellWriteHandlerContext); if (cellDataValue.getImageDataList() != null) { cellDataReturn.setImageDataList(cellDataValue.getImageDataList()); @@ -257,7 +275,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } return cellDataReturn; } - return doConvert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); + return doConvert(cellWriteHandlerContext); } private void fillProperty(WriteCellData cellDataValue, ExcelContentProperty excelContentProperty) { @@ -281,8 +299,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } } - private WriteCellData doConvert(WriteHolder currentWriteHolder, Class clazz, CellDataTypeEnum targetType, - Cell cell, Object value, ExcelContentProperty excelContentProperty) { + private WriteCellData doConvert(CellWriteHandlerContext cellWriteHandlerContext) { + ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty(); + Converter converter = null; if (excelContentProperty != null) { converter = excelContentProperty.getConverter(); @@ -290,31 +309,34 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { if (converter == null) { // csv is converted to string by default if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) { - targetType = CellDataTypeEnum.STRING; + cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING); } - converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType)); + converter = writeContext.currentWriteHolder().converterMap().get( + ConverterKeyBuild.buildKey(cellWriteHandlerContext.getOriginalFieldClass(), + cellWriteHandlerContext.getTargetCellDataType())); } - if (value == null && !(converter instanceof NullableObjectConverter)) { + if (cellWriteHandlerContext.getOriginalValue() == null && !(converter instanceof NullableObjectConverter)) { return new WriteCellData<>(CellDataTypeEnum.EMPTY); } if (converter == null) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Can not find 'Converter' support class " + clazz.getSimpleName() + "."); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Can not find 'Converter' support class " + cellWriteHandlerContext.getOriginalFieldClass() + .getSimpleName() + "."); } WriteCellData cellData; try { cellData = ((Converter)converter).convertToExcelData( - new WriteConverterContext<>(value, excelContentProperty, writeContext)); + new WriteConverterContext<>(cellWriteHandlerContext.getOriginalValue(), excelContentProperty, + writeContext)); } catch (Exception e) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Convert data:" + value + " error, at row:" + cell.getRow().getRowNum(), e); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " error, at row:" + + cellWriteHandlerContext.getRowIndex(), e); } if (cellData == null || cellData.getType() == null) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Convert data:" + value + " return null, at row:" + cell.getRow().getRowNum()); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return null, at row:" + + cellWriteHandlerContext.getRowIndex()); } return cellData; } diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java index db0f7247..ffb8844c 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -11,13 +11,14 @@ import java.util.TreeMap; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.alibaba.excel.write.metadata.CollectionRowData; import com.alibaba.excel.write.metadata.MapRowData; import com.alibaba.excel.write.metadata.RowData; @@ -64,9 +65,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { if (oneRowData == null) { return; } - WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE); + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, + rowIndex, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex); - WriteHandlerUtils.afterRowCreate(writeContext, row, rowIndex, relativeRowIndex, Boolean.FALSE); + rowWriteHandlerContext.setRow(row); + + WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext); if (oneRowData instanceof Collection) { addBasicTypeToExcel(new CollectionRowData((Collection)oneRowData), row, rowIndex, relativeRowIndex); @@ -75,7 +81,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } else { addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap); } - WriteHandlerUtils.afterRowDispose(writeContext, row, rowIndex, relativeRowIndex, Boolean.FALSE); + + WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext); } private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) { @@ -114,25 +121,28 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), head == null ? null : head.getFieldName()); - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, + row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, - excelContentProperty); - Object value = oneRowData.get(dataIndex); - WriteCellData cellData = converterAndSet(writeContext.currentWriteHolder(), - FieldUtils.getFieldClass(value), null, cell, row, value, null, head, relativeRowIndex, - rowIndex, columnIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, head, columnIndex, relativeRowIndex, - Boolean.FALSE, - excelContentProperty); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(oneRowData.get(dataIndex)); + cellWriteHandlerContext.setOriginalFieldClass( + FieldUtils.getFieldClass(cellWriteHandlerContext.getOriginalValue())); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, Map sortedAllFiledMap) { WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); BeanMap beanMap = BeanMapUtils.create(oneRowData); - Set beanMapHandledSet = new HashSet(); + Set beanMapHandledSet = new HashSet<>(); int maxCellIndex = -1; // If it's a class it needs to be cast by type if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) { @@ -144,20 +154,25 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { if (!beanMap.containsKey(name)) { continue; } + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, - Boolean.FALSE, excelContentProperty); - Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, row, head, columnIndex, relativeRowIndex, - Boolean.FALSE, - excelContentProperty); - Object value = beanMap.get(name); - WriteCellData cellData = converterAndSet(currentWriteHolder, head.getField().getType(), - null, cell, row, value, excelContentProperty, head, relativeRowIndex, rowIndex, columnIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, head, columnIndex, - relativeRowIndex, Boolean.FALSE, + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + + Cell cell = WorkBookUtil.createCell(row, columnIndex); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(beanMap.get(name)); + cellWriteHandlerContext.setOriginalFieldClass(head.getField().getType()); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); + beanMapHandledSet.add(name); maxCellIndex = Math.max(maxCellIndex, columnIndex); } @@ -181,20 +196,22 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { Object value = beanMap.get(filedName); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), filedName); - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + // fix https://github.com/alibaba/easyexcel/issues/1870 // If there is data, it is written to the next cell Cell cell = WorkBookUtil.createCell(row, maxCellIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, row, null, maxCellIndex, relativeRowIndex, - Boolean.FALSE, - excelContentProperty); - WriteCellData cellData = converterAndSet(currentWriteHolder, - FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, row, value, null, null, - relativeRowIndex, rowIndex, maxCellIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, row, null, maxCellIndex, relativeRowIndex, - Boolean.FALSE, - excelContentProperty); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, filedName, value)); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); maxCellIndex++; } } 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 2fb755a7..3eb49f2b 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -24,6 +24,8 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.alibaba.excel.write.metadata.fill.AnalysisCell; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillWrapper; @@ -189,8 +191,11 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } else { dataMap = BeanMapUtils.create(oneRowData); } - WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder(); for (AnalysisCell analysisCell : analysisCellList) { + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(), + relativeRowIndex, Boolean.FALSE, ExcelContentProperty.EMPTY); + if (analysisCell.getOnlyOneVariable()) { String variable = analysisCell.getVariableList().get(0); if (!dataMap.containsKey(variable)) { @@ -199,12 +204,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); - Cell cell = getOneCell(analysisCell, fillConfig, excelContentProperty); + cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); + + createCell(analysisCell, fillConfig, cellWriteHandlerContext); + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value)); - WriteCellData cellData = converterAndSet(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null, - relativeRowIndex, analysisCell.getRowIndex(), analysisCell.getColumnIndex()); - cellData.setAnalysisCell(analysisCell); + converterAndSet(cellWriteHandlerContext); + WriteCellData cellData = cellWriteHandlerContext.getFirstCellData(); // Restyle if (fillConfig.getAutoStyle()) { @@ -212,14 +219,15 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .ifPresent(cellData::setOriginCellStyle); } - - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, - excelContentProperty); } else { StringBuilder cellValueBuild = new StringBuilder(); int index = 0; List> cellDataList = new ArrayList<>(); - Cell cell = getOneCell(analysisCell, fillConfig, ExcelContentProperty.EMPTY); + + cellWriteHandlerContext.setExcelContentProperty(ExcelContentProperty.EMPTY); + + createCell(analysisCell, fillConfig, cellWriteHandlerContext); + Cell cell = cellWriteHandlerContext.getCell(); for (String variable : analysisCell.getVariableList()) { cellValueBuild.append(analysisCell.getPrepareDataList().get(index++)); @@ -229,11 +237,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); - WriteCellData cellData = convert(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable, value), CellDataTypeEnum.STRING, cell, value, - excelContentProperty); - cellData.setAnalysisCell(analysisCell); + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value)); + cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); + cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING); + + WriteCellData cellData = convert(cellWriteHandlerContext); cellDataList.add(cellData); + CellDataTypeEnum type = cellData.getType(); if (type != null) { switch (type) { @@ -253,6 +264,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } cellValueBuild.append(analysisCell.getPrepareDataList().get(index)); cell.setCellValue(cellValueBuild.toString()); + cellWriteHandlerContext.setCellDataList(cellDataList); + if (CollectionUtils.isNotEmpty(cellDataList)) { + cellWriteHandlerContext.setFirstCellData(cellDataList.get(0)); + } // Restyle if (fillConfig.getAutoStyle()) { @@ -260,10 +275,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .ifPresent(cell::setCellStyle); } - - WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, relativeRowIndex, - Boolean.FALSE, ExcelContentProperty.EMPTY); } + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } } @@ -278,11 +291,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return relativeRowIndex; } - private Cell getOneCell(AnalysisCell analysisCell, FillConfig fillConfig, - ExcelContentProperty excelContentProperty) { + private void createCell(AnalysisCell analysisCell, FillConfig fillConfig, + CellWriteHandlerContext cellWriteHandlerContext) { Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet(); if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) { - return cachedSheet.getRow(analysisCell.getRowIndex()).getCell(analysisCell.getColumnIndex()); + Row row = cachedSheet.getRow(analysisCell.getRowIndex()); + cellWriteHandlerContext.setRow(row); + Cell cell = row.getCell(analysisCell.getColumnIndex()); + cellWriteHandlerContext.setCell(cell); } Sheet sheet = writeContext.writeSheetHolder().getSheet(); @@ -319,31 +335,38 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { throw new ExcelGenerateException("The wrong direction."); } - Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell); - Cell cell = createCellIfNecessary(row, lastColumnIndex, excelContentProperty); + Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell, + cellWriteHandlerContext); + cellWriteHandlerContext.setRow(row); + + cellWriteHandlerContext.setRowIndex(lastRowIndex); + cellWriteHandlerContext.setColumnIndex(lastColumnIndex); + Cell cell = createCellIfNecessary(row, lastColumnIndex, cellWriteHandlerContext); + cellWriteHandlerContext.setCell(cell); if (isOriginalCell) { Map collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent( currentUniqueDataFlag, key -> MapUtils.newHashMap()); collectionFieldStyleMap.put(analysisCell, cell.getCellStyle()); } - return cell; } - private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, ExcelContentProperty excelContentProperty) { + private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, + CellWriteHandlerContext cellWriteHandlerContext) { Cell cell = row.getCell(lastColumnIndex); if (cell != null) { return cell; } - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, lastColumnIndex, null, Boolean.FALSE, - excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); cell = row.createCell(lastColumnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, null, null, Boolean.FALSE, excelContentProperty); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); return cell; } private Row createRowIfNecessary(Sheet sheet, Sheet cachedSheet, Integer lastRowIndex, FillConfig fillConfig, - AnalysisCell analysisCell, boolean isOriginalCell) { + AnalysisCell analysisCell, boolean isOriginalCell, CellWriteHandlerContext cellWriteHandlerContext) { Row row = sheet.getRow(lastRowIndex); if (row != null) { checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); @@ -351,7 +374,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } row = cachedSheet.getRow(lastRowIndex); if (row == null) { - WriteHandlerUtils.beforeRowCreate(writeContext, lastRowIndex, null, Boolean.FALSE); + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, + lastRowIndex, null, Boolean.FALSE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + if (fillConfig.getForceNewRow()) { row = cachedSheet.createRow(lastRowIndex); } else { @@ -364,8 +390,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { row = cachedSheet.createRow(lastRowIndex); } } + rowWriteHandlerContext.setRow(row); checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); - WriteHandlerUtils.afterRowCreate(writeContext, row, null, Boolean.FALSE); + + WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext); } else { checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java index 27530f6f..d585db93 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java @@ -3,14 +3,16 @@ package com.alibaba.excel.write.handler.context; import java.util.List; import com.alibaba.excel.context.WriteContext; +import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.WriteCellData; 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.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; -import lombok.AllArgsConstructor; import lombok.Data; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; @@ -21,7 +23,6 @@ import org.apache.poi.ss.usermodel.Row; * @author Jiaju Zhuang */ @Data -@AllArgsConstructor public class CellWriteHandlerContext { /** * write context @@ -43,6 +44,10 @@ public class CellWriteHandlerContext { * row */ private Row row; + /** + * index + */ + private Integer rowIndex; /** * cell */ @@ -78,4 +83,53 @@ public class CellWriteHandlerContext { * Field annotation configuration information. */ private ExcelContentProperty excelContentProperty; + + /** + * The value of the original + */ + private Object originalValue; + + /** + * The original field type + */ + private Class originalFieldClass; + + /** + * Target cell data type + */ + private CellDataTypeEnum targetCellDataType; + + /** + * Ignore the filling pattern and the {@code FillStyleCellWriteHandler} will not work. + * + * @see FillStyleCellWriteHandler + */ + private Boolean ignoreFillStyle; + + /** + * handler + */ + private List handlerList; + + public CellWriteHandlerContext(WriteContext writeContext, + WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder, + WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Cell cell, Integer columnIndex, + Integer relativeRowIndex, Head headData, List> cellDataList, WriteCellData firstCellData, + Boolean head, ExcelContentProperty excelContentProperty, List handlerList) { + this.writeContext = writeContext; + this.writeWorkbookHolder = writeWorkbookHolder; + this.writeSheetHolder = writeSheetHolder; + this.writeTableHolder = writeTableHolder; + this.row = row; + this.rowIndex = rowIndex; + this.cell = cell; + this.columnIndex = columnIndex; + this.relativeRowIndex = relativeRowIndex; + this.headData = headData; + this.cellDataList = cellDataList; + this.firstCellData = firstCellData; + this.head = head; + this.excelContentProperty = excelContentProperty; + this.handlerList = handlerList; + } } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java index 6578d3a2..cf3f79e6 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java @@ -1,6 +1,9 @@ package com.alibaba.excel.write.handler.context; +import java.util.List; + 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.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; @@ -49,4 +52,9 @@ public class RowWriteHandlerContext { * Nullable.It is null in the case of fill data. */ private Boolean head; + + /** + * handler + */ + private List handlerList; } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java index c1164eb0..17401bd8 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java @@ -1,6 +1,9 @@ package com.alibaba.excel.write.handler.context; +import java.util.List; + 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.WriteWorkbookHolder; @@ -27,4 +30,13 @@ public class SheetWriteHandlerContext { * sheet */ private WriteSheetHolder writeSheetHolder; + + /** + * handler + */ + private List handlerList; + /** + * handler + */ + private List ownHandlerList; } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java index f955d4bf..032f21c6 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java @@ -1,6 +1,9 @@ package com.alibaba.excel.write.handler.context; +import java.util.List; + import com.alibaba.excel.context.WriteContext; +import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; @@ -22,4 +25,14 @@ public class WorkbookWriteHandlerContext { * workbook */ private WriteWorkbookHolder writeWorkbookHolder; + + /** + * handler + */ + private List handlerList; + + /** + * handler + */ + private List ownHandlerList; } diff --git a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java index 819da9b1..eeae8e89 100644 --- a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java @@ -1,15 +1,13 @@ package com.alibaba.excel.write.handler.impl; -import java.util.List; - import com.alibaba.excel.constant.OrderConstant; import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.CellStyle; /** @@ -27,12 +25,12 @@ public class FillStyleCellWriteHandler implements CellWriteHandler { @Override public void afterCellDispose(CellWriteHandlerContext context) { - List> cellDataList = context.getCellDataList(); - if (CollectionUtils.size(cellDataList) != 1) { + if (BooleanUtils.isTrue(context.getIgnoreFillStyle())) { return; } + WriteCellData cellData = context.getFirstCellData(); - if (cellData.getAnalysisCell() != null && !cellData.getAnalysisCell().getOnlyOneVariable()) { + if (cellData == null) { return; } WriteCellStyle writeCellStyle = cellData.getWriteCellStyle(); 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 5059894a..2961efc0 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 @@ -430,4 +430,5 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ public boolean automaticMergeHead() { return getAutomaticMergeHead(); } + } diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java index 580eeb97..ede60eab 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java @@ -9,9 +9,7 @@ import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.write.metadata.WriteSheet; import lombok.Data; -import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.Setter; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFSheet; diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index 374b5b7a..ef619196 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -19,6 +19,7 @@ import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.IoUtils; import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StyleUtil; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; @@ -143,6 +144,11 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { */ private Map dataFormatMap; + /** + * handler context + */ + private WorkbookWriteHandlerContext workbookWriteHandlerContext; + public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { super(writeWorkbook, null); this.writeWorkbook = writeWorkbook;