Browse Source

Merge branch 'bugfix' into developing

pull/2159/head
Jiaju Zhuang 3 years ago
parent
commit
10fc17f30f
  1. 1
      README.md
  2. 68
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  3. 45
      src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java
  4. 11
      src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java
  5. 4
      src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java
  6. 32
      src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java
  7. 5
      src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
  8. 5
      src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java
  9. 5
      src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java
  10. 5
      src/main/java/com/alibaba/excel/util/ConverterUtils.java
  11. 236
      src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java
  12. 177
      src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java
  13. 90
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java
  14. 88
      src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java
  15. 58
      src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java
  16. 8
      src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java
  17. 12
      src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java
  18. 13
      src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java
  19. 10
      src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java
  20. 1
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  21. 2
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
  22. 6
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  23. 3
      update.md

1
README.md

@ -35,7 +35,6 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
* 读的时候`invoke`里面抛出异常,不会再额外封装一层`ExcelAnalysisException` (不会编译报错) * 读的时候`invoke`里面抛出异常,不会再额外封装一层`ExcelAnalysisException` (不会编译报错)
* 样式等注解涉及到 `boolean` or 一些枚举 值的 有变动,新增默认值(会编译报错,注解改就行) * 样式等注解涉及到 `boolean` or 一些枚举 值的 有变动,新增默认值(会编译报错,注解改就行)
* 大版本升级后建议相关内容重新测试下 * 大版本升级后建议相关内容重新测试下
* 要升级涉及到
### 最新版本 ### 最新版本
```xml ```xml

68
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -15,10 +15,15 @@ import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.NumberDataFormatterUtils; import com.alibaba.excel.util.NumberDataFormatterUtils;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils; import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.WriteWorkbook;
@ -83,13 +88,16 @@ public class WriteContextImpl implements WriteContext {
LOGGER.debug("Begin to Initialization 'WriteContextImpl'"); LOGGER.debug("Begin to Initialization 'WriteContextImpl'");
} }
initCurrentWorkbookHolder(writeWorkbook); initCurrentWorkbookHolder(writeWorkbook);
WriteHandlerUtils.beforeWorkbookCreate(this);
WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
this);
WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext);
try { try {
WorkBookUtil.createWorkBook(writeWorkbookHolder); WorkBookUtil.createWorkBook(writeWorkbookHolder);
} catch (Exception e) { } catch (Exception e) {
throw new ExcelGenerateException("Create workbook failure", e); throw new ExcelGenerateException("Create workbook failure", e);
} }
WriteHandlerUtils.afterWorkbookCreate(this); WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initialization 'WriteContextImpl' complete"); LOGGER.debug("Initialization 'WriteContextImpl' complete");
} }
@ -118,8 +126,10 @@ public class WriteContextImpl implements WriteContext {
initCurrentSheetHolder(writeSheet); initCurrentSheetHolder(writeSheet);
// Workbook handler need to supplementary execution // Workbook handler need to supplementary execution
WriteHandlerUtils.beforeWorkbookCreate(this, true); WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
WriteHandlerUtils.afterWorkbookCreate(this, true); this, true);
WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true);
WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true);
// Initialization current sheet // Initialization current sheet
initSheet(writeType); initSheet(writeType);
@ -162,7 +172,8 @@ public class WriteContextImpl implements WriteContext {
} }
private void initSheet(WriteTypeEnum writeType) { private void initSheet(WriteTypeEnum writeType) {
WriteHandlerUtils.beforeSheetCreate(this); SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this);
WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext);
Sheet currentSheet; Sheet currentSheet;
try { try {
if (writeSheetHolder.getSheetNo() != null) { if (writeSheetHolder.getSheetNo() != null) {
@ -192,7 +203,7 @@ public class WriteContextImpl implements WriteContext {
currentSheet = createSheet(); currentSheet = createSheet();
} }
writeSheetHolder.setSheet(currentSheet); writeSheetHolder.setSheet(currentSheet);
WriteHandlerUtils.afterSheetCreate(this); WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext);
if (WriteTypeEnum.ADD.equals(writeType)) { if (WriteTypeEnum.ADD.equals(writeType)) {
// Initialization head // Initialization head
initHead(writeSheetHolder.excelWriteHeadProperty()); initHead(writeSheetHolder.excelWriteHeadProperty());
@ -226,11 +237,17 @@ public class WriteContextImpl implements WriteContext {
} }
for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber() for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber()
+ newRowIndex; i++, relativeRowIndex++) { + newRowIndex; i++, relativeRowIndex++) {
WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE);
RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(this,
newRowIndex, relativeRowIndex, Boolean.TRUE);
WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext);
Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i); Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i);
WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE); rowWriteHandlerContext.setRow(row);
addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.TRUE); WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext);
addOneRowOfHeadDataToExcel(row, i, excelWriteHeadProperty.getHeadMap(), relativeRowIndex);
WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
} }
} }
@ -242,25 +259,29 @@ public class WriteContextImpl implements WriteContext {
} }
} }
private void addOneRowOfHeadDataToExcel(Row row, Map<Integer, Head> headMap, int relativeRowIndex) { private void addOneRowOfHeadDataToExcel(Row row, Integer rowIndex, Map<Integer, Head> headMap,
int relativeRowIndex) {
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) { for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
Head head = entry.getValue(); Head head = entry.getValue();
int columnIndex = entry.getKey(); int columnIndex = entry.getKey();
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null,
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName()); currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName());
WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE, CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row,
excelContentProperty); rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
Cell cell = row.createCell(columnIndex); Cell cell = row.createCell(columnIndex);
cellWriteHandlerContext.setCell(cell);
WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE, excelContentProperty); WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
WriteCellData<String> writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex)); WriteCellData<String> writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex));
cell.setCellValue(writeCellData.getStringValue()); cell.setCellValue(writeCellData.getStringValue());
cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(writeCellData));
cellWriteHandlerContext.setFirstCellData(writeCellData);
WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex, Boolean.TRUE, WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
excelContentProperty);
} }
} }
@ -288,10 +309,15 @@ public class WriteContextImpl implements WriteContext {
initCurrentTableHolder(writeTable); initCurrentTableHolder(writeTable);
// Workbook and sheet handler need to supplementary execution // Workbook and sheet handler need to supplementary execution
WriteHandlerUtils.beforeWorkbookCreate(this, true); WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext(
WriteHandlerUtils.afterWorkbookCreate(this, true); this, true);
WriteHandlerUtils.beforeSheetCreate(this, true); WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true);
WriteHandlerUtils.afterSheetCreate(this, true); WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true);
SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this,
true);
WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext, true);
WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext, true);
initHead(writeTableHolder.excelWriteHeadProperty()); initHead(writeTableHolder.excelWriteHeadProperty());
} }
@ -331,7 +357,7 @@ public class WriteContextImpl implements WriteContext {
return; return;
} }
finished = true; finished = true;
WriteHandlerUtils.afterWorkbookDispose(this); WriteHandlerUtils.afterWorkbookDispose(writeWorkbookHolder.getWorkbookWriteHandlerContext());
if (writeWorkbookHolder == null) { if (writeWorkbookHolder == null) {
return; return;
} }

45
src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java

@ -5,6 +5,9 @@ import java.util.Map;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
/** /**
* Converter unique key.Consider that you can just use class as the key. * Converter unique key.Consider that you can just use class as the key.
* *
@ -12,33 +15,35 @@ import com.alibaba.excel.util.MapUtils;
*/ */
public class ConverterKeyBuild { public class ConverterKeyBuild {
private static final Map<String, String> BOXING_MAP = MapUtils.newHashMap(); private static final Map<Class<?>, Class<?>> BOXING_MAP = MapUtils.newHashMap();
static { static {
BOXING_MAP.put(int.class.getName(), Integer.class.getName()); BOXING_MAP.put(int.class, Integer.class);
BOXING_MAP.put(byte.class.getName(), Byte.class.getName()); BOXING_MAP.put(byte.class, Byte.class);
BOXING_MAP.put(long.class.getName(), Long.class.getName()); BOXING_MAP.put(long.class, Long.class);
BOXING_MAP.put(double.class.getName(), Double.class.getName()); BOXING_MAP.put(double.class, Double.class);
BOXING_MAP.put(float.class.getName(), Float.class.getName()); BOXING_MAP.put(float.class, Float.class);
BOXING_MAP.put(char.class.getName(), Character.class.getName()); BOXING_MAP.put(char.class, Character.class);
BOXING_MAP.put(short.class.getName(), Short.class.getName()); BOXING_MAP.put(short.class, Short.class);
BOXING_MAP.put(boolean.class.getName(), Boolean.class.getName()); BOXING_MAP.put(boolean.class, Boolean.class);
} }
public static String buildKey(Class<?> clazz) { public static ConverterKey buildKey(Class<?> clazz) {
String className = clazz.getName(); Class<?> boxingClass = BOXING_MAP.get(clazz);
String boxingClassName = BOXING_MAP.get(clazz.getName()); if (boxingClass != null) {
if (boxingClassName == null) { return new ConverterKey(boxingClass, null);
return className;
} }
return boxingClassName; return new ConverterKey(clazz, null);
} }
public static String buildKey(Class<?> clazz, CellDataTypeEnum cellDataTypeEnum) { public static ConverterKey buildKey(Class<?> clazz, CellDataTypeEnum cellDataTypeEnum) {
String key = buildKey(clazz); return new ConverterKey(clazz, cellDataTypeEnum);
if (cellDataTypeEnum == null) {
return key;
} }
return key + "-" + cellDataTypeEnum;
@Data
@AllArgsConstructor
public static class ConverterKey {
private Class<?> clazz;
private CellDataTypeEnum cellDataTypeEnum;
} }
} }

11
src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java

@ -2,6 +2,7 @@ package com.alibaba.excel.converters;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey;
import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter;
@ -52,8 +53,8 @@ import com.alibaba.excel.util.MapUtils;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class DefaultConverterLoader { public class DefaultConverterLoader {
private static Map<String, Converter<?>> defaultWriteConverter; private static Map<ConverterKey, Converter<?>> defaultWriteConverter;
private static Map<String, Converter<?>> allConverter; private static Map<ConverterKey, Converter<?>> allConverter;
static { static {
initDefaultWriteConverter(); initDefaultWriteConverter();
@ -153,7 +154,7 @@ public class DefaultConverterLoader {
* *
* @return * @return
*/ */
public static Map<String, Converter<?>> loadDefaultWriteConverter() { public static Map<ConverterKey, Converter<?>> loadDefaultWriteConverter() {
return defaultWriteConverter; return defaultWriteConverter;
} }
@ -171,7 +172,7 @@ public class DefaultConverterLoader {
* *
* @return * @return
*/ */
public static Map<String, Converter<?>> loadDefaultReadConverter() { public static Map<ConverterKey, Converter<?>> loadDefaultReadConverter() {
return loadAllConverter(); return loadAllConverter();
} }
@ -180,7 +181,7 @@ public class DefaultConverterLoader {
* *
* @return * @return
*/ */
public static Map<String, Converter<?>> loadAllConverter() { public static Map<ConverterKey, Converter<?>> loadAllConverter() {
return allConverter; return allConverter;
} }

4
src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java

@ -32,7 +32,7 @@ public class ExcelDataConvertException extends RuntimeException {
*/ */
private ExcelContentProperty excelContentProperty; private ExcelContentProperty excelContentProperty;
public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData<?> cellData,
ExcelContentProperty excelContentProperty, String message) { ExcelContentProperty excelContentProperty, String message) {
super(message); super(message);
this.rowIndex = rowIndex; this.rowIndex = rowIndex;
@ -41,7 +41,7 @@ public class ExcelDataConvertException extends RuntimeException {
this.excelContentProperty = excelContentProperty; this.excelContentProperty = excelContentProperty;
} }
public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData<?> cellData,
ExcelContentProperty excelContentProperty, String message, Throwable cause) { ExcelContentProperty excelContentProperty, String message, Throwable cause) {
super(message, cause); super(message, cause);
this.rowIndex = rowIndex; this.rowIndex = rowIndex;

32
src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java

@ -0,0 +1,32 @@
package com.alibaba.excel.exception;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import lombok.Data;
/**
* Data convert exception
*
* @author Jiaju Zhuang
*/
@Data
public class ExcelWriteDataConvertException extends ExcelDataConvertException {
/**
* handler.
*/
private CellWriteHandlerContext cellWriteHandlerContext;
public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message) {
super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(),
cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message);
this.cellWriteHandlerContext = cellWriteHandlerContext;
}
public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message,
Throwable cause) {
super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(),
cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message,
cause);
this.cellWriteHandlerContext = cellWriteHandlerContext;
}
}

5
src/main/java/com/alibaba/excel/metadata/AbstractHolder.java

@ -4,6 +4,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -38,7 +39,7 @@ public abstract class AbstractHolder implements ConfigurationHolder {
* <p> * <p>
* Write key: * Write key:
*/ */
private Map<String, Converter<?>> converterMap; private Map<ConverterKey, Converter<?>> converterMap;
public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) { public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) {
this.newInitialization = Boolean.TRUE; this.newInitialization = Boolean.TRUE;
@ -81,7 +82,7 @@ public abstract class AbstractHolder implements ConfigurationHolder {
} }
@Override @Override
public Map<String, Converter<?>> converterMap() { public Map<ConverterKey, Converter<?>> converterMap() {
return getConverterMap(); return getConverterMap();
} }

5
src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java

@ -3,9 +3,9 @@ package com.alibaba.excel.metadata;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey;
/** /**
*
* Get the corresponding holder * Get the corresponding holder
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
@ -13,7 +13,6 @@ import com.alibaba.excel.converters.Converter;
public interface ConfigurationHolder extends Holder { public interface ConfigurationHolder extends Holder {
/** /**
*
* Record whether it's new or from cache * Record whether it's new or from cache
* *
* @return Record whether it's new or from cache * @return Record whether it's new or from cache
@ -32,5 +31,5 @@ public interface ConfigurationHolder extends Holder {
* *
* @return Converter * @return Converter
*/ */
Map<String, Converter<?>> converterMap(); Map<ConverterKey, Converter<?>> converterMap();
} }

5
src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java

@ -8,7 +8,6 @@ import java.util.List;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.fill.AnalysisCell;
import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import lombok.Data; import lombok.Data;
@ -56,10 +55,6 @@ public class WriteCellData<T> extends CellData<T> {
*/ */
private CellStyle originCellStyle; private CellStyle originCellStyle;
/**
* Only in the case of the fill is not null
*/
private AnalysisCell analysisCell;
public WriteCellData(String stringValue) { public WriteCellData(String stringValue) {
this(CellDataTypeEnum.STRING, stringValue); this(CellDataTypeEnum.STRING, stringValue);

5
src/main/java/com/alibaba/excel/util/ConverterUtils.java

@ -8,6 +8,7 @@ import java.util.Map;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey;
import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.NullableObjectConverter;
import com.alibaba.excel.converters.ReadConverterContext; import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
@ -80,7 +81,7 @@ public class ConverterUtils {
* @return * @return
*/ */
public static Object convertToJavaObject(ReadCellData<?> cellData, Field field, public static Object convertToJavaObject(ReadCellData<?> cellData, Field field,
ExcelContentProperty contentProperty, Map<String, Converter<?>> converterMap, AnalysisContext context, ExcelContentProperty contentProperty, Map<ConverterKey, Converter<?>> converterMap, AnalysisContext context,
Integer rowIndex, Integer columnIndex) { Integer rowIndex, Integer columnIndex) {
Class<?> clazz; Class<?> clazz;
if (field == null) { if (field == null) {
@ -126,7 +127,7 @@ public class ConverterUtils {
* @return * @return
*/ */
private static Object doConvertToJavaObject(ReadCellData<?> cellData, Class<?> clazz, private static Object doConvertToJavaObject(ReadCellData<?> cellData, Class<?> clazz,
ExcelContentProperty contentProperty, Map<String, Converter<?>> converterMap, AnalysisContext context, ExcelContentProperty contentProperty, Map<ConverterKey, Converter<?>> converterMap, AnalysisContext context,
Integer rowIndex, Integer columnIndex) { Integer rowIndex, Integer columnIndex) {
Converter<?> converter = null; Converter<?> converter = null;
if (contentProperty != null) { if (contentProperty != null) {

236
src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java

@ -5,7 +5,6 @@ import java.util.Map;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler;
@ -18,7 +17,6 @@ import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
/** /**
@ -30,217 +28,205 @@ public class WriteHandlerUtils {
private WriteHandlerUtils() {} private WriteHandlerUtils() {}
public static void beforeWorkbookCreate(WriteContext writeContext) { public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext) {
beforeWorkbookCreate(writeContext, false); return createWorkbookWriteHandlerContext(writeContext, false);
} }
public static void beforeWorkbookCreate(WriteContext writeContext, boolean runOwn) { public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext,
boolean runOwn) {
List<WriteHandler> handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); List<WriteHandler> handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn);
if (handlerList == null || handlerList.isEmpty()) { WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null, null, null);
return; if (runOwn) {
context.setOwnHandlerList(handlerList);
} else {
context.setHandlerList(handlerList);
}
writeContext.writeWorkbookHolder().setWorkbookWriteHandlerContext(context);
return context;
}
public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context) {
beforeWorkbookCreate(context, false);
}
public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList;
if (runOwn) {
handlerList = context.getOwnHandlerList();
} else {
handlerList = context.getHandlerList();
} }
WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null); if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(context); ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(context);
} }
} }
} }
public static void afterWorkbookCreate(WriteContext writeContext) { public static void afterWorkbookCreate(WorkbookWriteHandlerContext context) {
afterWorkbookCreate(writeContext, false); afterWorkbookCreate(context, false);
} }
public static void afterWorkbookCreate(WriteContext writeContext, boolean runOwn) { public static void afterWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); List<WriteHandler> handlerList;
if (handlerList == null || handlerList.isEmpty()) { if (runOwn) {
return; handlerList = context.getOwnHandlerList();
} else {
handlerList = context.getHandlerList();
} }
WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, if (CollectionUtils.isNotEmpty(handlerList)) {
writeContext.writeWorkbookHolder());
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(context); ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(context);
} }
} }
} }
public static void afterWorkbookDispose(WriteContext writeContext) { public static void afterWorkbookDispose(WorkbookWriteHandlerContext context) {
List<WriteHandler> handlerList = List<WriteHandler> handlerList = context.getHandlerList();
writeContext.currentWriteHolder().writeHandlerMap().get(WorkbookWriteHandler.class); if (CollectionUtils.isNotEmpty(handlerList)) {
if (handlerList == null || handlerList.isEmpty()) {
return;
}
WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext,
writeContext.writeWorkbookHolder());
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof WorkbookWriteHandler) {
((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(context); ((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(context);
} }
} }
} }
public static void beforeSheetCreate(WriteContext writeContext) { public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext) {
beforeSheetCreate(writeContext, false); return createSheetWriteHandlerContext(writeContext, false);
} }
public static void beforeSheetCreate(WriteContext writeContext, boolean runOwn) { public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext, boolean runOwn) {
List<WriteHandler> handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); List<WriteHandler> handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext,
writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder()); writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), null, null);
if (runOwn) {
context.setOwnHandlerList(handlerList);
} else {
context.setHandlerList(handlerList);
}
return context;
}
public static void beforeSheetCreate(SheetWriteHandlerContext context) {
beforeSheetCreate(context, false);
}
public static void beforeSheetCreate(SheetWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList;
if (runOwn) {
handlerList = context.getOwnHandlerList();
} else {
handlerList = context.getHandlerList();
}
if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
((SheetWriteHandler)writeHandler).beforeSheetCreate(context); ((SheetWriteHandler)writeHandler).beforeSheetCreate(context);
} }
} }
} }
public static void afterSheetCreate(WriteContext writeContext) { public static void afterSheetCreate(SheetWriteHandlerContext context) {
afterSheetCreate(writeContext, false); afterSheetCreate(context, false);
} }
public static void afterSheetCreate(WriteContext writeContext, boolean runOwn) { public static void afterSheetCreate(SheetWriteHandlerContext context, boolean runOwn) {
List<WriteHandler> handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); List<WriteHandler> handlerList;
if (handlerList == null || handlerList.isEmpty()) { if (runOwn) {
return; handlerList = context.getOwnHandlerList();
} else {
handlerList = context.getHandlerList();
} }
SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, if (CollectionUtils.isNotEmpty(handlerList)) {
writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder());
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof SheetWriteHandler) {
((SheetWriteHandler)writeHandler).afterSheetCreate(context); ((SheetWriteHandler)writeHandler).afterSheetCreate(context);
} }
} }
} }
public static void beforeCellCreate(WriteContext writeContext, Row row, Head head, Integer columnIndex, public static CellWriteHandlerContext createCellWriteHandlerContext(WriteContext writeContext, Row row,
Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { Integer rowIndex, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead,
List<WriteHandler> handlerList = ExcelContentProperty excelContentProperty) {
writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(
if (handlerList == null || handlerList.isEmpty()) { CellWriteHandler.class);
return; return new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, rowIndex, null, columnIndex,
relativeRowIndex, head, null, null, isHead, excelContentProperty, handlerList);
} }
CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, null, columnIndex, relativeRowIndex, public static void beforeCellCreate(CellWriteHandlerContext context) {
head, null, null, isHead, excelContentProperty); List<WriteHandler> handlerList = context.getHandlerList();
if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).beforeCellCreate(context); ((CellWriteHandler)writeHandler).beforeCellCreate(context);
} }
} }
} }
public static void afterCellCreate(WriteContext writeContext, Cell cell, Head head, Integer relativeRowIndex, public static void afterCellCreate(CellWriteHandlerContext context) {
Boolean isHead, ExcelContentProperty excelContentProperty) { List<WriteHandler> handlerList = context.getHandlerList();
List<WriteHandler> handlerList = if (CollectionUtils.isNotEmpty(handlerList)) {
writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell,
cell.getColumnIndex(), relativeRowIndex, head, null, null, isHead, excelContentProperty);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellCreate(context); ((CellWriteHandler)writeHandler).afterCellCreate(context);
} }
} }
} }
public static void afterCellDataConverted(WriteContext writeContext, WriteCellData<?> cellData, Cell cell, public static void afterCellDataConverted(CellWriteHandlerContext context) {
Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { List<WriteHandler> handlerList = context.getHandlerList();
List<WriteHandler> handlerList = if (CollectionUtils.isNotEmpty(handlerList)) {
writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
List<WriteCellData<?>> 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, excelContentProperty);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellDataConverted(context); ((CellWriteHandler)writeHandler).afterCellDataConverted(context);
} }
} }
} }
public static void afterCellDispose(WriteContext writeContext, WriteCellData<?> cellData, Cell cell, Head head, public static void afterCellDispose(CellWriteHandlerContext context) {
Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { List<WriteHandler> handlerList = context.getHandlerList();
List<WriteCellData<?>> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); if (CollectionUtils.isNotEmpty(handlerList)) {
afterCellDispose(writeContext, cellDataList, cell, head, relativeRowIndex, isHead, excelContentProperty);
}
public static void afterCellDispose(WriteContext writeContext, List<WriteCellData<?>> cellDataList, Cell cell,
Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) {
List<WriteHandler> handlerList =
writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class);
if (handlerList == null || handlerList.isEmpty()) {
return;
}
WriteCellData<?> cellData = null;
if (CollectionUtils.isNotEmpty(cellDataList)) {
cellData = cellDataList.get(0);
}
CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell,
cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof CellWriteHandler) {
((CellWriteHandler)writeHandler).afterCellDispose(context); ((CellWriteHandler)writeHandler).afterCellDispose(context);
} }
} }
} }
public static void beforeRowCreate(WriteContext writeContext, Integer rowIndex, Integer relativeRowIndex, public static RowWriteHandlerContext createRowWriteHandlerContext(WriteContext writeContext, Integer rowIndex,
Boolean isHead) { Integer relativeRowIndex, Boolean isHead) {
List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(
if (handlerList == null || handlerList.isEmpty()) { RowWriteHandler.class);
return; return new RowWriteHandlerContext(writeContext,
writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex,
isHead, handlerList);
} }
RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, isHead); public static void beforeRowCreate(RowWriteHandlerContext context) {
List<WriteHandler> handlerList = context.getHandlerList();
if (CollectionUtils.isNotEmpty(handlerList)) {
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).beforeRowCreate(context); ((RowWriteHandler)writeHandler).beforeRowCreate(context);
} }
} }
} }
public static void afterRowCreate(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { public static void afterRowCreate(RowWriteHandlerContext context) {
List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); List<WriteHandler> handlerList = context.getHandlerList();
if (handlerList == null || handlerList.isEmpty()) { if (CollectionUtils.isNotEmpty(handlerList)) {
return;
}
RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex,
isHead);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).afterRowCreate(context); ((RowWriteHandler)writeHandler).afterRowCreate(context);
} }
} }
} }
public static void afterRowDispose(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { public static void afterRowDispose(RowWriteHandlerContext context) {
List<WriteHandler> handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); List<WriteHandler> handlerList = context.getHandlerList();
if (handlerList == null || handlerList.isEmpty()) { if (CollectionUtils.isNotEmpty(handlerList)) {
return;
}
RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(),
writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex,
isHead);
for (WriteHandler writeHandler : handlerList) { for (WriteHandler writeHandler : handlerList) {
if (writeHandler instanceof RowWriteHandler) {
((RowWriteHandler)writeHandler).afterRowDispose(context); ((RowWriteHandler)writeHandler).afterRowDispose(context);
} }
} }
} }
private static List<WriteHandler> getHandlerList(WriteContext writeContext, Class<? extends WriteHandler> clazz, private static List<WriteHandler> getHandlerList(WriteContext writeContext, Class<? extends
WriteHandler> clazz,
boolean runOwn) { boolean runOwn) {
Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap; Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap;
if (runOwn) { if (runOwn) {

177
src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java

@ -8,8 +8,7 @@ import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.NullableObjectConverter;
import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.exception.ExcelWriteDataConvertException;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CommentData; import com.alibaba.excel.metadata.data.CommentData;
import com.alibaba.excel.metadata.data.FormulaData; import com.alibaba.excel.metadata.data.FormulaData;
import com.alibaba.excel.metadata.data.HyperlinkData; import com.alibaba.excel.metadata.data.HyperlinkData;
@ -19,10 +18,11 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.FileTypeUtils; import com.alibaba.excel.util.FileTypeUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.StyleUtil; import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils; import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.metadata.holder.WriteHolder; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
@ -33,6 +33,7 @@ import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
/** /**
@ -47,124 +48,140 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
this.writeContext = writeContext; this.writeContext = writeContext;
} }
protected WriteCellData<?> converterAndSet(WriteHolder currentWriteHolder, Class<?> clazz, /**
CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, * Transform the data and then to set into the cell
Integer relativeRowIndex, int rowIndex, int columnIndex) { *
boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() * @param cellWriteHandlerContext context
.getAutoTrim()); * @return
if (needTrim) { */
value = ((String)value).trim(); protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) {
}
WriteCellData<?> cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); WriteCellData<?> cellData = convert(cellWriteHandlerContext);
WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(cellData));
excelContentProperty); cellWriteHandlerContext.setFirstCellData(cellData);
WriteHandlerUtils.afterCellDataConverted(cellWriteHandlerContext);
// Fill in picture information // Fill in picture information
fillImage(cell, cellData.getImageDataList()); fillImage(cellWriteHandlerContext, cellData.getImageDataList());
// Fill in comment information // Fill in comment information
fillComment(cell, cellData.getCommentData()); fillComment(cellWriteHandlerContext, cellData.getCommentData());
// Fill in hyper link information // Fill in hyper link information
fillHyperLink(cell, cellData.getHyperlinkData()); fillHyperLink(cellWriteHandlerContext, cellData.getHyperlinkData());
// Fill in formula information // Fill in formula information
fillFormula(cell, cellData.getFormulaData()); fillFormula(cellWriteHandlerContext, cellData.getFormulaData());
// Fill index // Fill index
cellData.setRowIndex(rowIndex); cellData.setRowIndex(cellWriteHandlerContext.getRowIndex());
cellData.setColumnIndex(columnIndex); cellData.setColumnIndex(cellWriteHandlerContext.getColumnIndex());
if (cellData.getType() == null) { if (cellData.getType() == null) {
cellData.setType(CellDataTypeEnum.EMPTY); cellData.setType(CellDataTypeEnum.EMPTY);
} }
Cell cell = cellWriteHandlerContext.getCell();
switch (cellData.getType()) { switch (cellData.getType()) {
case STRING: case STRING:
cell.setCellValue(cellData.getStringValue()); cell.setCellValue(cellData.getStringValue());
return cellData; return;
case BOOLEAN: case BOOLEAN:
cell.setCellValue(cellData.getBooleanValue()); cell.setCellValue(cellData.getBooleanValue());
return cellData; return;
case NUMBER: case NUMBER:
cell.setCellValue(cellData.getNumberValue().doubleValue()); cell.setCellValue(cellData.getNumberValue().doubleValue());
return cellData; return;
case DATE: case DATE:
cell.setCellValue(cellData.getDateValue()); cell.setCellValue(cellData.getDateValue());
return cellData; return;
case RICH_TEXT_STRING: case RICH_TEXT_STRING:
cell.setCellValue(StyleUtil cell.setCellValue(StyleUtil
.buildRichTextString(writeContext.writeWorkbookHolder(), cellData.getRichTextStringDataValue())); .buildRichTextString(writeContext.writeWorkbookHolder(), cellData.getRichTextStringDataValue()));
return cellData; return;
case EMPTY: case EMPTY:
return cellData; return;
default: default:
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData, throw new ExcelWriteDataConvertException(cellWriteHandlerContext,
excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType() "Not supported data:" + cellWriteHandlerContext.getOriginalValue() + " return type:"
+ "at row:" + cell.getRow().getRowNum()); + cellData.getType()
+ "at row:" + cellWriteHandlerContext.getRowIndex());
} }
} }
private void fillFormula(Cell cell, FormulaData formulaData) { private void fillFormula(CellWriteHandlerContext cellWriteHandlerContext, FormulaData formulaData) {
if (formulaData == null) { if (formulaData == null) {
return; return;
} }
Cell cell = cellWriteHandlerContext.getCell();
if (formulaData.getFormulaValue() != null) { if (formulaData.getFormulaValue() != null) {
cell.setCellFormula(formulaData.getFormulaValue()); cell.setCellFormula(formulaData.getFormulaValue());
} }
} }
private void fillHyperLink(Cell cell, HyperlinkData hyperlinkData) { private void fillHyperLink(CellWriteHandlerContext cellWriteHandlerContext, HyperlinkData hyperlinkData) {
if (hyperlinkData == null) { if (hyperlinkData == null) {
return; return;
} }
CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper(); Integer rowIndex = cellWriteHandlerContext.getRowIndex();
Integer columnIndex = cellWriteHandlerContext.getColumnIndex();
Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook();
Cell cell = cellWriteHandlerContext.getCell();
CreationHelper helper = workbook.getCreationHelper();
Hyperlink hyperlink = helper.createHyperlink(StyleUtil.getHyperlinkType(hyperlinkData.getHyperlinkType())); Hyperlink hyperlink = helper.createHyperlink(StyleUtil.getHyperlinkType(hyperlinkData.getHyperlinkType()));
hyperlink.setAddress(hyperlinkData.getAddress()); hyperlink.setAddress(hyperlinkData.getAddress());
hyperlink.setFirstRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getFirstRowIndex(), hyperlink.setFirstRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getFirstRowIndex(),
hyperlinkData.getRelativeFirstRowIndex())); hyperlinkData.getRelativeFirstRowIndex()));
hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getFirstColumnIndex(), hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getFirstColumnIndex(),
hyperlinkData.getRelativeFirstColumnIndex())); hyperlinkData.getRelativeFirstColumnIndex()));
hyperlink.setLastRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getLastRowIndex(), hyperlink.setLastRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getLastRowIndex(),
hyperlinkData.getRelativeLastRowIndex())); hyperlinkData.getRelativeLastRowIndex()));
hyperlink.setLastColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getLastColumnIndex(), hyperlink.setLastColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getLastColumnIndex(),
hyperlinkData.getRelativeLastColumnIndex())); hyperlinkData.getRelativeLastColumnIndex()));
cell.setHyperlink(hyperlink); cell.setHyperlink(hyperlink);
} }
private void fillComment(Cell cell, CommentData commentData) { private void fillComment(CellWriteHandlerContext cellWriteHandlerContext, CommentData commentData) {
if (commentData == null) { if (commentData == null) {
return; return;
} }
ClientAnchor anchor; ClientAnchor anchor;
Integer rowIndex = cellWriteHandlerContext.getRowIndex();
Integer columnIndex = cellWriteHandlerContext.getColumnIndex();
Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet();
Cell cell = cellWriteHandlerContext.getCell();
if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.XLSX) { if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.XLSX) {
anchor = new XSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), anchor = new XSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()),
StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getTop()),
StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getRight()),
StyleUtil.getCoordinate(commentData.getBottom()), StyleUtil.getCoordinate(commentData.getBottom()),
StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(),
commentData.getRelativeFirstColumnIndex()), commentData.getRelativeFirstColumnIndex()),
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(),
commentData.getRelativeFirstRowIndex()), commentData.getRelativeFirstRowIndex()),
StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(),
commentData.getRelativeLastColumnIndex()) + 1, commentData.getRelativeLastColumnIndex()) + 1,
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(),
commentData.getRelativeLastRowIndex()) + 1); commentData.getRelativeLastRowIndex()) + 1);
} else { } else {
anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()),
StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getTop()),
StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getRight()),
StyleUtil.getCoordinate(commentData.getBottom()), StyleUtil.getCoordinate(commentData.getBottom()),
(short)StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), (short)StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(),
commentData.getRelativeFirstColumnIndex()), commentData.getRelativeFirstColumnIndex()),
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(),
commentData.getRelativeFirstRowIndex()), commentData.getRelativeFirstRowIndex()),
(short)(StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), (short)(StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(),
commentData.getRelativeLastColumnIndex()) + 1), commentData.getRelativeLastColumnIndex()) + 1),
StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(),
commentData.getRelativeLastRowIndex()) + 1); commentData.getRelativeLastRowIndex()) + 1);
} }
Comment comment = cell.getSheet().createDrawingPatriarch().createCellComment(anchor);
Comment comment = sheet.createDrawingPatriarch().createCellComment(anchor);
if (commentData.getRichTextStringData() != null) { if (commentData.getRichTextStringData() != null) {
comment.setString( comment.setString(
StyleUtil.buildRichTextString(writeContext.writeWorkbookHolder(), commentData.getRichTextStringData())); StyleUtil.buildRichTextString(writeContext.writeWorkbookHolder(), commentData.getRichTextStringData()));
@ -175,18 +192,22 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
cell.setCellComment(comment); cell.setCellComment(comment);
} }
protected void fillImage(Cell cell, List<ImageData> imageDataList) { protected void fillImage(CellWriteHandlerContext cellWriteHandlerContext, List<ImageData> imageDataList) {
if (CollectionUtils.isEmpty(imageDataList)) { if (CollectionUtils.isEmpty(imageDataList)) {
return; return;
} }
Sheet sheet = cell.getSheet(); Integer rowIndex = cellWriteHandlerContext.getRowIndex();
Integer columnIndex = cellWriteHandlerContext.getColumnIndex();
Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet();
Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook();
Drawing<?> drawing = sheet.getDrawingPatriarch(); Drawing<?> drawing = sheet.getDrawingPatriarch();
if (drawing == null) { if (drawing == null) {
drawing = sheet.createDrawingPatriarch(); drawing = sheet.createDrawingPatriarch();
} }
CreationHelper helper = sheet.getWorkbook().getCreationHelper(); CreationHelper helper = sheet.getWorkbook().getCreationHelper();
for (ImageData imageData : imageDataList) { for (ImageData imageData : imageDataList) {
int index = sheet.getWorkbook().addPicture(imageData.getImage(), int index = workbook.addPicture(imageData.getImage(),
FileTypeUtils.getImageTypeFormat(imageData.getImage())); FileTypeUtils.getImageTypeFormat(imageData.getImage()));
ClientAnchor anchor = helper.createClientAnchor(); ClientAnchor anchor = helper.createClientAnchor();
if (imageData.getTop() != null) { if (imageData.getTop() != null) {
@ -201,13 +222,13 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
if (imageData.getLeft() != null) { if (imageData.getLeft() != null) {
anchor.setDx1(StyleUtil.getCoordinate(imageData.getLeft())); anchor.setDx1(StyleUtil.getCoordinate(imageData.getLeft()));
} }
anchor.setRow1(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getFirstRowIndex(), anchor.setRow1(StyleUtil.getCellCoordinate(rowIndex, imageData.getFirstRowIndex(),
imageData.getRelativeFirstRowIndex())); imageData.getRelativeFirstRowIndex()));
anchor.setCol1(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getFirstColumnIndex(), anchor.setCol1(StyleUtil.getCellCoordinate(columnIndex, imageData.getFirstColumnIndex(),
imageData.getRelativeFirstColumnIndex())); imageData.getRelativeFirstColumnIndex()));
anchor.setRow2(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getLastRowIndex(), anchor.setRow2(StyleUtil.getCellCoordinate(rowIndex, imageData.getLastRowIndex(),
imageData.getRelativeLastRowIndex()) + 1); imageData.getRelativeLastRowIndex()) + 1);
anchor.setCol2(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getLastColumnIndex(), anchor.setCol2(StyleUtil.getCellCoordinate(columnIndex, imageData.getLastColumnIndex(),
imageData.getRelativeLastColumnIndex()) + 1); imageData.getRelativeLastColumnIndex()) + 1);
if (imageData.getAnchorType() != null) { if (imageData.getAnchorType() != null) {
anchor.setAnchorType(imageData.getAnchorType().getValue()); anchor.setAnchorType(imageData.getAnchorType().getValue());
@ -216,17 +237,16 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
} }
} }
protected WriteCellData<?> convert(WriteHolder currentWriteHolder, Class<?> clazz, CellDataTypeEnum targetType, protected WriteCellData<?> convert(CellWriteHandlerContext cellWriteHandlerContext) {
Cell cell, Object value, ExcelContentProperty excelContentProperty) {
// This means that the user has defined the data. // This means that the user has defined the data.
if (clazz == WriteCellData.class) { if (cellWriteHandlerContext.getOriginalFieldClass() == WriteCellData.class) {
if (value == null) { if (cellWriteHandlerContext.getOriginalValue() == null) {
return new WriteCellData<>(CellDataTypeEnum.EMPTY); return new WriteCellData<>(CellDataTypeEnum.EMPTY);
} }
WriteCellData<?> cellDataValue = (WriteCellData<?>)value; WriteCellData<?> cellDataValue = (WriteCellData<?>)cellWriteHandlerContext.getOriginalValue();
if (cellDataValue.getType() != null) { if (cellDataValue.getType() != null) {
// Configuration information may not be read here // Configuration information may not be read here
fillProperty(cellDataValue, excelContentProperty); fillProperty(cellDataValue, cellWriteHandlerContext.getExcelContentProperty());
return cellDataValue; return cellDataValue;
} else { } else {
@ -235,8 +255,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
return cellDataValue; return cellDataValue;
} }
} }
WriteCellData<?> cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), WriteCellData<?> cellDataReturn = doConvert(cellWriteHandlerContext);
targetType, cell, cellDataValue.getData(), excelContentProperty);
if (cellDataValue.getImageDataList() != null) { if (cellDataValue.getImageDataList() != null) {
cellDataReturn.setImageDataList(cellDataValue.getImageDataList()); cellDataReturn.setImageDataList(cellDataValue.getImageDataList());
@ -256,7 +275,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
} }
return cellDataReturn; return cellDataReturn;
} }
return doConvert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); return doConvert(cellWriteHandlerContext);
} }
private void fillProperty(WriteCellData<?> cellDataValue, ExcelContentProperty excelContentProperty) { private void fillProperty(WriteCellData<?> cellDataValue, ExcelContentProperty excelContentProperty) {
@ -280,8 +299,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
} }
} }
private WriteCellData<?> doConvert(WriteHolder currentWriteHolder, Class<?> clazz, CellDataTypeEnum targetType, private WriteCellData<?> doConvert(CellWriteHandlerContext cellWriteHandlerContext) {
Cell cell, Object value, ExcelContentProperty excelContentProperty) { ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty();
Converter<?> converter = null; Converter<?> converter = null;
if (excelContentProperty != null) { if (excelContentProperty != null) {
converter = excelContentProperty.getConverter(); converter = excelContentProperty.getConverter();
@ -289,31 +309,34 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor {
if (converter == null) { if (converter == null) {
// csv is converted to string by default // csv is converted to string by default
if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) { if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) {
targetType = CellDataTypeEnum.STRING; cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING);
} }
converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType)); converter = writeContext.currentWriteHolder().converterMap().get(
ConverterKeyBuild.buildKey(cellWriteHandlerContext.getOriginalFieldClass(),
cellWriteHandlerContext.getTargetCellDataType()));
} }
if (value == null && !(converter instanceof NullableObjectConverter)) { if (cellWriteHandlerContext.getOriginalValue() == null && !(converter instanceof NullableObjectConverter)) {
return new WriteCellData<>(CellDataTypeEnum.EMPTY); return new WriteCellData<>(CellDataTypeEnum.EMPTY);
} }
if (converter == null) { if (converter == null) {
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), throw new ExcelWriteDataConvertException(cellWriteHandlerContext,
new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, "Can not find 'Converter' support class " + cellWriteHandlerContext.getOriginalFieldClass()
"Can not find 'Converter' support class " + clazz.getSimpleName() + "."); .getSimpleName() + ".");
} }
WriteCellData<?> cellData; WriteCellData<?> cellData;
try { try {
cellData = ((Converter<Object>)converter).convertToExcelData( cellData = ((Converter<Object>)converter).convertToExcelData(
new WriteConverterContext<>(value, excelContentProperty, writeContext)); new WriteConverterContext<>(cellWriteHandlerContext.getOriginalValue(), excelContentProperty,
writeContext));
} catch (Exception e) { } catch (Exception e) {
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), throw new ExcelWriteDataConvertException(cellWriteHandlerContext,
new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " error, at row:"
"Convert data:" + value + " error, at row:" + cell.getRow().getRowNum(), e); + cellWriteHandlerContext.getRowIndex(), e);
} }
if (cellData == null || cellData.getType() == null) { if (cellData == null || cellData.getType() == null) {
throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), throw new ExcelWriteDataConvertException(cellWriteHandlerContext,
new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return null, at row:"
"Convert data:" + value + " return null, at row:" + cell.getRow().getRowNum()); + cellWriteHandlerContext.getRowIndex());
} }
return cellData; return cellData;
} }

90
src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java

@ -11,13 +11,14 @@ import java.util.TreeMap;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.ClassUtils;
import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.FieldUtils;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.util.WriteHandlerUtils; import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.metadata.CollectionRowData; import com.alibaba.excel.write.metadata.CollectionRowData;
import com.alibaba.excel.write.metadata.MapRowData; import com.alibaba.excel.write.metadata.MapRowData;
import com.alibaba.excel.write.metadata.RowData; import com.alibaba.excel.write.metadata.RowData;
@ -64,9 +65,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
if (oneRowData == null) { if (oneRowData == null) {
return; return;
} }
WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE); RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext,
rowIndex, relativeRowIndex, Boolean.FALSE);
WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext);
Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex); Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex);
WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE); rowWriteHandlerContext.setRow(row);
WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext);
if (oneRowData instanceof Collection<?>) { if (oneRowData instanceof Collection<?>) {
addBasicTypeToExcel(new CollectionRowData((Collection<?>)oneRowData), row, rowIndex, relativeRowIndex); addBasicTypeToExcel(new CollectionRowData((Collection<?>)oneRowData), row, rowIndex, relativeRowIndex);
@ -75,7 +81,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
} else { } else {
addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap); addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap);
} }
WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE);
WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext);
} }
private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) { private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) {
@ -114,23 +121,28 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(),
head == null ? null : head.getFieldName()); head == null ? null : head.getFieldName());
WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext,
excelContentProperty); row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
Cell cell = WorkBookUtil.createCell(row, columnIndex); Cell cell = WorkBookUtil.createCell(row, columnIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, cellWriteHandlerContext.setCell(cell);
excelContentProperty);
Object value = oneRowData.get(dataIndex); WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
WriteCellData<?> cellData = converterAndSet(writeContext.currentWriteHolder(),
FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex, rowIndex, columnIndex); cellWriteHandlerContext.setOriginalValue(oneRowData.get(dataIndex));
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, cellWriteHandlerContext.setOriginalFieldClass(
excelContentProperty); FieldUtils.getFieldClass(cellWriteHandlerContext.getOriginalValue()));
converterAndSet(cellWriteHandlerContext);
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
} }
private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex,
Map<Integer, Field> sortedAllFiledMap) { Map<Integer, Field> sortedAllFiledMap) {
WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); WriteHolder currentWriteHolder = writeContext.currentWriteHolder();
BeanMap beanMap = BeanMapUtils.create(oneRowData); BeanMap beanMap = BeanMapUtils.create(oneRowData);
Set<String> beanMapHandledSet = new HashSet<String>(); Set<String> beanMapHandledSet = new HashSet<>();
int maxCellIndex = -1; int maxCellIndex = -1;
// If it's a class it needs to be cast by type // If it's a class it needs to be cast by type
if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) { if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) {
@ -142,18 +154,25 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
if (!beanMap.containsKey(name)) { if (!beanMap.containsKey(name)) {
continue; continue;
} }
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap,
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name);
WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(
Boolean.FALSE, excelContentProperty); writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE,
Cell cell = WorkBookUtil.createCell(row, columnIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE,
excelContentProperty);
Object value = beanMap.get(name);
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); excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
Cell cell = WorkBookUtil.createCell(row, columnIndex);
cellWriteHandlerContext.setCell(cell);
WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
cellWriteHandlerContext.setOriginalValue(beanMap.get(name));
cellWriteHandlerContext.setOriginalFieldClass(head.getField().getType());
converterAndSet(cellWriteHandlerContext);
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
beanMapHandledSet.add(name); beanMapHandledSet.add(name);
maxCellIndex = Math.max(maxCellIndex, columnIndex); maxCellIndex = Math.max(maxCellIndex, columnIndex);
} }
@ -177,18 +196,23 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor {
Object value = beanMap.get(filedName); Object value = beanMap.get(filedName);
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap,
currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), filedName); currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), filedName);
WriteHandlerUtils.beforeCellCreate(writeContext, row, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(
excelContentProperty); writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty);
WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
// fix https://github.com/alibaba/easyexcel/issues/1870 // fix https://github.com/alibaba/easyexcel/issues/1870
// If there is data, it is written to the next cell // If there is data, it is written to the next cell
Cell cell = WorkBookUtil.createCell(row, maxCellIndex++); Cell cell = WorkBookUtil.createCell(row, maxCellIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE, cellWriteHandlerContext.setCell(cell);
excelContentProperty);
WriteCellData<?> cellData = converterAndSet(currentWriteHolder, WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex,
rowIndex, maxCellIndex); cellWriteHandlerContext.setOriginalValue(value);
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, filedName, value));
excelContentProperty); converterAndSet(cellWriteHandlerContext);
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
maxCellIndex++;
} }
} }

88
src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java

@ -24,6 +24,8 @@ import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.WriteHandlerUtils; import com.alibaba.excel.util.WriteHandlerUtils;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import com.alibaba.excel.write.metadata.fill.AnalysisCell; import com.alibaba.excel.write.metadata.fill.AnalysisCell;
import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper; import com.alibaba.excel.write.metadata.fill.FillWrapper;
@ -189,8 +191,11 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
} else { } else {
dataMap = BeanMapUtils.create(oneRowData); dataMap = BeanMapUtils.create(oneRowData);
} }
WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder();
for (AnalysisCell analysisCell : analysisCellList) { for (AnalysisCell analysisCell : analysisCellList) {
CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(
writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(),
relativeRowIndex, Boolean.FALSE, ExcelContentProperty.EMPTY);
if (analysisCell.getOnlyOneVariable()) { if (analysisCell.getOnlyOneVariable()) {
String variable = analysisCell.getVariableList().get(0); String variable = analysisCell.getVariableList().get(0);
if (!dataMap.containsKey(variable)) { if (!dataMap.containsKey(variable)) {
@ -199,12 +204,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
Object value = dataMap.get(variable); Object value = dataMap.get(variable);
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap,
writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable);
Cell cell = getOneCell(analysisCell, fillConfig, excelContentProperty); cellWriteHandlerContext.setExcelContentProperty(excelContentProperty);
createCell(analysisCell, fillConfig, cellWriteHandlerContext);
cellWriteHandlerContext.setOriginalValue(value);
cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value));
WriteCellData<?> cellData = converterAndSet(writeSheetHolder, converterAndSet(cellWriteHandlerContext);
FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null, WriteCellData<?> cellData = cellWriteHandlerContext.getFirstCellData();
relativeRowIndex, analysisCell.getRowIndex(), analysisCell.getColumnIndex());
cellData.setAnalysisCell(analysisCell);
// Restyle // Restyle
if (fillConfig.getAutoStyle()) { if (fillConfig.getAutoStyle()) {
@ -212,14 +219,15 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
.map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell))
.ifPresent(cellData::setOriginCellStyle); .ifPresent(cellData::setOriginCellStyle);
} }
WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE,
excelContentProperty);
} else { } else {
StringBuilder cellValueBuild = new StringBuilder(); StringBuilder cellValueBuild = new StringBuilder();
int index = 0; int index = 0;
List<WriteCellData<?>> cellDataList = new ArrayList<>(); List<WriteCellData<?>> cellDataList = new ArrayList<>();
Cell cell = getOneCell(analysisCell, fillConfig, ExcelContentProperty.EMPTY);
cellWriteHandlerContext.setExcelContentProperty(ExcelContentProperty.EMPTY);
createCell(analysisCell, fillConfig, cellWriteHandlerContext);
Cell cell = cellWriteHandlerContext.getCell();
for (String variable : analysisCell.getVariableList()) { for (String variable : analysisCell.getVariableList()) {
cellValueBuild.append(analysisCell.getPrepareDataList().get(index++)); cellValueBuild.append(analysisCell.getPrepareDataList().get(index++));
@ -229,11 +237,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
Object value = dataMap.get(variable); Object value = dataMap.get(variable);
ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap,
writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable);
WriteCellData<?> cellData = convert(writeSheetHolder, cellWriteHandlerContext.setOriginalValue(value);
FieldUtils.getFieldClass(dataMap, variable, value), CellDataTypeEnum.STRING, cell, value, cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value));
excelContentProperty); cellWriteHandlerContext.setExcelContentProperty(excelContentProperty);
cellData.setAnalysisCell(analysisCell); cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING);
WriteCellData<?> cellData = convert(cellWriteHandlerContext);
cellDataList.add(cellData); cellDataList.add(cellData);
CellDataTypeEnum type = cellData.getType(); CellDataTypeEnum type = cellData.getType();
if (type != null) { if (type != null) {
switch (type) { switch (type) {
@ -253,6 +264,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
} }
cellValueBuild.append(analysisCell.getPrepareDataList().get(index)); cellValueBuild.append(analysisCell.getPrepareDataList().get(index));
cell.setCellValue(cellValueBuild.toString()); cell.setCellValue(cellValueBuild.toString());
cellWriteHandlerContext.setCellDataList(cellDataList);
if (CollectionUtils.isNotEmpty(cellDataList)) {
cellWriteHandlerContext.setFirstCellData(cellDataList.get(0));
}
// Restyle // Restyle
if (fillConfig.getAutoStyle()) { if (fillConfig.getAutoStyle()) {
@ -260,10 +275,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
.map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell))
.ifPresent(cell::setCellStyle); .ifPresent(cell::setCellStyle);
} }
WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, relativeRowIndex,
Boolean.FALSE, ExcelContentProperty.EMPTY);
} }
WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext);
} }
} }
@ -278,11 +291,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
return relativeRowIndex; return relativeRowIndex;
} }
private Cell getOneCell(AnalysisCell analysisCell, FillConfig fillConfig, private void createCell(AnalysisCell analysisCell, FillConfig fillConfig,
ExcelContentProperty excelContentProperty) { CellWriteHandlerContext cellWriteHandlerContext) {
Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet(); Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet();
if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) { if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) {
return cachedSheet.getRow(analysisCell.getRowIndex()).getCell(analysisCell.getColumnIndex()); Row row = cachedSheet.getRow(analysisCell.getRowIndex());
cellWriteHandlerContext.setRow(row);
Cell cell = row.getCell(analysisCell.getColumnIndex());
cellWriteHandlerContext.setCell(cell);
} }
Sheet sheet = writeContext.writeSheetHolder().getSheet(); Sheet sheet = writeContext.writeSheetHolder().getSheet();
@ -319,31 +335,38 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
throw new ExcelGenerateException("The wrong direction."); throw new ExcelGenerateException("The wrong direction.");
} }
Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell); Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell,
Cell cell = createCellIfNecessary(row, lastColumnIndex, excelContentProperty); cellWriteHandlerContext);
cellWriteHandlerContext.setRow(row);
cellWriteHandlerContext.setRowIndex(lastRowIndex);
cellWriteHandlerContext.setColumnIndex(lastColumnIndex);
Cell cell = createCellIfNecessary(row, lastColumnIndex, cellWriteHandlerContext);
cellWriteHandlerContext.setCell(cell);
if (isOriginalCell) { if (isOriginalCell) {
Map<AnalysisCell, CellStyle> collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent( Map<AnalysisCell, CellStyle> collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent(
currentUniqueDataFlag, key -> MapUtils.newHashMap()); currentUniqueDataFlag, key -> MapUtils.newHashMap());
collectionFieldStyleMap.put(analysisCell, cell.getCellStyle()); collectionFieldStyleMap.put(analysisCell, cell.getCellStyle());
} }
return cell;
} }
private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, ExcelContentProperty excelContentProperty) { private Cell createCellIfNecessary(Row row, Integer lastColumnIndex,
CellWriteHandlerContext cellWriteHandlerContext) {
Cell cell = row.getCell(lastColumnIndex); Cell cell = row.getCell(lastColumnIndex);
if (cell != null) { if (cell != null) {
return cell; return cell;
} }
WriteHandlerUtils.beforeCellCreate(writeContext, row, null, lastColumnIndex, null, Boolean.FALSE, WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext);
excelContentProperty);
cell = row.createCell(lastColumnIndex); cell = row.createCell(lastColumnIndex);
WriteHandlerUtils.afterCellCreate(writeContext, cell, null, null, Boolean.FALSE, excelContentProperty); cellWriteHandlerContext.setCell(cell);
WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext);
return cell; return cell;
} }
private Row createRowIfNecessary(Sheet sheet, Sheet cachedSheet, Integer lastRowIndex, FillConfig fillConfig, private Row createRowIfNecessary(Sheet sheet, Sheet cachedSheet, Integer lastRowIndex, FillConfig fillConfig,
AnalysisCell analysisCell, boolean isOriginalCell) { AnalysisCell analysisCell, boolean isOriginalCell, CellWriteHandlerContext cellWriteHandlerContext) {
Row row = sheet.getRow(lastRowIndex); Row row = sheet.getRow(lastRowIndex);
if (row != null) { if (row != null) {
checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); checkRowHeight(analysisCell, fillConfig, isOriginalCell, row);
@ -351,7 +374,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
} }
row = cachedSheet.getRow(lastRowIndex); row = cachedSheet.getRow(lastRowIndex);
if (row == null) { if (row == null) {
WriteHandlerUtils.beforeRowCreate(writeContext, lastRowIndex, null, Boolean.FALSE); RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext,
lastRowIndex, null, Boolean.FALSE);
WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext);
if (fillConfig.getForceNewRow()) { if (fillConfig.getForceNewRow()) {
row = cachedSheet.createRow(lastRowIndex); row = cachedSheet.createRow(lastRowIndex);
} else { } else {
@ -364,8 +390,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor {
row = cachedSheet.createRow(lastRowIndex); row = cachedSheet.createRow(lastRowIndex);
} }
} }
rowWriteHandlerContext.setRow(row);
checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); checkRowHeight(analysisCell, fillConfig, isOriginalCell, row);
WriteHandlerUtils.afterRowCreate(writeContext, row, null, Boolean.FALSE);
WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext);
} else { } else {
checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); checkRowHeight(analysisCell, fillConfig, isOriginalCell, row);
} }

58
src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java

@ -3,14 +3,16 @@ package com.alibaba.excel.write.handler.context;
import java.util.List; import java.util.List;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -21,7 +23,6 @@ import org.apache.poi.ss.usermodel.Row;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@Data @Data
@AllArgsConstructor
public class CellWriteHandlerContext { public class CellWriteHandlerContext {
/** /**
* write context * write context
@ -43,6 +44,10 @@ public class CellWriteHandlerContext {
* row * row
*/ */
private Row row; private Row row;
/**
* index
*/
private Integer rowIndex;
/** /**
* cell * cell
*/ */
@ -78,4 +83,53 @@ public class CellWriteHandlerContext {
* Field annotation configuration information. * Field annotation configuration information.
*/ */
private ExcelContentProperty excelContentProperty; private ExcelContentProperty excelContentProperty;
/**
* The value of the original
*/
private Object originalValue;
/**
* The original field type
*/
private Class<?> originalFieldClass;
/**
* Target cell data type
*/
private CellDataTypeEnum targetCellDataType;
/**
* Ignore the filling pattern and the {@code FillStyleCellWriteHandler} will not work.
*
* @see FillStyleCellWriteHandler
*/
private Boolean ignoreFillStyle;
/**
* handler
*/
private List<WriteHandler> handlerList;
public CellWriteHandlerContext(WriteContext writeContext,
WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Cell cell, Integer columnIndex,
Integer relativeRowIndex, Head headData, List<WriteCellData<?>> cellDataList, WriteCellData<?> firstCellData,
Boolean head, ExcelContentProperty excelContentProperty, List<WriteHandler> handlerList) {
this.writeContext = writeContext;
this.writeWorkbookHolder = writeWorkbookHolder;
this.writeSheetHolder = writeSheetHolder;
this.writeTableHolder = writeTableHolder;
this.row = row;
this.rowIndex = rowIndex;
this.cell = cell;
this.columnIndex = columnIndex;
this.relativeRowIndex = relativeRowIndex;
this.headData = headData;
this.cellDataList = cellDataList;
this.firstCellData = firstCellData;
this.head = head;
this.excelContentProperty = excelContentProperty;
this.handlerList = handlerList;
}
} }

8
src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java

@ -1,6 +1,9 @@
package com.alibaba.excel.write.handler.context; package com.alibaba.excel.write.handler.context;
import java.util.List;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
@ -49,4 +52,9 @@ public class RowWriteHandlerContext {
* Nullable.It is null in the case of fill data. * Nullable.It is null in the case of fill data.
*/ */
private Boolean head; private Boolean head;
/**
* handler
*/
private List<WriteHandler> handlerList;
} }

12
src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java

@ -1,6 +1,9 @@
package com.alibaba.excel.write.handler.context; package com.alibaba.excel.write.handler.context;
import java.util.List;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
@ -27,4 +30,13 @@ public class SheetWriteHandlerContext {
* sheet * sheet
*/ */
private WriteSheetHolder writeSheetHolder; private WriteSheetHolder writeSheetHolder;
/**
* handler
*/
private List<WriteHandler> handlerList;
/**
* handler
*/
private List<WriteHandler> ownHandlerList;
} }

13
src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java

@ -1,6 +1,9 @@
package com.alibaba.excel.write.handler.context; package com.alibaba.excel.write.handler.context;
import java.util.List;
import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContext;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -22,4 +25,14 @@ public class WorkbookWriteHandlerContext {
* workbook * workbook
*/ */
private WriteWorkbookHolder writeWorkbookHolder; private WriteWorkbookHolder writeWorkbookHolder;
/**
* handler
*/
private List<WriteHandler> handlerList;
/**
* handler
*/
private List<WriteHandler> ownHandlerList;
} }

10
src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java

@ -1,16 +1,14 @@
package com.alibaba.excel.write.handler.impl; package com.alibaba.excel.write.handler.impl;
import java.util.List;
import com.alibaba.excel.constant.OrderConstant; import com.alibaba.excel.constant.OrderConstant;
import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
/** /**
@ -28,12 +26,12 @@ public class FillStyleCellWriteHandler implements CellWriteHandler {
@Override @Override
public void afterCellDispose(CellWriteHandlerContext context) { public void afterCellDispose(CellWriteHandlerContext context) {
List<WriteCellData<?>> cellDataList = context.getCellDataList(); if (BooleanUtils.isTrue(context.getIgnoreFillStyle())) {
if (CollectionUtils.size(cellDataList) != 1) {
return; return;
} }
WriteCellData<?> cellData = context.getFirstCellData(); WriteCellData<?> cellData = context.getFirstCellData();
if (cellData.getAnalysisCell() != null && !cellData.getAnalysisCell().getOnlyOneVariable()) { if (cellData == null) {
return; return;
} }
WriteCellStyle writeCellStyle = cellData.getWriteCellStyle(); WriteCellStyle writeCellStyle = cellData.getWriteCellStyle();

1
src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java

@ -430,4 +430,5 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
public boolean automaticMergeHead() { public boolean automaticMergeHead() {
return getAutomaticMergeHead(); return getAutomaticMergeHead();
} }
} }

2
src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java

@ -9,9 +9,7 @@ import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.Data; import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFSheet;

6
src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

@ -19,6 +19,7 @@ import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.IoUtils; import com.alibaba.excel.util.IoUtils;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
import com.alibaba.excel.util.StyleUtil; import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext;
import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.WriteWorkbook;
import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
@ -143,6 +144,11 @@ public class WriteWorkbookHolder extends AbstractWriteHolder {
*/ */
private Map<DataFormatData, Short> dataFormatMap; private Map<DataFormatData, Short> dataFormatMap;
/**
* handler context
*/
private WorkbookWriteHandlerContext workbookWriteHandlerContext;
public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { public WriteWorkbookHolder(WriteWorkbook writeWorkbook) {
super(writeWorkbook, null); super(writeWorkbook, null);
this.writeWorkbook = writeWorkbook; this.writeWorkbook = writeWorkbook;

3
update.md

@ -1,3 +1,6 @@
# 3.0.2
* 修复写入的性能问题
# 3.0.1 # 3.0.1
* 升级到正式版 * 升级到正式版
* 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124) * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124)

Loading…
Cancel
Save