> orderFiledMap,
diff --git a/src/main/java/com/alibaba/excel/util/FieldUtils.java b/src/main/java/com/alibaba/excel/util/FieldUtils.java
index a5c75731..367c6e55 100644
--- a/src/main/java/com/alibaba/excel/util/FieldUtils.java
+++ b/src/main/java/com/alibaba/excel/util/FieldUtils.java
@@ -5,7 +5,6 @@ import java.lang.reflect.Modifier;
import java.util.Map;
import com.alibaba.excel.metadata.NullObject;
-import com.alibaba.excel.write.metadata.RowData;
import net.sf.cglib.beans.BeanMap;
@@ -20,19 +19,17 @@ public class FieldUtils {
private static final int START_RESOLVE_FIELD_LENGTH = 2;
- public static Class> getFieldClass(Map dataMap, String filedName) {
+ public static Class> getFieldClass(Map dataMap, String filedName, Object value) {
if (dataMap instanceof BeanMap) {
- return ((BeanMap)dataMap).getPropertyType(filedName);
- }
- Object value = dataMap.get(filedName);
- if (value != null) {
- return value.getClass();
+ Class> fieldClass = ((BeanMap)dataMap).getPropertyType(filedName);
+ if (fieldClass != null) {
+ return fieldClass;
+ }
}
- return nullObjectClass;
+ return getFieldClass(value);
}
- public static Class> getFieldClass(RowData rowData, int dataIndex) {
- Object value = rowData.get(dataIndex);
+ public static Class> getFieldClass(Object value) {
if (value != null) {
return value.getClass();
}
diff --git a/src/main/java/com/alibaba/excel/util/ListUtils.java b/src/main/java/com/alibaba/excel/util/ListUtils.java
index e64acb36..1a3451d8 100644
--- a/src/main/java/com/alibaba/excel/util/ListUtils.java
+++ b/src/main/java/com/alibaba/excel/util/ListUtils.java
@@ -10,10 +10,21 @@ import lombok.NonNull;
import org.apache.commons.compress.utils.Iterators;
/**
- * List utils
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
- * @author Jiaju Zhuang
- **/
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @author Apache Software Foundation (ASF)
+ */
public class ListUtils {
private ListUtils() {}
diff --git a/src/main/java/com/alibaba/excel/util/MapUtils.java b/src/main/java/com/alibaba/excel/util/MapUtils.java
index 7d049e9d..6039fbde 100644
--- a/src/main/java/com/alibaba/excel/util/MapUtils.java
+++ b/src/main/java/com/alibaba/excel/util/MapUtils.java
@@ -5,10 +5,21 @@ import java.util.LinkedHashMap;
import java.util.TreeMap;
/**
- * Map utils
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
- * @author Jiaju Zhuang
- **/
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @author Apache Software Foundation (ASF)
+ */
public class MapUtils {
private MapUtils() {}
diff --git a/src/main/java/com/alibaba/excel/util/NumberUtils.java b/src/main/java/com/alibaba/excel/util/NumberUtils.java
index 4226e967..c467762c 100644
--- a/src/main/java/com/alibaba/excel/util/NumberUtils.java
+++ b/src/main/java/com/alibaba/excel/util/NumberUtils.java
@@ -186,4 +186,5 @@ public class NumberUtils {
decimalFormat.setParseBigDecimal(true);
return decimalFormat.parse(string);
}
+
}
diff --git a/src/main/java/com/alibaba/excel/util/StringUtils.java b/src/main/java/com/alibaba/excel/util/StringUtils.java
index 40b77d52..b076370c 100644
--- a/src/main/java/com/alibaba/excel/util/StringUtils.java
+++ b/src/main/java/com/alibaba/excel/util/StringUtils.java
@@ -1,5 +1,6 @@
package com.alibaba.excel.util;
-/*
+
+/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
@@ -12,8 +13,9 @@ package com.alibaba.excel.util;
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
+ * @author Apache Software Foundation (ASF)
*/
-
public class StringUtils {
private StringUtils() {}
@@ -194,7 +196,6 @@ public class StringUtils {
return true;
}
-
/**
* Checks if the CharSequence contains only Unicode digits.
* A decimal point is not a Unicode digit and returns false.
@@ -221,7 +222,7 @@ public class StringUtils {
* StringUtils.isNumeric("+123") = false
*
*
- * @param cs the CharSequence to check, may be null
+ * @param cs the CharSequence to check, may be null
* @return {@code true} if only contains digits, and is non-null
* @since 3.0 Changed signature from isNumeric(String) to isNumeric(CharSequence)
* @since 3.0 Changed "" to return false and not true
diff --git a/src/main/java/com/alibaba/excel/util/StyleUtil.java b/src/main/java/com/alibaba/excel/util/StyleUtil.java
index 70c7ac7a..1bafcdc8 100644
--- a/src/main/java/com/alibaba/excel/util/StyleUtil.java
+++ b/src/main/java/com/alibaba/excel/util/StyleUtil.java
@@ -10,8 +10,10 @@ import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
+import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.common.usermodel.HyperlinkType;
+import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
@@ -19,11 +21,14 @@ import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.Units;
+import org.apache.poi.xssf.usermodel.XSSFColor;
+import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
/**
* @author jipengfei
*/
+@Slf4j
public class StyleUtil {
private StyleUtil() {}
@@ -32,11 +37,16 @@ public class StyleUtil {
* Build cell style
*
* @param workbook
+ * @param originCellStyle
* @param writeCellStyle
* @return
*/
- public static CellStyle buildCellStyle(Workbook workbook, WriteCellStyle writeCellStyle) {
+ public static CellStyle buildCellStyle(Workbook workbook, CellStyle originCellStyle,
+ WriteCellStyle writeCellStyle) {
CellStyle cellStyle = workbook.createCellStyle();
+ if (originCellStyle != null) {
+ cellStyle.cloneStyleFrom(originCellStyle);
+ }
if (writeCellStyle == null) {
return cellStyle;
}
@@ -115,14 +125,23 @@ public class StyleUtil {
return dataFormatData.getIndex();
}
if (StringUtils.isNotBlank(dataFormatData.getFormat())) {
+ if (log.isDebugEnabled()) {
+ log.info("create new data fromat:{}", dataFormatData);
+ }
DataFormat dataFormatCreate = workbook.createDataFormat();
return dataFormatCreate.getFormat(dataFormatData.getFormat());
}
return BuiltinFormats.GENERAL;
}
- public static Font buildFont(Workbook workbook, WriteFont writeFont) {
- Font font = workbook.createFont();
+ public static Font buildFont(Workbook workbook, Font originFont, WriteFont writeFont) {
+ if (log.isDebugEnabled()) {
+ log.info("create new font:{},{}", writeFont, originFont);
+ }
+ if (writeFont == null && originFont == null) {
+ return null;
+ }
+ Font font = createFont(workbook, originFont, writeFont);
if (writeFont == null || font == null) {
return font;
}
@@ -156,6 +175,44 @@ public class StyleUtil {
return font;
}
+ private static Font createFont(Workbook workbook, Font originFont, WriteFont writeFont) {
+ Font font = workbook.createFont();
+ if (originFont == null) {
+ return font;
+ }
+ if (originFont instanceof XSSFFont) {
+ XSSFFont xssfFont = (XSSFFont)font;
+ XSSFFont xssfOriginFont = ((XSSFFont)originFont);
+ xssfFont.setFontName(xssfOriginFont.getFontName());
+ xssfFont.setFontHeightInPoints(xssfOriginFont.getFontHeightInPoints());
+ xssfFont.setItalic(xssfOriginFont.getItalic());
+ xssfFont.setStrikeout(xssfOriginFont.getStrikeout());
+ // Colors cannot be overwritten
+ if (writeFont == null || writeFont.getColor() == null) {
+ xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null));
+ }
+ xssfFont.setTypeOffset(xssfOriginFont.getTypeOffset());
+ xssfFont.setUnderline(xssfOriginFont.getUnderline());
+ xssfFont.setCharSet(xssfOriginFont.getCharSet());
+ xssfFont.setBold(xssfOriginFont.getBold());
+ return xssfFont;
+ } else if (originFont instanceof HSSFFont) {
+ HSSFFont hssfFont = (HSSFFont)font;
+ HSSFFont hssfOriginFont = (HSSFFont)originFont;
+ hssfFont.setFontName(hssfOriginFont.getFontName());
+ hssfFont.setFontHeightInPoints(hssfOriginFont.getFontHeightInPoints());
+ hssfFont.setItalic(hssfOriginFont.getItalic());
+ hssfFont.setStrikeout(hssfOriginFont.getStrikeout());
+ hssfFont.setColor(hssfOriginFont.getColor());
+ hssfFont.setTypeOffset(hssfOriginFont.getTypeOffset());
+ hssfFont.setUnderline(hssfOriginFont.getUnderline());
+ hssfFont.setCharSet(hssfOriginFont.getCharSet());
+ hssfFont.setBold(hssfOriginFont.getBold());
+ return hssfFont;
+ }
+ return font;
+ }
+
public static RichTextString buildRichTextString(WriteWorkbookHolder writeWorkbookHolder,
RichTextStringData richTextStringData) {
if (richTextStringData == null) {
@@ -168,12 +225,12 @@ public class StyleUtil {
richTextString = new HSSFRichTextString(richTextStringData.getTextString());
}
if (richTextStringData.getWriteFont() != null) {
- richTextString.applyFont(writeWorkbookHolder.createFont(richTextStringData.getWriteFont()));
+ richTextString.applyFont(writeWorkbookHolder.createFont(richTextStringData.getWriteFont(), null, true));
}
if (CollectionUtils.isNotEmpty(richTextStringData.getIntervalFontList())) {
for (IntervalFont intervalFont : richTextStringData.getIntervalFontList()) {
richTextString.applyFont(intervalFont.getStartIndex(), intervalFont.getEndIndex(),
- writeWorkbookHolder.createFont(intervalFont.getWriteFont()));
+ writeWorkbookHolder.createFont(intervalFont.getWriteFont(), null, true));
}
}
return richTextString;
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 5cd0cda2..597d94de 100644
--- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
@@ -23,12 +23,10 @@ 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.metadata.style.WriteCellStyle;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
@@ -51,14 +49,15 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
protected WriteCellData> converterAndSet(WriteHolder currentWriteHolder, Class> clazz,
CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head,
- Integer relativeRowIndex) {
+ 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, head, relativeRowIndex, Boolean.FALSE);
+ WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE,
+ excelContentProperty);
// Fill in picture information
fillImage(cell, cellData.getImageDataList());
@@ -72,12 +71,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
// Fill in formula information
fillFormula(cell, cellData.getFormulaData());
- // Fill in style information
- fillStyle(cell, cellData.getWriteCellStyle());
-
// Fill index
- cellData.setRowIndex(cell.getRowIndex());
- cellData.setColumnIndex(cell.getColumnIndex());
+ cellData.setRowIndex(rowIndex);
+ cellData.setColumnIndex(columnIndex);
if (cellData.getType() == null) {
cellData.setType(CellDataTypeEnum.EMPTY);
@@ -109,14 +105,6 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
}
- private void fillStyle(Cell cell, WriteCellStyle writeCellStyle) {
- if (writeCellStyle == null) {
- return;
- }
- CellStyle cellStyle = writeContext.writeWorkbookHolder().createCellStyle(writeCellStyle);
- cell.setCellStyle(cellStyle);
- }
-
private void fillFormula(Cell cell, FormulaData formulaData) {
if (formulaData == null) {
return;
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 e053d81f..bdfea49e 100644
--- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
@@ -59,26 +59,26 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
}
}
- private void addOneRowOfDataToExcel(Object oneRowData, int n, int relativeRowIndex,
+ private void addOneRowOfDataToExcel(Object oneRowData, int rowIndex, int relativeRowIndex,
Map sortedAllFiledMap) {
if (oneRowData == null) {
return;
}
- WriteHandlerUtils.beforeRowCreate(writeContext, n, relativeRowIndex, Boolean.FALSE);
- Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), n);
+ WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE);
+ Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex);
WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE);
if (oneRowData instanceof Collection>) {
- addBasicTypeToExcel(new CollectionRowData((Collection>)oneRowData), row, relativeRowIndex);
+ addBasicTypeToExcel(new CollectionRowData((Collection>)oneRowData), row, rowIndex, relativeRowIndex);
} else if (oneRowData instanceof Map) {
- addBasicTypeToExcel(new MapRowData((Map)oneRowData), row, relativeRowIndex);
+ addBasicTypeToExcel(new MapRowData((Map)oneRowData), row, rowIndex, relativeRowIndex);
} else {
- addJavaObjectToExcel(oneRowData, row, relativeRowIndex, sortedAllFiledMap);
+ addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap);
}
WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE);
}
- private void addBasicTypeToExcel(RowData oneRowData, Row row, int relativeRowIndex) {
+ private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) {
if (oneRowData.isEmpty()) {
return;
}
@@ -89,10 +89,10 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
if (dataIndex >= oneRowData.size()) {
return;
}
- int cellIndex = entry.getKey();
+ int columnIndex = entry.getKey();
Head head = entry.getValue();
- doAddBasicTypeToExcel(oneRowData, head, row, relativeRowIndex, dataIndex++, cellIndex);
- maxCellIndex = Math.max(maxCellIndex, cellIndex);
+ doAddBasicTypeToExcel(oneRowData, head, row, rowIndex, relativeRowIndex, dataIndex++, columnIndex);
+ maxCellIndex = Math.max(maxCellIndex, columnIndex);
}
// Finish
if (dataIndex >= oneRowData.size()) {
@@ -104,22 +104,29 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
int size = oneRowData.size() - dataIndex;
for (int i = 0; i < size; i++) {
- doAddBasicTypeToExcel(oneRowData, null, row, relativeRowIndex, dataIndex++, maxCellIndex++);
+ doAddBasicTypeToExcel(oneRowData, null, row, rowIndex, relativeRowIndex, dataIndex++, maxCellIndex++);
}
}
- private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int relativeRowIndex, int dataIndex,
- int cellIndex) {
- WriteHandlerUtils.beforeCellCreate(writeContext, row, head, cellIndex, relativeRowIndex, Boolean.FALSE);
- Cell cell = WorkBookUtil.createCell(row, cellIndex);
- WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE);
+ private void doAddBasicTypeToExcel(RowData oneRowData, Head head, Row row, int rowIndex, int relativeRowIndex,
+ int dataIndex, int columnIndex) {
+ ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null,
+ writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(),
+ head == null ? null : head.getFieldName());
+
+ WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE,
+ excelContentProperty);
+ Cell cell = WorkBookUtil.createCell(row, columnIndex);
+ WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE,
+ excelContentProperty);
Object value = oneRowData.get(dataIndex);
WriteCellData> cellData = converterAndSet(writeContext.currentWriteHolder(),
- FieldUtils.getFieldClass(oneRowData, dataIndex), null, cell, value, null, head, relativeRowIndex);
- WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
+ FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex, rowIndex, columnIndex);
+ WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE,
+ excelContentProperty);
}
- private void addJavaObjectToExcel(Object oneRowData, Row row, int relativeRowIndex,
+ private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex,
Map sortedAllFiledMap) {
WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
BeanMap beanMap = BeanMapUtils.create(oneRowData);
@@ -128,26 +135,27 @@ 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()) {
- int cellIndex = entry.getKey();
- ExcelContentProperty excelContentProperty = entry.getValue();
- String name = FieldUtils.resolveCglibFieldName(excelContentProperty.getField());
+ for (Map.Entry entry : headMap.entrySet()) {
+ int columnIndex = entry.getKey();
+ 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);
- Cell cell = WorkBookUtil.createCell(row, cellIndex);
- WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE);
+ 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, head, relativeRowIndex, Boolean.FALSE,
+ excelContentProperty);
Object value = beanMap.get(name);
- WriteCellData> cellData = converterAndSet(currentWriteHolder,
- excelContentProperty.getField().getType(),
- null, cell, value, excelContentProperty, head, relativeRowIndex);
- WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE);
+ WriteCellData> cellData = converterAndSet(currentWriteHolder, head.getField().getType(),
+ null, cell, value, excelContentProperty, head, relativeRowIndex, rowIndex, columnIndex);
+ WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE,
+ excelContentProperty);
beanMapHandledSet.add(name);
- maxCellIndex = Math.max(maxCellIndex, cellIndex);
+ maxCellIndex = Math.max(maxCellIndex, columnIndex);
}
}
// Finish
@@ -160,21 +168,27 @@ 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), null, cell, value, null, null, relativeRowIndex);
- WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);
+ FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex,
+ rowIndex, maxCellIndex);
+ 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 e91e2834..2fb755a7 100644
--- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
+++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
@@ -7,6 +7,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import com.alibaba.excel.context.WriteContext;
@@ -17,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;
@@ -188,33 +190,49 @@ 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),
- null, cell, value, fieldNameContentPropertyMap.get(variable), null, relativeRowIndex);
- WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE);
+ FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null,
+ relativeRowIndex, analysisCell.getRowIndex(), analysisCell.getColumnIndex());
+ cellData.setAnalysisCell(analysisCell);
+
+ // Restyle
+ if (fillConfig.getAutoStyle()) {
+ Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag))
+ .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);
+
for (String variable : analysisCell.getVariableList()) {
cellValueBuild.append(analysisCell.getPrepareDataList().get(index++));
if (!dataMap.containsKey(variable)) {
continue;
}
Object value = dataMap.get(variable);
- WriteCellData> cellData = convert(writeSheetHolder, value == null ? null : value.getClass(),
- CellDataTypeEnum.STRING, cell, value, fieldNameContentPropertyMap.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);
cellDataList.add(cellData);
CellDataTypeEnum type = cellData.getType();
if (type != null) {
@@ -235,8 +253,16 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
cellValueBuild.append(analysisCell.getPrepareDataList().get(index));
cell.setCellValue(cellValueBuild.toString());
+
+ // Restyle
+ if (fillConfig.getAutoStyle()) {
+ Optional.ofNullable(collectionFieldStyleCache.get(currentUniqueDataFlag))
+ .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell))
+ .ifPresent(cell::setCellStyle);
+ }
+
WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, relativeRowIndex,
- Boolean.FALSE);
+ Boolean.FALSE, ExcelContentProperty.EMPTY);
}
}
}
@@ -252,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());
@@ -293,32 +320,25 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
}
Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell);
- Cell cell = createCellIfNecessary(row, lastColumnIndex);
-
- Map collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent(
- currentUniqueDataFlag, key -> MapUtils.newHashMap());
+ Cell cell = createCellIfNecessary(row, lastColumnIndex, excelContentProperty);
if (isOriginalCell) {
+ Map collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent(
+ currentUniqueDataFlag, key -> MapUtils.newHashMap());
collectionFieldStyleMap.put(analysisCell, cell.getCellStyle());
- } else {
- if (fillConfig.getAutoStyle()) {
- CellStyle cellStyle = collectionFieldStyleMap.get(analysisCell);
- if (cellStyle != null) {
- cell.setCellStyle(cellStyle);
- }
- }
}
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 9956ea19..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
@@ -11,6 +11,7 @@ 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;
/**
* fill cell style.
@@ -28,16 +29,20 @@ public class FillStyleCellWriteHandler implements CellWriteHandler {
@Override
public void afterCellDispose(CellWriteHandlerContext context) {
List> cellDataList = context.getCellDataList();
- if (CollectionUtils.isEmpty(cellDataList) || cellDataList.size() > 1) {
+ 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();
- if (writeCellStyle == null) {
+ CellStyle originCellStyle = cellData.getOriginCellStyle();
+ if (writeCellStyle == null && originCellStyle == null) {
return;
}
WriteWorkbookHolder writeWorkbookHolder = context.getWriteWorkbookHolder();
- context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle));
+ context.getCell().setCellStyle(writeWorkbookHolder.createCellStyle(writeCellStyle, originCellStyle));
}
}
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 d6921681..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;
@@ -27,6 +28,7 @@ import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
+import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
import com.alibaba.excel.write.metadata.WriteBasicParameter;
@@ -77,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.
*/
@@ -194,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);
}
}
@@ -215,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);
@@ -226,9 +228,9 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
dealColumnWidth(handlerList);
}
- if (hasStyle) {
+ //if (hasStyle) {
dealStyle(handlerList);
- }
+ //}
dealRowHigh(handlerList);
dealOnceAbsoluteMerge(handlerList);
@@ -242,13 +244,17 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
}
@Override
- protected WriteCellStyle headCellStyle(Head head) {
- return WriteCellStyle.build(head.getHeadStyleProperty(), head.getHeadFontProperty());
+ protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
+ ExcelContentProperty excelContentProperty = context.getExcelContentProperty();
+ return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(),
+ excelContentProperty.getContentFontProperty());
}
@Override
- protected WriteCellStyle contentCellStyle(Head head) {
- return WriteCellStyle.build(head.getContentStyleProperty(), head.getContentFontProperty());
+ protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
+ ExcelContentProperty excelContentProperty = context.getExcelContentProperty();
+ return WriteCellStyle.build(excelContentProperty.getContentStyleProperty(),
+ excelContentProperty.getContentFontProperty());
}
};
handlerList.add(styleStrategy);
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 85e15275..374b5b7a 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
@@ -24,11 +24,14 @@ import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
@@ -37,6 +40,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
* @author Jiaju Zhuang
*/
@Data
+@Slf4j
public class WriteWorkbookHolder extends AbstractWriteHolder {
/***
* Current poi Workbook.This is only for writing, and there may be no data in version 07 when template data needs to
@@ -129,7 +133,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
/**
* Used to cell style.
*/
- private Map cellStyleMap;
+ private Map> cellStyleIndexMap;
/**
* Used to font.
*/
@@ -206,7 +210,7 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
} else {
this.writeExcelOnException = writeWorkbook.getWriteExcelOnException();
}
- this.cellStyleMap = MapUtils.newHashMap();
+ this.cellStyleIndexMap = MapUtils.newHashMap();
this.fontMap = MapUtils.newHashMap();
this.dataFormatMap = MapUtils.newHashMap();
}
@@ -242,17 +246,48 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* create a cell style.
*
* @param writeCellStyle
+ * @param originCellStyle
* @return
*/
- public CellStyle createCellStyle(WriteCellStyle writeCellStyle) {
- CellStyle cellStyle = cellStyleMap.get(writeCellStyle);
+ public CellStyle createCellStyle(WriteCellStyle writeCellStyle, CellStyle originCellStyle) {
+ if (writeCellStyle == null) {
+ return originCellStyle;
+ }
+ WriteCellStyle tempWriteCellStyle = new WriteCellStyle();
+ WriteCellStyle.merge(writeCellStyle, tempWriteCellStyle);
+
+ short styleIndex = -1;
+ Font originFont = null;
+ boolean useCache = true;
+ if (originCellStyle != null) {
+ styleIndex = originCellStyle.getIndex();
+ if (originCellStyle instanceof XSSFCellStyle) {
+ originFont = ((XSSFCellStyle)originCellStyle).getFont();
+ } else if (originCellStyle instanceof HSSFCellStyle) {
+ originFont = ((HSSFCellStyle)originCellStyle).getFont(workbook);
+ }
+ useCache = false;
+ }
+
+ Map cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex,
+ key -> MapUtils.newHashMap());
+ CellStyle cellStyle = cellStyleMap.get(tempWriteCellStyle);
if (cellStyle != null) {
return cellStyle;
}
- cellStyle = StyleUtil.buildCellStyle(workbook, writeCellStyle);
- cellStyle.setDataFormat(createDataFormat(writeCellStyle.getDataFormatData()));
- cellStyle.setFont(createFont(writeCellStyle.getWriteFont()));
- cellStyleMap.put(writeCellStyle, cellStyle);
+ if (log.isDebugEnabled()) {
+ log.info("create new style:{},{}", tempWriteCellStyle, originCellStyle);
+ }
+ cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle);
+ Short dataFormat = createDataFormat(tempWriteCellStyle.getDataFormatData(), useCache);
+ if (dataFormat != null) {
+ cellStyle.setDataFormat(dataFormat);
+ }
+ Font font = createFont(tempWriteCellStyle.getWriteFont(), originFont, useCache);
+ if (font != null) {
+ cellStyle.setFont(font);
+ }
+ cellStyleMap.put(tempWriteCellStyle, cellStyle);
return cellStyle;
}
@@ -260,15 +295,23 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* create a font.
*
* @param writeFont
+ * @param originFont
+ * @param useCache
* @return
*/
- public Font createFont(WriteFont writeFont) {
- Font font = fontMap.get(writeFont);
+ public Font createFont(WriteFont writeFont, Font originFont, boolean useCache) {
+ if (!useCache) {
+ return StyleUtil.buildFont(workbook, originFont, writeFont);
+ }
+ WriteFont tempWriteFont = new WriteFont();
+ WriteFont.merge(writeFont, tempWriteFont);
+
+ Font font = fontMap.get(tempWriteFont);
if (font != null) {
return font;
}
- font = StyleUtil.buildFont(workbook, writeFont);
- fontMap.put(writeFont, font);
+ font = StyleUtil.buildFont(workbook, originFont, tempWriteFont);
+ fontMap.put(tempWriteFont, font);
return font;
}
@@ -276,15 +319,25 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
* create a data format.
*
* @param dataFormatData
+ * @param useCache
* @return
*/
- public Short createDataFormat(DataFormatData dataFormatData) {
- Short dataFormat = dataFormatMap.get(dataFormatData);
+ public Short createDataFormat(DataFormatData dataFormatData, boolean useCache) {
+ if (dataFormatData == null) {
+ return null;
+ }
+ if (!useCache) {
+ return StyleUtil.buildDataFormat(workbook, dataFormatData);
+ }
+ DataFormatData tempDataFormatData = new DataFormatData();
+ DataFormatData.merge(dataFormatData, tempDataFormatData);
+
+ Short dataFormat = dataFormatMap.get(tempDataFormatData);
if (dataFormat != null) {
return dataFormat;
}
- dataFormat = StyleUtil.buildDataFormat(workbook, dataFormatData);
- dataFormatMap.put(dataFormatData, dataFormat);
+ dataFormat = StyleUtil.buildDataFormat(workbook, tempDataFormatData);
+ dataFormatMap.put(tempDataFormatData, dataFormat);
return dataFormat;
}
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..7f52cd1d 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,42 @@ 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()) {
- Integer index = entry.getKey();
- ExcelContentProperty excelContentPropertyData = entry.getValue();
- if (excelContentPropertyData == null) {
+ for (Map.Entry entry : getHeadMap().entrySet()) {
+ 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 +73,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/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java
index e37987bb..f9d76f7f 100644
--- a/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java
+++ b/src/main/java/com/alibaba/excel/write/style/AbstractVerticalCellStyleStrategy.java
@@ -18,12 +18,12 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
return;
}
WriteCellData> cellData = context.getFirstCellData();
- WriteCellStyle.merge(headCellStyle(context.getHeadData()), cellData.getOrCreateStyle());
+ WriteCellStyle.merge(headCellStyle(context), cellData.getOrCreateStyle());
}
@Override
protected void setContentCellStyle(CellWriteHandlerContext context) {
- if (stopProcessing(context)) {
+ if (context.getFirstCellData() == null) {
return;
}
WriteCellData> cellData = context.getFirstCellData();
@@ -31,13 +31,13 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
}
/**
- * Returns the column width corresponding to each column head.
+ * Returns the column width corresponding to each column head
*
* @param context
* @return
*/
- protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
- return contentCellStyle(context.getHeadData());
+ protected WriteCellStyle headCellStyle(CellWriteHandlerContext context) {
+ return headCellStyle(context.getHeadData());
}
/**
@@ -46,7 +46,19 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
* @param head Nullable
* @return
*/
- protected abstract WriteCellStyle headCellStyle(Head head);
+ protected WriteCellStyle headCellStyle(Head head) {
+ return null;
+ }
+
+ /**
+ * Returns the column width corresponding to each column head.
+ *
+ * @param context
+ * @return
+ */
+ protected WriteCellStyle contentCellStyle(CellWriteHandlerContext context) {
+ return contentCellStyle(context.getHeadData());
+ }
/**
* Returns the column width corresponding to each column head
@@ -55,9 +67,7 @@ public abstract class AbstractVerticalCellStyleStrategy extends AbstractCellStyl
* @return
*/
protected WriteCellStyle contentCellStyle(Head head) {
- throw new UnsupportedOperationException(
- "One of the two methods 'contentCellStyle(Cell cell, Head head, Integer relativeRowIndex)' and "
- + "'contentCellStyle(Head head)' must be implemented.");
+ return null;
}
protected boolean stopProcessing(CellWriteHandlerContext context) {
diff --git a/src/test/java/com/alibaba/easyexcel/test/core/StyleTestUtils.java b/src/test/java/com/alibaba/easyexcel/test/core/StyleTestUtils.java
new file mode 100644
index 00000000..266f724a
--- /dev/null
+++ b/src/test/java/com/alibaba/easyexcel/test/core/StyleTestUtils.java
@@ -0,0 +1,43 @@
+package com.alibaba.easyexcel.test.core;
+
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+
+public class StyleTestUtils {
+
+ public static byte[] getFillForegroundColor(Cell cell) {
+ if (cell instanceof XSSFCell) {
+ return ((XSSFCell)cell).getCellStyle().getFillForegroundColorColor().getRGB();
+ } else {
+ return short2byte(((HSSFCell)cell).getCellStyle().getFillForegroundColorColor().getTriplet());
+ }
+ }
+
+ public static byte[] getFontColor(Cell cell, Workbook workbook) {
+ if (cell instanceof XSSFCell) {
+ return ((XSSFCell)cell).getCellStyle().getFont().getXSSFColor().getRGB();
+ } else {
+ return short2byte(((HSSFCell)cell).getCellStyle().getFont(workbook).getHSSFColor((HSSFWorkbook)workbook)
+ .getTriplet());
+ }
+ }
+
+ public static short getFontHeightInPoints(Cell cell, Workbook workbook) {
+ if (cell instanceof XSSFCell) {
+ return ((XSSFCell)cell).getCellStyle().getFont().getFontHeightInPoints();
+ } else {
+ return ((HSSFCell)cell).getCellStyle().getFont(workbook).getFontHeightInPoints();
+ }
+ }
+
+ private static byte[] short2byte(short[] shorts) {
+ byte[] bytes = new byte[shorts.length];
+ for (int i = 0; i < shorts.length; i++) {
+ bytes[i] = (byte)shorts[i];
+ }
+ return bytes;
+ }
+}
diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
index 3df12b1c..e85d63a6 100644
--- a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
+++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
@@ -6,23 +6,23 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.FixMethodOrder;
-import org.junit.Test;
-import org.junit.runners.MethodSorters;
-
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
+import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
/**
- *
* @author Jiaju Zhuang
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@@ -30,8 +30,10 @@ public class FillDataTest {
private static File file07;
private static File file03;
+ private static File fileCsv;
private static File simpleTemplate07;
private static File simpleTemplate03;
+ private static File simpleTemplateCsv;
private static File fileComplex07;
private static File complexFillTemplate07;
private static File fileComplex03;
@@ -53,8 +55,10 @@ public class FillDataTest {
public static void init() {
file07 = TestFileUtil.createNewFile("fill07.xlsx");
file03 = TestFileUtil.createNewFile("fill03.xls");
+ fileCsv = TestFileUtil.createNewFile("fill.csv");
simpleTemplate07 = TestFileUtil.readFile("fill" + File.separator + "simple.xlsx");
simpleTemplate03 = TestFileUtil.readFile("fill" + File.separator + "simple.xls");
+ simpleTemplateCsv = TestFileUtil.readFile("fill" + File.separator + "simple.csv");
fileComplex07 = TestFileUtil.createNewFile("fillComplex07.xlsx");
complexFillTemplate07 = TestFileUtil.readFile("fill" + File.separator + "complex.xlsx");
fileComplex03 = TestFileUtil.createNewFile("fillComplex03.xls");
@@ -83,6 +87,13 @@ public class FillDataTest {
fill(file03, simpleTemplate03);
}
+ @Test
+ public void t03FillCsv() {
+ ExcelGenerateException excelGenerateException = Assert.assertThrows(ExcelGenerateException.class,
+ () -> fill(fileCsv, simpleTemplateCsv));
+ Assert.assertEquals("csv cannot use template.", excelGenerateException.getMessage());
+ }
+
@Test
public void t03ComplexFill07() {
complexFill(fileComplex07, complexFillTemplate07);
@@ -147,11 +158,11 @@ public class FillDataTest {
excelWriter.finish();
List