From 25537211893d77ca2944e12317a925ec4bd700a8 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Wed, 20 Oct 2021 23:07:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=A1=AB=E5=85=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/annotation/ExcelProperty.java | 2 +- .../excel/context/WriteContextImpl.java | 14 +- .../java/com/alibaba/excel/metadata/Head.java | 18 +- .../excel/metadata/data/WriteCellData.java | 7 + .../property/ExcelContentProperty.java | 21 +- .../metadata/property/ExcelHeadProperty.java | 48 +--- .../listener/ModelBuildEventListener.java | 19 +- .../DefaultAnalysisEventProcessor.java | 7 - .../com/alibaba/excel/util/ClassUtils.java | 212 +++++++++++++++--- .../alibaba/excel/util/WriteHandlerUtils.java | 23 +- .../executor/AbstractExcelWriteExecutor.java | 3 +- .../write/executor/ExcelWriteAddExecutor.java | 49 ++-- .../executor/ExcelWriteFillExecutor.java | 37 +-- .../context/CellWriteHandlerContext.java | 5 + .../impl/FillStyleCellWriteHandler.java | 7 +- .../metadata/holder/AbstractWriteHolder.java | 21 +- .../property/ExcelWriteHeadProperty.java | 73 ++---- .../fill/style/FillStyleAnnotatedData.java | 2 +- .../fill/style/FillStyleAnnotatedTest.java | 36 +-- .../core/fill/style/FillStyleDataTest.java | 51 +++-- src/test/resources/fill/style.xls | Bin 27648 -> 27648 bytes src/test/resources/fill/style.xlsx | Bin 11114 -> 11122 bytes update.md | 1 + 23 files changed, 383 insertions(+), 273 deletions(-) diff --git a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java index e763ab37..db539266 100644 --- a/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java +++ b/src/main/java/com/alibaba/excel/annotation/ExcelProperty.java @@ -54,7 +54,7 @@ public @interface ExcelProperty { * * @return Converter */ - Class converter() default AutoConverter.class; + Class> converter() default AutoConverter.class; /** * diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 2c66b535..f31705f7 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -10,7 +10,9 @@ import com.alibaba.excel.enums.WriteTypeEnum; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; 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.NumberDataFormatterUtils; @@ -244,17 +246,21 @@ public class WriteContextImpl implements WriteContext { for (Map.Entry entry : headMap.entrySet()) { Head head = entry.getValue(); int columnIndex = entry.getKey(); - WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE); + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, + currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName()); + + WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE, + excelContentProperty); Cell cell = row.createCell(columnIndex); - WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE); + WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE, excelContentProperty); WriteCellData writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex)); cell.setCellValue(writeCellData.getStringValue()); - WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex, - Boolean.TRUE); + WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex, Boolean.TRUE, + excelContentProperty); } } diff --git a/src/main/java/com/alibaba/excel/metadata/Head.java b/src/main/java/com/alibaba/excel/metadata/Head.java index 2f98a14b..7e1fd47b 100644 --- a/src/main/java/com/alibaba/excel/metadata/Head.java +++ b/src/main/java/com/alibaba/excel/metadata/Head.java @@ -1,5 +1,6 @@ package com.alibaba.excel.metadata; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -22,6 +23,10 @@ public class Head { * Column index of head */ private Integer columnIndex; + /** + * It only has values when passed in {@link Sheet#setClazz(Class)} and {@link Table#setClazz(Class)} + */ + private Field field; /** * It only has values when passed in {@link Sheet#setClazz(Class)} and {@link Table#setClazz(Class)} */ @@ -38,10 +43,12 @@ public class Head { * Whether to specify a name */ private Boolean forceName; + /** * column with */ private ColumnWidthProperty columnWidthProperty; + /** * Loop merge */ @@ -50,22 +57,15 @@ public class Head { * Head style */ private StyleProperty headStyleProperty; - /** - * Content style - */ - private StyleProperty contentStyleProperty; /** * Head font */ private FontProperty headFontProperty; - /** - * Content font - */ - private FontProperty contentFontProperty; - public Head(Integer columnIndex, String fieldName, List headNameList, Boolean forceIndex, + public Head(Integer columnIndex, Field field, String fieldName, List headNameList, Boolean forceIndex, Boolean forceName) { this.columnIndex = columnIndex; + this.field = field; this.fieldName = fieldName; if (headNameList == null) { this.headNameList = new ArrayList<>(); 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 cf60f58e..5a3487bf 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java @@ -8,6 +8,7 @@ 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; @@ -43,6 +44,7 @@ public class WriteCellData extends CellData { * hyper link */ private HyperlinkData hyperlinkData; + /** * style */ @@ -54,6 +56,11 @@ 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/metadata/property/ExcelContentProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java index f6f92532..1c0bb232 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java @@ -3,7 +3,6 @@ package com.alibaba.excel.metadata.property; import java.lang.reflect.Field; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.metadata.Head; import lombok.Data; @@ -12,18 +11,30 @@ import lombok.Data; */ @Data public class ExcelContentProperty { + public static final ExcelContentProperty EMPTY = new ExcelContentProperty(); + /** * Java filed */ private Field field; - /** - * Excel head - */ - private Head head; /** * Custom defined converters */ private Converter converter; + /** + * date time format + */ private DateTimeFormatProperty dateTimeFormatProperty; + /** + * number format + */ private NumberFormatProperty numberFormatProperty; + /** + * Content style + */ + private StyleProperty contentStyleProperty; + /** + * Content font + */ + private FontProperty contentFontProperty; } diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index 3562501d..a0359fb3 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -8,15 +8,11 @@ import java.util.Map; import java.util.TreeMap; import com.alibaba.excel.annotation.ExcelProperty; -import com.alibaba.excel.annotation.format.DateTimeFormat; -import com.alibaba.excel.annotation.format.NumberFormat; -import com.alibaba.excel.converters.AutoConverter; -import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.HeadKindEnum; -import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.util.ClassUtils; +import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder; @@ -38,7 +34,7 @@ public class ExcelHeadProperty { /** * Custom class */ - private Class headClazz; + private Class headClazz; /** * The types of head */ @@ -51,24 +47,14 @@ public class ExcelHeadProperty { * Configuration header information */ private Map headMap; - /** - * Configuration column information - */ - private Map contentPropertyMap; - /** - * Configuration column information - */ - private Map fieldNameContentPropertyMap; /** * Fields ignored */ private Map ignoreMap; - public ExcelHeadProperty(Holder holder, Class headClazz, List> head) { + public ExcelHeadProperty(Holder holder, Class headClazz, List> head) { this.headClazz = headClazz; headMap = new TreeMap<>(); - contentPropertyMap = new TreeMap<>(); - fieldNameContentPropertyMap = MapUtils.newHashMap(); ignoreMap = MapUtils.newHashMap(); headKind = HeadKindEnum.NONE; headRowNumber = 0; @@ -80,8 +66,7 @@ public class ExcelHeadProperty { continue; } } - headMap.put(headIndex, new Head(headIndex, null, head.get(i), Boolean.FALSE, Boolean.TRUE)); - contentPropertyMap.put(headIndex, null); + headMap.put(headIndex, new Head(headIndex, null, null, head.get(i), Boolean.FALSE, Boolean.TRUE)); headIndex++; } headKind = HeadKindEnum.STRING; @@ -148,39 +133,20 @@ public class ExcelHeadProperty { private void initOneColumnProperty(int index, Field field, Boolean forceIndex) { ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); List tmpHeadList = new ArrayList(); + String fieldName = FieldUtils.resolveCglibFieldName(field); boolean notForceName = excelProperty == null || excelProperty.value().length <= 0 || (excelProperty.value().length == 1 && StringUtils.isEmpty((excelProperty.value())[0])); if (headMap.containsKey(index)) { tmpHeadList.addAll(headMap.get(index).getHeadNameList()); } else { if (notForceName) { - tmpHeadList.add(field.getName()); + tmpHeadList.add(fieldName); } else { Collections.addAll(tmpHeadList, excelProperty.value()); } } - Head head = new Head(index, field.getName(), tmpHeadList, forceIndex, !notForceName); - ExcelContentProperty excelContentProperty = new ExcelContentProperty(); - if (excelProperty != null) { - Class convertClazz = excelProperty.converter(); - if (convertClazz != AutoConverter.class) { - try { - Converter converter = convertClazz.newInstance(); - excelContentProperty.setConverter(converter); - } catch (Exception e) { - throw new ExcelCommonException("Can not instance custom converter:" + convertClazz.getName()); - } - } - } - excelContentProperty.setHead(head); - excelContentProperty.setField(field); - excelContentProperty - .setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); - excelContentProperty - .setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); + Head head = new Head(index, field, fieldName, tmpHeadList, forceIndex, !notForceName); headMap.put(index, head); - contentPropertyMap.put(index, excelContentProperty); - fieldNameContentPropertyMap.put(field.getName(), excelContentProperty); } public boolean hasHead() { diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 6a814f4d..7ba8b88f 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -8,14 +8,14 @@ import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.ReadCellData; -import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; import com.alibaba.excel.util.BeanMapUtils; +import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.ConverterUtils; -import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.MapUtils; +import net.sf.cglib.beans.BeanMap; import org.apache.commons.collections4.CollectionUtils; /** @@ -92,22 +92,23 @@ public class ModelBuildEventListener implements ReadListener headMap = excelReadHeadProperty.getHeadMap(); Map map = MapUtils.newHashMapWithExpectedSize(headMap.size()); - Map contentPropertyMap = excelReadHeadProperty.getContentPropertyMap(); + BeanMap dataMap = BeanMapUtils.create(resultModel); for (Map.Entry entry : headMap.entrySet()) { Integer index = entry.getKey(); + Head head = entry.getValue(); + String fieldName = head.getFieldName(); if (!cellDataMap.containsKey(index)) { continue; } ReadCellData cellData = cellDataMap.get(index); - ExcelContentProperty excelContentProperty = contentPropertyMap.get(index); - Object value = ConverterUtils.convertToJavaObject(cellData, excelContentProperty.getField(), - excelContentProperty, readSheetHolder.converterMap(), context, - context.readRowHolder().getRowIndex(), index); + Object value = ConverterUtils.convertToJavaObject(cellData, head.getField(), + ClassUtils.declaredExcelContentProperty(dataMap, readSheetHolder.excelReadHeadProperty().getHeadClazz(), + fieldName), readSheetHolder.converterMap(), context, context.readRowHolder().getRowIndex(), index); if (value != null) { - map.put(FieldUtils.resolveCglibFieldName(excelContentProperty.getField()), value); + map.put(fieldName, value); } } - BeanMapUtils.create(resultModel).putAll(map); + dataMap.putAll(map); return resultModel; } diff --git a/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java b/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java index c3c50234..914a9654 100644 --- a/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java +++ b/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java @@ -11,7 +11,6 @@ import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.ReadCellData; -import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; @@ -118,15 +117,11 @@ public class DefaultAnalysisEventProcessor implements AnalysisEventProcessor { Map dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext); ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty(); Map headMapData = excelHeadPropertyData.getHeadMap(); - Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); Map tmpHeadMap = new HashMap(headMapData.size() * 4 / 3 + 1); - Map tmpContentPropertyMap = - new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); for (Map.Entry entry : headMapData.entrySet()) { Head headData = entry.getValue(); if (headData.getForceIndex() || !headData.getForceName()) { tmpHeadMap.put(entry.getKey(), headData); - tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); continue; } List headNameList = headData.getHeadNameList(); @@ -146,12 +141,10 @@ public class DefaultAnalysisEventProcessor implements AnalysisEventProcessor { if (headName.equals(headString)) { headData.setColumnIndex(stringKey); tmpHeadMap.put(stringKey, headData); - tmpContentPropertyMap.put(stringKey, contentPropertyMapData.get(entry.getKey())); break; } } } excelHeadPropertyData.setHeadMap(tmpHeadMap); - excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); } } diff --git a/src/main/java/com/alibaba/excel/util/ClassUtils.java b/src/main/java/com/alibaba/excel/util/ClassUtils.java index 50e9351a..491d4b3d 100644 --- a/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -1,6 +1,5 @@ package com.alibaba.excel.util; -import java.lang.ref.SoftReference; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; @@ -10,16 +9,26 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; +import com.alibaba.excel.annotation.write.style.ContentFontStyle; +import com.alibaba.excel.annotation.write.style.ContentStyle; +import com.alibaba.excel.converters.AutoConverter; +import com.alibaba.excel.converters.Converter; import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.metadata.Holder; +import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.metadata.property.FontProperty; +import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.metadata.holder.WriteHolder; +import net.sf.cglib.beans.BeanMap; + /** * Class utils * @@ -27,7 +36,150 @@ import com.alibaba.excel.write.metadata.holder.WriteHolder; **/ public class ClassUtils { - public static final Map, SoftReference> FIELD_CACHE = new ConcurrentHashMap<>(); + public static final Map, FieldCache> FIELD_CACHE = new ConcurrentHashMap<>(); + + /** + * The cache configuration information for each of the class + */ + public static final Map, Map> CLASS_CONTENT_CACHE + = new ConcurrentHashMap<>(); + + /** + * The cache configuration information for each of the class + */ + public static final Map CONTENT_CACHE = new ConcurrentHashMap<>(); + + /** + * Calculate the configuration information for the class + * + * @param dataMap + * @param headClazz + * @param fieldName + * @return + */ + public static ExcelContentProperty declaredExcelContentProperty(Map dataMap, Class headClazz, + String fieldName) { + Class clazz = null; + if (dataMap instanceof BeanMap) { + Object bean = ((BeanMap)dataMap).getBean(); + if (bean != null) { + clazz = bean.getClass(); + } + } + return getExcelContentProperty(clazz, headClazz, fieldName); + } + + private static ExcelContentProperty getExcelContentProperty(Class clazz, Class headClass, String fieldName) { + return CONTENT_CACHE.computeIfAbsent(buildKey(clazz, headClass, fieldName), key -> { + ExcelContentProperty excelContentProperty = Optional.ofNullable(declaredFieldContentMap(clazz)) + .map(map -> map.get(fieldName)) + .orElse(null); + ExcelContentProperty headExcelContentProperty = Optional.ofNullable(declaredFieldContentMap(headClass)) + .map(map -> map.get(fieldName)) + .orElse(null); + ExcelContentProperty combineExcelContentProperty = new ExcelContentProperty(); + + combineExcelContentProperty(combineExcelContentProperty, headExcelContentProperty); + if (clazz != headClass) { + combineExcelContentProperty(combineExcelContentProperty, excelContentProperty); + } + return combineExcelContentProperty; + }); + + } + + public static void combineExcelContentProperty(ExcelContentProperty combineExcelContentProperty, + ExcelContentProperty excelContentProperty) { + if (excelContentProperty == null) { + return; + } + if (excelContentProperty.getField() != null) { + combineExcelContentProperty.setField(excelContentProperty.getField()); + } + if (excelContentProperty.getConverter() != null) { + combineExcelContentProperty.setConverter(excelContentProperty.getConverter()); + } + if (excelContentProperty.getDateTimeFormatProperty() != null) { + combineExcelContentProperty.setDateTimeFormatProperty(excelContentProperty.getDateTimeFormatProperty()); + } + if (excelContentProperty.getNumberFormatProperty() != null) { + combineExcelContentProperty.setNumberFormatProperty(excelContentProperty.getNumberFormatProperty()); + } + if (excelContentProperty.getContentStyleProperty() != null) { + combineExcelContentProperty.setContentStyleProperty(excelContentProperty.getContentStyleProperty()); + } + if (excelContentProperty.getContentFontProperty() != null) { + combineExcelContentProperty.setContentFontProperty(excelContentProperty.getContentFontProperty()); + } + } + + private static String buildKey(Class clazz, Class headClass, String fieldName) { + String key = ""; + if (clazz != null) { + key += clazz.getName(); + } + key += "-"; + if (headClass != null) { + key += headClass.getName(); + } + key += "-"; + if (fieldName != null) { + key += fieldName; + } + return key; + } + + private static Map declaredFieldContentMap(Class clazz) { + if (clazz == null) { + return null; + } + return CLASS_CONTENT_CACHE.computeIfAbsent(clazz, key -> { + List tempFieldList = new ArrayList<>(); + Class tempClass = clazz; + while (tempClass != null) { + Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); + // Get the parent class and give it to yourself + tempClass = tempClass.getSuperclass(); + } + + ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class); + ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class); + Map fieldContentMap = MapUtils.newHashMapWithExpectedSize( + tempFieldList.size()); + for (Field field : tempFieldList) { + ExcelContentProperty excelContentProperty = new ExcelContentProperty(); + excelContentProperty.setField(field); + + ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); + if (excelProperty != null) { + Class> convertClazz = excelProperty.converter(); + if (convertClazz != AutoConverter.class) { + try { + Converter converter = convertClazz.newInstance(); + excelContentProperty.setConverter(converter); + } catch (Exception e) { + throw new ExcelCommonException( + "Can not instance custom converter:" + convertClazz.getName()); + } + } + } + + ContentStyle contentStyle = field.getAnnotation(ContentStyle.class); + if (contentStyle == null) { + contentStyle = parentContentStyle; + } + excelContentProperty.setContentStyleProperty(StyleProperty.build(contentStyle)); + + ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class); + if (contentFontStyle == null) { + contentFontStyle = parentContentFontStyle; + } + excelContentProperty.setContentFontProperty(FontProperty.build(contentFontStyle)); + fieldContentMap.put(field.getName(), excelContentProperty); + } + return fieldContentMap; + }); + } /** * Parsing filed in the class @@ -41,7 +193,7 @@ public class ClassUtils { */ public static void declaredFields(Class clazz, Map sortedAllFiledMap, Map indexFiledMap, Map ignoreMap, Boolean needIgnore, Holder holder) { - FieldCache fieldCache = getFieldCache(clazz); + FieldCache fieldCache = declaredFields(clazz); if (fieldCache == null) { return; } @@ -92,45 +244,31 @@ public class ClassUtils { declaredFields(clazz, sortedAllFiledMap, null, null, needIgnore, writeHolder); } - private static FieldCache getFieldCache(Class clazz) { + private static FieldCache declaredFields(Class clazz) { if (clazz == null) { return null; } - SoftReference fieldCacheSoftReference = FIELD_CACHE.get(clazz); - if (fieldCacheSoftReference != null && fieldCacheSoftReference.get() != null) { - return fieldCacheSoftReference.get(); - } - synchronized (clazz) { - fieldCacheSoftReference = FIELD_CACHE.get(clazz); - if (fieldCacheSoftReference != null && fieldCacheSoftReference.get() != null) { - return fieldCacheSoftReference.get(); + return FIELD_CACHE.computeIfAbsent(clazz, key -> { + List tempFieldList = new ArrayList<>(); + Class tempClass = clazz; + // When the parent class is null, it indicates that the parent class (Object class) has reached the top + // level. + while (tempClass != null) { + Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); + // Get the parent class and give it to yourself + tempClass = tempClass.getSuperclass(); } - declaredFields(clazz); - } - return FIELD_CACHE.get(clazz).get(); - } + // Screening of field + Map> orderFiledMap = new TreeMap>(); + Map indexFiledMap = new TreeMap(); + Map ignoreMap = new HashMap(16); - private static void declaredFields(Class clazz) { - List tempFieldList = new ArrayList<>(); - Class tempClass = clazz; - // When the parent class is null, it indicates that the parent class (Object class) has reached the top - // level. - while (tempClass != null) { - Collections.addAll(tempFieldList, tempClass.getDeclaredFields()); - // Get the parent class and give it to yourself - tempClass = tempClass.getSuperclass(); - } - // Screening of field - Map> orderFiledMap = new TreeMap>(); - Map indexFiledMap = new TreeMap(); - Map ignoreMap = new HashMap(16); - - ExcelIgnoreUnannotated excelIgnoreUnannotated = clazz.getAnnotation(ExcelIgnoreUnannotated.class); - for (Field field : tempFieldList) { - declaredOneField(field, orderFiledMap, indexFiledMap, ignoreMap, excelIgnoreUnannotated); - } - FIELD_CACHE.put(clazz, new SoftReference( - new FieldCache(buildSortedAllFiledMap(orderFiledMap, indexFiledMap), indexFiledMap, ignoreMap))); + ExcelIgnoreUnannotated excelIgnoreUnannotated = clazz.getAnnotation(ExcelIgnoreUnannotated.class); + for (Field field : tempFieldList) { + declaredOneField(field, orderFiledMap, indexFiledMap, ignoreMap, excelIgnoreUnannotated); + } + return new FieldCache(buildSortedAllFiledMap(orderFiledMap, indexFiledMap), indexFiledMap, ignoreMap); + }); } private static Map buildSortedAllFiledMap(Map> orderFiledMap, diff --git a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java index 4279baf4..78ce8d8e 100644 --- a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java +++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java @@ -6,6 +6,7 @@ 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; import com.alibaba.excel.write.handler.SheetWriteHandler; @@ -116,7 +117,7 @@ public class WriteHandlerUtils { } public static void beforeCellCreate(WriteContext writeContext, Row row, Head head, Integer columnIndex, - Integer relativeRowIndex, Boolean isHead) { + Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { @@ -124,7 +125,7 @@ public class WriteHandlerUtils { } CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, null, columnIndex, relativeRowIndex, - head, null, null, isHead); + head, null, null, isHead, excelContentProperty); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof CellWriteHandler) { ((CellWriteHandler)writeHandler).beforeCellCreate(context); @@ -133,7 +134,7 @@ public class WriteHandlerUtils { } public static void afterCellCreate(WriteContext writeContext, Cell cell, Head head, Integer relativeRowIndex, - Boolean isHead) { + Boolean isHead, ExcelContentProperty excelContentProperty) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { @@ -141,7 +142,7 @@ public class WriteHandlerUtils { } CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, null, null, isHead); + cell.getColumnIndex(), relativeRowIndex, head, null, null, isHead, excelContentProperty); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof CellWriteHandler) { ((CellWriteHandler)writeHandler).afterCellCreate(context); @@ -150,8 +151,7 @@ public class WriteHandlerUtils { } public static void afterCellDataConverted(WriteContext writeContext, WriteCellData cellData, Cell cell, - Head head, - Integer relativeRowIndex, Boolean isHead) { + Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { @@ -160,7 +160,7 @@ public class WriteHandlerUtils { List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead); + cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof CellWriteHandler) { ((CellWriteHandler)writeHandler).afterCellDataConverted(context); @@ -169,14 +169,13 @@ public class WriteHandlerUtils { } public static void afterCellDispose(WriteContext writeContext, WriteCellData cellData, Cell cell, Head head, - Integer relativeRowIndex, Boolean isHead) { + Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - afterCellDispose(writeContext, cellDataList, cell, head, relativeRowIndex, isHead); + afterCellDispose(writeContext, cellDataList, cell, head, relativeRowIndex, isHead, excelContentProperty); } public static void afterCellDispose(WriteContext writeContext, List> cellDataList, Cell cell, - Head head, - Integer relativeRowIndex, Boolean isHead) { + Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); if (handlerList == null || handlerList.isEmpty()) { @@ -188,7 +187,7 @@ public class WriteHandlerUtils { } CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead); + cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); for (WriteHandler writeHandler : handlerList) { if (writeHandler instanceof CellWriteHandler) { ((CellWriteHandler)writeHandler).afterCellDispose(context); 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 ecc34b55..92e42419 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -56,7 +56,8 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { value = ((String)value).trim(); } WriteCellData cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); - WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, + excelContentProperty); // Fill in picture information fillImage(cell, cellData.getImageDataList()); 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 866f6378..6bfe34c2 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -110,13 +110,20 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int relativeRowIndex, int dataIndex, int cellIndex) { - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE); + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, + writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), + head == null ? null : head.getFieldName()); + + WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE, + excelContentProperty); Cell cell = WorkBookUtil.createCell(row, cellIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, + excelContentProperty); Object value = oneRowData.get(dataIndex); WriteCellData cellData = converterAndSet(writeContext.currentWriteHolder(), FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, + excelContentProperty); } private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowIndex, @@ -128,24 +135,25 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { // If it's a class it needs to be cast by type if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) { Map headMap = writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadMap(); - Map contentPropertyMap = - writeContext.currentWriteHolder().excelWriteHeadProperty().getContentPropertyMap(); - for (Map.Entry entry : contentPropertyMap.entrySet()) { + for (Map.Entry entry : headMap.entrySet()) { int cellIndex = entry.getKey(); - ExcelContentProperty excelContentProperty = entry.getValue(); - String name = FieldUtils.resolveCglibFieldName(excelContentProperty.getField()); + Head head = entry.getValue(); + String name = head.getFieldName(); if (!beanMap.containsKey(name)) { continue; } - Head head = headMap.get(cellIndex); - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE); + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, + currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); + WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE, + excelContentProperty); Cell cell = WorkBookUtil.createCell(row, cellIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, + excelContentProperty); Object value = beanMap.get(name); - WriteCellData cellData = converterAndSet(currentWriteHolder, - excelContentProperty.getField().getType(), + WriteCellData cellData = converterAndSet(currentWriteHolder, head.getField().getType(), null, cell, value, excelContentProperty, head, relativeRowIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, + excelContentProperty); beanMapHandledSet.add(name); maxCellIndex = Math.max(maxCellIndex, cellIndex); } @@ -160,21 +168,26 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { initSortedAllFiledMapFieldList(oneRowData.getClass(), sortedAllFiledMap); for (Map.Entry entry : sortedAllFiledMap.entrySet()) { Field field = entry.getValue(); - String filedName = field.getName(); + String filedName = FieldUtils.resolveCglibFieldName(field); boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName) || ignoreMap.containsKey(filedName); if (uselessData) { continue; } Object value = beanMap.get(filedName); - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, maxCellIndex, relativeRowIndex, Boolean.FALSE); + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, + currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), filedName); + WriteHandlerUtils.beforeCellCreate(writeContext, row, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, + excelContentProperty); // 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, null, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE, + excelContentProperty); WriteCellData cellData = converterAndSet(currentWriteHolder, FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, + excelContentProperty); } } 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 70e28e77..10025390 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -18,6 +18,7 @@ import com.alibaba.excel.exception.ExcelGenerateException; 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.ListUtils; import com.alibaba.excel.util.MapUtils; @@ -189,19 +190,21 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { dataMap = BeanMapUtils.create(oneRowData); } WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder(); - Map fieldNameContentPropertyMap = - writeContext.currentWriteHolder().excelWriteHeadProperty().getFieldNameContentPropertyMap(); for (AnalysisCell analysisCell : analysisCellList) { - Cell cell = getOneCell(analysisCell, fillConfig); if (analysisCell.getOnlyOneVariable()) { String variable = analysisCell.getVariableList().get(0); if (!dataMap.containsKey(variable)) { continue; } Object value = dataMap.get(variable); + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, + writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); + Cell cell = getOneCell(analysisCell, fillConfig, excelContentProperty); + WriteCellData cellData = converterAndSet(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable, value), - null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex); + FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null, + relativeRowIndex); + cellData.setAnalysisCell(analysisCell); // Restyle if (fillConfig.getAutoStyle()) { @@ -210,20 +213,26 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { .ifPresent(cellData::setOriginCellStyle); } - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE); + 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); + for (String variable : analysisCell.getVariableList()) { cellValueBuild.append(analysisCell.getPrepareDataList().get(index++)); if (!dataMap.containsKey(variable)) { continue; } 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, - fieldNameContentPropertyMap.get(variable)); + excelContentProperty); + cellData.setAnalysisCell(analysisCell); cellDataList.add(cellData); CellDataTypeEnum type = cellData.getType(); if (type != null) { @@ -253,7 +262,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, relativeRowIndex, - Boolean.FALSE); + Boolean.FALSE, ExcelContentProperty.EMPTY); } } } @@ -269,7 +278,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return relativeRowIndex; } - private Cell getOneCell(AnalysisCell analysisCell, FillConfig fillConfig) { + private Cell getOneCell(AnalysisCell analysisCell, FillConfig fillConfig, + ExcelContentProperty excelContentProperty) { Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet(); if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) { return cachedSheet.getRow(analysisCell.getRowIndex()).getCell(analysisCell.getColumnIndex()); @@ -310,7 +320,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell); - Cell cell = createCellIfNecessary(row, lastColumnIndex); + Cell cell = createCellIfNecessary(row, lastColumnIndex, excelContentProperty); if (isOriginalCell) { Map collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent( @@ -320,14 +330,15 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return cell; } - private Cell createCellIfNecessary(Row row, Integer lastColumnIndex) { + private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, ExcelContentProperty excelContentProperty) { Cell cell = row.getCell(lastColumnIndex); if (cell != null) { return cell; } - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, lastColumnIndex, null, Boolean.FALSE); + WriteHandlerUtils.beforeCellCreate(writeContext, row, null, lastColumnIndex, null, Boolean.FALSE, + excelContentProperty); cell = row.createCell(lastColumnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, null, null, Boolean.FALSE); + WriteHandlerUtils.afterCellCreate(writeContext, cell, null, null, Boolean.FALSE, excelContentProperty); return cell; } 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 92c1500f..27530f6f 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 @@ -5,6 +5,7 @@ import java.util.List; 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.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; @@ -73,4 +74,8 @@ public class CellWriteHandlerContext { * Nullable.It is null in the case of fill data. */ private Boolean head; + /** + * Field annotation configuration information. + */ + private ExcelContentProperty excelContentProperty; } 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 faf39ad7..c8116b4d 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 @@ -29,10 +29,13 @@ public class FillStyleCellWriteHandler implements CellWriteHandler { @Override public void afterCellDispose(CellWriteHandlerContext context) { List> cellDataList = context.getCellDataList(); - if (CollectionUtils.isEmpty(cellDataList)) { + if (CollectionUtils.size(cellDataList) != 1) { + return; + } + WriteCellData cellData = context.getFirstCellData(); + if (cellData.getAnalysisCell() != null && !cellData.getAnalysisCell().getOnlyOneVariable()) { return; } - WriteCellData cellData = cellDataList.get(0); WriteCellStyle writeCellStyle = cellData.getWriteCellStyle(); CellStyle originCellStyle = cellData.getOriginCellStyle(); if (writeCellStyle == null && originCellStyle == null) { 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 8028e4bd..5059894a 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 @@ -18,6 +18,7 @@ import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.event.Order; import com.alibaba.excel.metadata.AbstractHolder; import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty; import com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty; import com.alibaba.excel.metadata.property.RowHeightProperty; @@ -78,6 +79,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ * Whether to automatically merge headers.Default is true. */ private Boolean automaticMergeHead; + /** * Ignore the custom columns. */ @@ -195,7 +197,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } if (writeBasicParameter.getCustomConverterList() != null && !writeBasicParameter.getCustomConverterList().isEmpty()) { - for (Converter converter : writeBasicParameter.getCustomConverterList()) { + for (Converter converter : writeBasicParameter.getCustomConverterList()) { getConverterMap().put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter); } } @@ -216,8 +218,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ if (head.getColumnWidthProperty() != null) { hasColumnWidth = true; } - if (head.getHeadStyleProperty() != null || head.getHeadFontProperty() != null - || head.getContentStyleProperty() != null || head.getContentFontProperty() != null) { + if (head.getHeadStyleProperty() != null || head.getHeadFontProperty() != null) { hasStyle = true; } dealLoopMerge(handlerList, head); @@ -227,9 +228,9 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ dealColumnWidth(handlerList); } - if (hasStyle) { + //if (hasStyle) { dealStyle(handlerList); - } + //} dealRowHigh(handlerList); dealOnceAbsoluteMerge(handlerList); @@ -244,14 +245,16 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ @Override protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) { - //return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty()); - return null; + ExcelContentProperty excelContentProperty = context.getExcelContentProperty(); + return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(), + excelContentProperty.getContentFontProperty()); } @Override protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) { - //return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty()); - return null; + ExcelContentProperty excelContentProperty = context.getExcelContentProperty(); + return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(), + excelContentProperty.getContentFontProperty()); } }; handlerList.add(styleStrategy); diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index c2779136..692a510a 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -7,11 +7,8 @@ import java.util.List; import java.util.Map; import java.util.Set; -import com.alibaba.excel.annotation.write.style.ColumnWidth; -import com.alibaba.excel.annotation.write.style.ContentFontStyle; import com.alibaba.excel.annotation.write.style.ContentLoopMerge; import com.alibaba.excel.annotation.write.style.ContentRowHeight; -import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.annotation.write.style.HeadRowHeight; import com.alibaba.excel.annotation.write.style.HeadStyle; @@ -20,8 +17,6 @@ import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.CellRange; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Holder; -import com.alibaba.excel.metadata.property.ColumnWidthProperty; -import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty; @@ -29,49 +24,43 @@ import com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty; import com.alibaba.excel.metadata.property.RowHeightProperty; import com.alibaba.excel.metadata.property.StyleProperty; +import lombok.Data; + /** * Define the header attribute of excel * * @author jipengfei */ +@Data public class ExcelWriteHeadProperty extends ExcelHeadProperty { private RowHeightProperty headRowHeightProperty; private RowHeightProperty contentRowHeightProperty; private OnceAbsoluteMergeProperty onceAbsoluteMergeProperty; - public ExcelWriteHeadProperty(Holder holder, Class headClazz, List> head) { + public ExcelWriteHeadProperty(Holder holder, Class headClazz, List> head) { super(holder, headClazz, head); if (getHeadKind() != HeadKindEnum.CLASS) { return; } this.headRowHeightProperty = - RowHeightProperty.build((HeadRowHeight)headClazz.getAnnotation(HeadRowHeight.class)); + RowHeightProperty.build(headClazz.getAnnotation(HeadRowHeight.class)); this.contentRowHeightProperty = - RowHeightProperty.build((ContentRowHeight)headClazz.getAnnotation(ContentRowHeight.class)); + RowHeightProperty.build(headClazz.getAnnotation(ContentRowHeight.class)); this.onceAbsoluteMergeProperty = - OnceAbsoluteMergeProperty.build((OnceAbsoluteMerge)headClazz.getAnnotation(OnceAbsoluteMerge.class)); + OnceAbsoluteMergeProperty.build(headClazz.getAnnotation(OnceAbsoluteMerge.class)); - ColumnWidth parentColumnWidth = (ColumnWidth)headClazz.getAnnotation(ColumnWidth.class); - HeadStyle parentHeadStyle = (HeadStyle)headClazz.getAnnotation(HeadStyle.class); - HeadFontStyle parentHeadFontStyle = (HeadFontStyle)headClazz.getAnnotation(HeadFontStyle.class); - ContentStyle parentContentStyle = (ContentStyle)headClazz.getAnnotation(ContentStyle.class); - ContentFontStyle parentContentFontStyle = (ContentFontStyle)headClazz.getAnnotation(ContentFontStyle.class); + HeadStyle parentHeadStyle = headClazz.getAnnotation(HeadStyle.class); + HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class); - for (Map.Entry entry : getContentPropertyMap().entrySet()) { + for (Map.Entry entry : getHeadMap().entrySet()) { Integer index = entry.getKey(); - ExcelContentProperty excelContentPropertyData = entry.getValue(); - if (excelContentPropertyData == null) { + Head headData = entry.getValue(); + if (headData == null) { throw new IllegalArgumentException( "Passing in the class and list the head, the two must be the same size."); } - Field field = excelContentPropertyData.getField(); - Head headData = getHeadMap().get(index); - ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class); - if (columnWidth == null) { - columnWidth = parentColumnWidth; - } - headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth)); + Field field = headData.getField(); HeadStyle headStyle = field.getAnnotation(HeadStyle.class); if (headStyle == null) { @@ -85,46 +74,10 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { } headData.setHeadFontProperty(FontProperty.build(headFontStyle)); - ContentStyle contentStyle = field.getAnnotation(ContentStyle.class); - if (contentStyle == null) { - contentStyle = parentContentStyle; - } - headData.setContentStyleProperty(StyleProperty.build(contentStyle)); - - ContentFontStyle contentFontStyle = field.getAnnotation(ContentFontStyle.class); - if (contentFontStyle == null) { - contentFontStyle = parentContentFontStyle; - } - headData.setContentFontProperty(FontProperty.build(contentFontStyle)); - headData.setLoopMergeProperty(LoopMergeProperty.build(field.getAnnotation(ContentLoopMerge.class))); } } - public RowHeightProperty getHeadRowHeightProperty() { - return headRowHeightProperty; - } - - public void setHeadRowHeightProperty(RowHeightProperty headRowHeightProperty) { - this.headRowHeightProperty = headRowHeightProperty; - } - - public RowHeightProperty getContentRowHeightProperty() { - return contentRowHeightProperty; - } - - public void setContentRowHeightProperty(RowHeightProperty contentRowHeightProperty) { - this.contentRowHeightProperty = contentRowHeightProperty; - } - - public OnceAbsoluteMergeProperty getOnceAbsoluteMergeProperty() { - return onceAbsoluteMergeProperty; - } - - public void setOnceAbsoluteMergeProperty(OnceAbsoluteMergeProperty onceAbsoluteMergeProperty) { - this.onceAbsoluteMergeProperty = onceAbsoluteMergeProperty; - } - /** * Calculate all cells that need to be merged * diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java index f5984a1e..52b1cc04 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java @@ -21,7 +21,7 @@ public class FillStyleAnnotatedData { @ContentFontStyle(bold = BooleanEnum.TRUE, color = 16) private Double number; @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 17) - @ContentFontStyle(bold = BooleanEnum.TRUE, color = 18) + @ContentFontStyle(bold = BooleanEnum.TRUE, color = 58) private Date date; @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 12) @ContentFontStyle(bold = BooleanEnum.TRUE, color = 18) diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java index d1d45322..13367904 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedTest.java @@ -63,26 +63,26 @@ public class FillStyleAnnotatedTest { XSSFCell cell0 = row.getCell(0); Assert.assertEquals("张三", cell0.getStringCellValue()); Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("FF00B050", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF7030A0", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assert.assertEquals("FFFFFF00", cell0.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assert.assertEquals("FF808000", cell0.getCellStyle().getFont().getXSSFColor().getARGBHex()); Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); + Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertFalse(cell1.getCellStyle().getFont().getBold()); + Assert.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assert.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assert.assertTrue(cell1.getCellStyle().getFont().getBold()); XSSFCell cell2 = row.getCell(2); Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("FFFFC000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FFC0504D", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assert.assertEquals("FF008000", cell2.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assert.assertEquals("FF003300", cell2.getCellStyle().getFont().getXSSFColor().getARGBHex()); Assert.assertTrue(cell2.getCellStyle().getFont().getBold()); XSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); @@ -116,28 +116,28 @@ public class FillStyleAnnotatedTest { HSSFCell cell0 = row.getCell(0); Assert.assertEquals("张三", cell0.getStringCellValue()); Assert.assertEquals(49, cell0.getCellStyle().getDataFormat()); - Assert.assertEquals("0:8080:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:0:8080", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assert.assertEquals("FFFF:FFFF:0", cell0.getCellStyle().getFillForegroundColorColor().getHexString()); + Assert.assertEquals("8080:8080:0", cell0.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); + Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); - Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assert.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); + Assert.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertFalse(cell1.getCellStyle().getFont(workbook).getBold()); + Assert.assertTrue(cell1.getCellStyle().getFont(workbook).getBold()); HSSFCell cell2 = row.getCell(2); Assert.assertEquals("2020-01-01 01:01:01", DateUtils.format(cell2.getDateCellValue(), "yyyy-MM-dd HH:mm:ss")); Assert.assertEquals("yyyy-MM-dd HH:mm:ss", cell2.getCellStyle().getDataFormatString()); - Assert.assertEquals("FFFF:CCCC:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:0:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString()); + Assert.assertEquals("0:8080:0", cell2.getCellStyle().getFillForegroundColorColor().getHexString()); + Assert.assertEquals("0:3333:0", cell2.getCellStyle().getFont(workbook).getHSSFColor(workbook).getHexString()); Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); HSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java index e8af2e34..8e418549 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleDataTest.java @@ -72,7 +72,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); + Assert.assertEquals("5.2", cell1.getStringCellValue()); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals("FF92D050", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); Assert.assertEquals("FF4BACC6", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); @@ -86,7 +86,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell2.getCellStyle().getFont().getBold()); XSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); @@ -126,7 +126,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); + Assert.assertEquals("5.2", cell1.getStringCellValue()); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals("9999:CCCC:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); Assert.assertEquals("0:8080:8080", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) @@ -141,7 +141,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); HSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) @@ -187,7 +187,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell0.getCellStyle().getFont().getBold()); XSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); + Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals("FFFF0000", cell1.getCellStyle().getFillForegroundColorColor().getARGBHex()); Assert.assertEquals("FF800000", cell1.getCellStyle().getFont().getXSSFColor().getARGBHex()); @@ -201,28 +201,27 @@ public class FillStyleDataTest { Assert.assertTrue(cell2.getCellStyle().getFont().getBold()); XSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("FF0000FF", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF000080", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assert.assertEquals("FFFF0000", cell3.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assert.assertEquals("FFEEECE1", cell3.getCellStyle().getFont().getXSSFColor().getARGBHex()); Assert.assertTrue(cell3.getCellStyle().getFont().getBold()); XSSFCell cell4 = row.getCell(4); Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFFFF00", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF808000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell4.getCellStyle().getFont().getBold()); + Assert.assertEquals("FFC00000", cell4.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assert.assertEquals("FF000000", cell4.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assert.assertFalse(cell4.getCellStyle().getFont().getBold()); XSSFCell cell5 = row.getCell(5); Assert.assertEquals("空", cell5.getStringCellValue()); Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("FF008080", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); - Assert.assertEquals("FF003366", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); - Assert.assertTrue(cell5.getCellStyle().getFont().getBold()); + Assert.assertEquals("FFF79646", cell5.getCellStyle().getFillForegroundColorColor().getARGBHex()); + Assert.assertEquals("FF8064A2", cell5.getCellStyle().getFont().getXSSFColor().getARGBHex()); + Assert.assertFalse(cell5.getCellStyle().getFont().getBold()); } - @Test public void t12FillStyleHandler03() throws Exception { fillStyleHandler(fileStyleHandler03, fileStyleTemplate03); @@ -242,7 +241,7 @@ public class FillStyleDataTest { Assert.assertTrue(cell0.getCellStyle().getFont(workbook).getBold()); HSSFCell cell1 = row.getCell(1); - Assert.assertEquals("5", cell1.getStringCellValue()); + Assert.assertEquals(5.2, cell1.getNumericCellValue(), 1); Assert.assertEquals(0, cell1.getCellStyle().getDataFormat()); Assert.assertEquals("FFFF:0:0", cell1.getCellStyle().getFillForegroundColorColor().getHexString()); Assert.assertEquals("8080:0:0", cell1.getCellStyle().getFont(workbook).getHSSFColor(workbook) @@ -257,32 +256,32 @@ public class FillStyleDataTest { Assert.assertTrue(cell2.getCellStyle().getFont(workbook).getBold()); HSSFCell cell3 = row.getCell(3); - Assert.assertEquals("张三今年5岁了", cell3.getStringCellValue()); + Assert.assertEquals("张三今年5.2岁了", cell3.getStringCellValue()); Assert.assertEquals(0, cell3.getCellStyle().getDataFormat()); - Assert.assertEquals("0:0:FFFF", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:0:8080", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assert.assertEquals("FFFF:0:0", cell3.getCellStyle().getFillForegroundColorColor().getHexString()); + Assert.assertEquals("FFFF:FFFF:9999", cell3.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); Assert.assertTrue(cell3.getCellStyle().getFont(workbook).getBold()); HSSFCell cell4 = row.getCell(4); Assert.assertEquals("{.name}忽略,张三", cell4.getStringCellValue()); Assert.assertEquals(0, cell4.getCellStyle().getDataFormat()); - Assert.assertEquals("FFFF:FFFF:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("8080:8080:0", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assert.assertEquals("9999:3333:0", cell4.getCellStyle().getFillForegroundColorColor().getHexString()); + Assert.assertEquals("3333:3333:3333", cell4.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell4.getCellStyle().getFont(workbook).getBold()); + Assert.assertFalse(cell4.getCellStyle().getFont(workbook).getBold()); HSSFCell cell5 = row.getCell(5); Assert.assertEquals("空", cell5.getStringCellValue()); Assert.assertEquals(0, cell5.getCellStyle().getDataFormat()); - Assert.assertEquals("0:8080:8080", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); - Assert.assertEquals("0:3333:6666", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) + Assert.assertEquals("9999:3333:0", cell5.getCellStyle().getFillForegroundColorColor().getHexString()); + Assert.assertEquals("CCCC:9999:FFFF", cell5.getCellStyle().getFont(workbook).getHSSFColor(workbook) .getHexString()); - Assert.assertTrue(cell5.getCellStyle().getFont(workbook).getBold()); + Assert.assertFalse(cell5.getCellStyle().getFont(workbook).getBold()); } private void fillStyleHandler(File file, File template) throws Exception { - EasyExcel.write(file, FillData.class).withTemplate(template).sheet() + EasyExcel.write(file, FillStyleData.class).withTemplate(template).sheet() .registerWriteHandler(new AbstractVerticalCellStyleStrategy() { @Override diff --git a/src/test/resources/fill/style.xls b/src/test/resources/fill/style.xls index 9e7ca73971419c9ef6aba3643a8b5fbc236ff734..3127743c0d0dd5655d3f7bf37dc55265c9f491b6 100644 GIT binary patch delta 2791 zcmchZTWl0n7=ZsXvpe0grOe*D+bz2!8cU0GyG^4BwcVDTrVkc~D8b0U1;@Xqy;Fniyh}MtH!a8sB{I61+X=`k!<5On1B8Ci=j6 zm^tVBujfDiT#nvmM{l!B4H}zs)@si%JfSC@hdtvAb9xfPx|x`t-nG3G!f*%b;6Avf z0WdfQ8k?WSX+5gZ?H@gwE!S$V&{Ea_eK5{|M#=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C_N#vFIsn`)$fKIB%uV2| zmWt0!j6YO_1Y8Fn?1rxu0A|+IkC@rib}9I)Fa5thPX^L8M&*9n&;gdo;FsrvLX#?O zCLFBP{&2}8S)nW6mTsRFA&61tSj<;rMEB{=?hh;@-ZSIqNWRrVKA`cG}@iy-lJRtdD887(LKvQ_zh90pYw1u|_ z-U3A{epDuk7H0ejn2`+%JPc>M;&+-ST4O2}be)y4sCTmgS_}A8PLEV5N`)}pK^0iT zj}ONZetOHo{EuTBcr>1E9drcr#PLcpDf|rp;g3`u{>!62N{~`nBY^vo?UB6>A8Tkl zIlh(C)W+uCA*a8o*#6DjF~j!rmZrNu+`wigslw72u4P~3=fSJ#NBFlnpW4r`FWrlC zaUIv&416JH;BdZ<7cx4Y&n;kIrjFm|b-a}A#pgS8{2{Y|$Fn=Ii~KKl)Nwpp#DnQ4 zFqzQtr@Vn*Mh$!{Zs2;tz(Tu$H%Tv&?n@dtNxB*{ut|EB^eNJ>k={wM-;!PG(ZP@%r0QeZE=-KA1tC@%{0Gz>g2D1IK6(x8fWR4W;;NX@7*vedVE z*}xiCI8*IrD|n{brra1b-yC{{(QUCAJ8ix(G)u?~4c}n%BbWRy)&KsZah0j&m63ZQ TI6dVxmB)XNv5NWEl#l%b!!*zK diff --git a/src/test/resources/fill/style.xlsx b/src/test/resources/fill/style.xlsx index 234c3b30852827f436ef2d7157903f8b69f8cda4..062540d0d8ef0a5cfb0ee83b76e798e468bdac96 100644 GIT binary patch delta 3268 zcmZ{ncRU;18po3up=y@b9w{McP^*L1sK(wzYZWCmV$`Nc?X5Iul~?T1zSFws=>FFjF=#DNjH#VG+VF|-kpRyd>ARA;GT{Nj4LB6WiALrVNOIlWp z5qXb}t-W=;|NE#YO#_(L*zL%Ba&TcT)ZcJ$;`ZhZI`<-p3mQ-8b~Jj-CHkBqq%itF zfICS}PQBjd3DoYGD76RegE#{VorMJnKMGLQg)RNARRq#lWYe_>T!hJTDa z7H>^&hP58zX+3X!x(^p9`fT5&qe*e(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1gw)Q3wDxzT1PCV9_3XvKW$u#%398tG67dG-g=^)y=%kb}aa%yD4 z!R?0Zrqz%zz+Z$inNRCS)^&?=FkH0CP#{5gnV#e)#)RY?geb>e-1ThjOC~Y9q~XU5 zWczMgi{GxFy&Lc8m(`>6xN-C6cWro zOkpR@TY4 z9NGm*!KNPWzk78nr><%>n=9n1<0Ywlo}73dq*7hwzzci@Z_baljM+z;w#8y-4I&I? zNmX|j(>*_)#0I_rvFK8#qI|`SKB#VcaOCf+oVVt4Uee%NUqrr~pU$WT%qP2eawK4^ z#Lv=zgK?)yWDsdQqpeLr0P}=mAcq{fG}=Akz4dX3IzZN{+<5&3>5m6#E;`q;ukrF#ON1CoWG=u z?|+i2aBwAg=HoVt;tCb{8KuO37vYs)4B@yQB{`AfUYq|awfXh0_~p0$E2?o|Wqk%H N&O%v+1*C9%@lS#4+|mF5 diff --git a/update.md b/update.md index ca31cb5b..70e11c8c 100644 --- a/update.md +++ b/update.md @@ -2,6 +2,7 @@ * 升级到正式版 * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124) * 修复填充数据为空 可能NPE的bug +* 修复填充样式可能不生效bug # 3.0.0-beta3