diff --git a/README.md b/README.md index 660af477..f0ce3ad6 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ EasyExcel # JAVA解析Excel工具EasyExcel Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便 -## 64M内存1分钟内读取75M(46W行25列)的Excel +## 64M内存20秒内读取75M(46W行25列)的Excel(3.0.2+版本) 当然还有极速模式能更快,但是内存占用会在100M多一点 ![img](img/readme/large.png) @@ -41,7 +41,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都 com.alibaba easyexcel - 3.0.1 + 3.0.2 ``` diff --git a/img/readme/large.png b/img/readme/large.png index 04195a10..48223c56 100644 Binary files a/img/readme/large.png and b/img/readme/large.png differ diff --git a/pom.xml b/pom.xml index 66331d1b..00bae497 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.alibaba easyexcel - 3.0.1 + 3.0.2 jar easyexcel diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index 329f5250..ea8b87b1 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -86,9 +86,9 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { // set style table setStylesTable(xlsxReadWorkbookHolder, xssfReader); - sheetList = new ArrayList(); - sheetMap = new HashMap(); - commentsTableMap = new HashMap(); + sheetList = new ArrayList<>(); + sheetMap = new HashMap<>(); + commentsTableMap = new HashMap<>(); XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator)xssfReader.getSheetsData(); int index = 0; if (!ite.hasNext()) { diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java index a6fd5340..9d8e4736 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CellTagHandler.java @@ -2,19 +2,15 @@ package com.alibaba.excel.analysis.v07.handlers; import java.math.BigDecimal; -import com.alibaba.excel.constant.BuiltinFormats; import com.alibaba.excel.constant.ExcelXmlConstants; import com.alibaba.excel.context.xlsx.XlsxReadContext; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.StringUtils; -import org.apache.poi.xssf.model.StylesTable; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.xml.sax.Attributes; /** @@ -51,17 +47,9 @@ public class CellTagHandler extends AbstractXlsxTagHandler { } else { dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); } - StylesTable stylesTable = xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable(); - if (stylesTable == null) { - return; - } - XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger); - short dataFormat = xssfCellStyle.getDataFormat(); - DataFormatData dataFormatData = new DataFormatData(); - dataFormatData.setIndex(dataFormat); - dataFormatData.setFormat(BuiltinFormats.getBuiltinFormat(dataFormat, - xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale())); - xlsxReadSheetHolder.getTempCellData().setDataFormatData(dataFormatData); + + xlsxReadSheetHolder.getTempCellData().setDataFormatData( + xlsxReadContext.xlsxReadWorkbookHolder().dataFormatData(dateFormatIndexInteger)); } @Override diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index f31705f7..db892989 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -15,10 +15,15 @@ import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.FileUtils; +import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.NumberDataFormatterUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; +import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteWorkbook; @@ -83,13 +88,16 @@ public class WriteContextImpl implements WriteContext { LOGGER.debug("Begin to Initialization 'WriteContextImpl'"); } initCurrentWorkbookHolder(writeWorkbook); - WriteHandlerUtils.beforeWorkbookCreate(this); + + WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( + this); + WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext); try { WorkBookUtil.createWorkBook(writeWorkbookHolder); } catch (Exception e) { throw new ExcelGenerateException("Create workbook failure", e); } - WriteHandlerUtils.afterWorkbookCreate(this); + WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Initialization 'WriteContextImpl' complete"); } @@ -118,8 +126,10 @@ public class WriteContextImpl implements WriteContext { initCurrentSheetHolder(writeSheet); // Workbook handler need to supplementary execution - WriteHandlerUtils.beforeWorkbookCreate(this, true); - WriteHandlerUtils.afterWorkbookCreate(this, true); + WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( + this); + WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); + WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); // Initialization current sheet initSheet(writeType); @@ -162,7 +172,8 @@ public class WriteContextImpl implements WriteContext { } private void initSheet(WriteTypeEnum writeType) { - WriteHandlerUtils.beforeSheetCreate(this); + SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this); + WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext); Sheet currentSheet; try { if (writeSheetHolder.getSheetNo() != null) { @@ -192,7 +203,7 @@ public class WriteContextImpl implements WriteContext { currentSheet = createSheet(); } writeSheetHolder.setSheet(currentSheet); - WriteHandlerUtils.afterSheetCreate(this); + WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext); if (WriteTypeEnum.ADD.equals(writeType)) { // Initialization head initHead(writeSheetHolder.excelWriteHeadProperty()); @@ -226,11 +237,17 @@ public class WriteContextImpl implements WriteContext { } for (int relativeRowIndex = 0, i = newRowIndex; i < excelWriteHeadProperty.getHeadRowNumber() + newRowIndex; i++, relativeRowIndex++) { - WriteHandlerUtils.beforeRowCreate(this, newRowIndex, relativeRowIndex, Boolean.TRUE); + + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(this, + newRowIndex, relativeRowIndex, Boolean.TRUE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + Row row = WorkBookUtil.createRow(writeSheetHolder.getSheet(), i); - WriteHandlerUtils.afterRowCreate(this, row, relativeRowIndex, Boolean.TRUE); - addOneRowOfHeadDataToExcel(row, excelWriteHeadProperty.getHeadMap(), relativeRowIndex); - WriteHandlerUtils.afterRowDispose(this, row, relativeRowIndex, Boolean.TRUE); + rowWriteHandlerContext.setRow(row); + + 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 headMap, int relativeRowIndex) { + private void addOneRowOfHeadDataToExcel(Row row, Integer rowIndex, Map headMap, + int relativeRowIndex) { for (Map.Entry entry : headMap.entrySet()) { Head head = entry.getValue(); int columnIndex = entry.getKey(); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(null, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), head.getFieldName()); - WriteHandlerUtils.beforeCellCreate(this, row, head, columnIndex, relativeRowIndex, Boolean.TRUE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(this, row, + rowIndex, head, columnIndex, relativeRowIndex, Boolean.TRUE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); Cell cell = row.createCell(columnIndex); + cellWriteHandlerContext.setCell(cell); - WriteHandlerUtils.afterCellCreate(this, cell, head, relativeRowIndex, Boolean.TRUE, excelContentProperty); + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); WriteCellData writeCellData = new WriteCellData<>(head.getHeadNameList().get(relativeRowIndex)); cell.setCellValue(writeCellData.getStringValue()); + cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(writeCellData)); + cellWriteHandlerContext.setFirstCellData(writeCellData); - WriteHandlerUtils.afterCellDispose(this, writeCellData, cell, head, relativeRowIndex, Boolean.TRUE, - excelContentProperty); + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } } @@ -288,10 +309,14 @@ public class WriteContextImpl implements WriteContext { initCurrentTableHolder(writeTable); // Workbook and sheet handler need to supplementary execution - WriteHandlerUtils.beforeWorkbookCreate(this, true); - WriteHandlerUtils.afterWorkbookCreate(this, true); - WriteHandlerUtils.beforeSheetCreate(this, true); - WriteHandlerUtils.afterSheetCreate(this, true); + WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( + this); + WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); + WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); + + SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this); + WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext, true); + WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext, true); initHead(writeTableHolder.excelWriteHeadProperty()); } @@ -331,7 +356,7 @@ public class WriteContextImpl implements WriteContext { return; } finished = true; - WriteHandlerUtils.afterWorkbookDispose(this); + WriteHandlerUtils.afterWorkbookDispose(writeWorkbookHolder.getWorkbookWriteHandlerContext()); if (writeWorkbookHolder == null) { return; } diff --git a/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java index f1068591..d6417be7 100644 --- a/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java +++ b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java @@ -5,6 +5,11 @@ import java.util.Map; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.util.MapUtils; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + /** * Converter unique key.Consider that you can just use class as the key. * @@ -12,33 +17,37 @@ import com.alibaba.excel.util.MapUtils; */ public class ConverterKeyBuild { - private static final Map BOXING_MAP = MapUtils.newHashMap(); + private static final Map, Class>> BOXING_MAP = MapUtils.newHashMap(); static { - BOXING_MAP.put(int.class.getName(), Integer.class.getName()); - BOXING_MAP.put(byte.class.getName(), Byte.class.getName()); - BOXING_MAP.put(long.class.getName(), Long.class.getName()); - BOXING_MAP.put(double.class.getName(), Double.class.getName()); - BOXING_MAP.put(float.class.getName(), Float.class.getName()); - BOXING_MAP.put(char.class.getName(), Character.class.getName()); - BOXING_MAP.put(short.class.getName(), Short.class.getName()); - BOXING_MAP.put(boolean.class.getName(), Boolean.class.getName()); + BOXING_MAP.put(int.class, Integer.class); + BOXING_MAP.put(byte.class, Byte.class); + BOXING_MAP.put(long.class, Long.class); + BOXING_MAP.put(double.class, Double.class); + BOXING_MAP.put(float.class, Float.class); + BOXING_MAP.put(char.class, Character.class); + BOXING_MAP.put(short.class, Short.class); + BOXING_MAP.put(boolean.class, Boolean.class); } - public static String buildKey(Class> clazz) { - String className = clazz.getName(); - String boxingClassName = BOXING_MAP.get(clazz.getName()); - if (boxingClassName == null) { - return className; - } - return boxingClassName; + public static ConverterKey buildKey(Class> clazz) { + return buildKey(clazz, null); } - public static String buildKey(Class> clazz, CellDataTypeEnum cellDataTypeEnum) { - String key = buildKey(clazz); - if (cellDataTypeEnum == null) { - return key; + public static ConverterKey buildKey(Class> clazz, CellDataTypeEnum cellDataTypeEnum) { + Class> boxingClass = BOXING_MAP.get(clazz); + if (boxingClass != null) { + return new ConverterKey(boxingClass, cellDataTypeEnum); } - return key + "-" + cellDataTypeEnum; + return new ConverterKey(clazz, cellDataTypeEnum); + } + + @Getter + @Setter + @EqualsAndHashCode + @AllArgsConstructor + public static class ConverterKey { + private Class> clazz; + private CellDataTypeEnum cellDataTypeEnum; } } diff --git a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index fda84b84..ac2dd8dc 100644 --- a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java @@ -2,6 +2,7 @@ package com.alibaba.excel.converters; import java.util.Map; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter; @@ -52,8 +53,8 @@ import com.alibaba.excel.util.MapUtils; * @author Jiaju Zhuang */ public class DefaultConverterLoader { - private static Map> defaultWriteConverter; - private static Map> allConverter; + private static Map> defaultWriteConverter; + private static Map> allConverter; static { initDefaultWriteConverter(); @@ -153,7 +154,7 @@ public class DefaultConverterLoader { * * @return */ - public static Map> loadDefaultWriteConverter() { + public static Map> loadDefaultWriteConverter() { return defaultWriteConverter; } @@ -171,7 +172,7 @@ public class DefaultConverterLoader { * * @return */ - public static Map> loadDefaultReadConverter() { + public static Map> loadDefaultReadConverter() { return loadAllConverter(); } @@ -180,7 +181,7 @@ public class DefaultConverterLoader { * * @return */ - public static Map> loadAllConverter() { + public static Map> loadAllConverter() { return allConverter; } diff --git a/src/main/java/com/alibaba/excel/converters/ReadConverterContext.java b/src/main/java/com/alibaba/excel/converters/ReadConverterContext.java index e1367065..9c7970cd 100644 --- a/src/main/java/com/alibaba/excel/converters/ReadConverterContext.java +++ b/src/main/java/com/alibaba/excel/converters/ReadConverterContext.java @@ -5,14 +5,18 @@ import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * read converter context * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor public class ReadConverterContext { /** diff --git a/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java b/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java index cb01c773..46abda47 100644 --- a/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java +++ b/src/main/java/com/alibaba/excel/converters/WriteConverterContext.java @@ -4,15 +4,19 @@ import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.metadata.property.ExcelContentProperty; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * write converter context * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor @AllArgsConstructor public class WriteConverterContext { diff --git a/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java b/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java index 1cb9c934..ff1b409d 100644 --- a/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java +++ b/src/main/java/com/alibaba/excel/exception/ExcelDataConvertException.java @@ -4,14 +4,18 @@ import com.alibaba.excel.metadata.data.CellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.write.builder.ExcelWriterBuilder; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Data convert exception * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcelDataConvertException extends RuntimeException { /** * NotNull. @@ -32,7 +36,7 @@ public class ExcelDataConvertException extends RuntimeException { */ private ExcelContentProperty excelContentProperty; - public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, + public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData> cellData, ExcelContentProperty excelContentProperty, String message) { super(message); this.rowIndex = rowIndex; @@ -41,7 +45,7 @@ public class ExcelDataConvertException extends RuntimeException { this.excelContentProperty = excelContentProperty; } - public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData cellData, + public ExcelDataConvertException(Integer rowIndex, Integer columnIndex, CellData> cellData, ExcelContentProperty excelContentProperty, String message, Throwable cause) { super(message, cause); this.rowIndex = rowIndex; diff --git a/src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java b/src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java new file mode 100644 index 00000000..b8e3fcb3 --- /dev/null +++ b/src/main/java/com/alibaba/excel/exception/ExcelWriteDataConvertException.java @@ -0,0 +1,36 @@ +package com.alibaba.excel.exception; + +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Data convert exception + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class ExcelWriteDataConvertException extends ExcelDataConvertException { + /** + * handler. + */ + private CellWriteHandlerContext cellWriteHandlerContext; + + public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message) { + super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(), + cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message); + this.cellWriteHandlerContext = cellWriteHandlerContext; + } + + public ExcelWriteDataConvertException(CellWriteHandlerContext cellWriteHandlerContext, String message, + Throwable cause) { + super(cellWriteHandlerContext.getRowIndex(), cellWriteHandlerContext.getColumnIndex(), + cellWriteHandlerContext.getFirstCellData(), cellWriteHandlerContext.getExcelContentProperty(), message, + cause); + this.cellWriteHandlerContext = cellWriteHandlerContext; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractCell.java b/src/main/java/com/alibaba/excel/metadata/AbstractCell.java index 6647cf0c..9650b303 100644 --- a/src/main/java/com/alibaba/excel/metadata/AbstractCell.java +++ b/src/main/java/com/alibaba/excel/metadata/AbstractCell.java @@ -1,13 +1,17 @@ package com.alibaba.excel.metadata; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * cell * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class AbstractCell implements Cell { /** * Row index diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java index 735a0623..839a8b4b 100644 --- a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java @@ -4,16 +4,21 @@ import java.util.List; import java.util.Map; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * Write/read holder * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public abstract class AbstractHolder implements ConfigurationHolder { /** @@ -38,7 +43,7 @@ public abstract class AbstractHolder implements ConfigurationHolder { * * Write key: */ - private Map> converterMap; + private Map> converterMap; public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) { this.newInitialization = Boolean.TRUE; @@ -81,7 +86,7 @@ public abstract class AbstractHolder implements ConfigurationHolder { } @Override - public Map> converterMap() { + public Map> converterMap() { return getConverterMap(); } diff --git a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java index f574f10e..0c11f942 100644 --- a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java +++ b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java @@ -5,14 +5,18 @@ import java.util.Locale; import com.alibaba.excel.converters.Converter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Basic parameter * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class BasicParameter { /** * You can only choose one of the {@link BasicParameter#head} and {@link BasicParameter#clazz} diff --git a/src/main/java/com/alibaba/excel/metadata/CellRange.java b/src/main/java/com/alibaba/excel/metadata/CellRange.java index bd0f54f9..8552d09f 100644 --- a/src/main/java/com/alibaba/excel/metadata/CellRange.java +++ b/src/main/java/com/alibaba/excel/metadata/CellRange.java @@ -1,11 +1,15 @@ package com.alibaba.excel.metadata; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellRange { private int firstRow; diff --git a/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java b/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java index ee4d7edf..e842466d 100644 --- a/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java @@ -3,9 +3,9 @@ package com.alibaba.excel.metadata; import java.util.Map; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; /** - * * Get the corresponding holder * * @author Jiaju Zhuang @@ -13,7 +13,6 @@ import com.alibaba.excel.converters.Converter; public interface ConfigurationHolder extends Holder { /** - * * Record whether it's new or from cache * * @return Record whether it's new or from cache @@ -32,5 +31,5 @@ public interface ConfigurationHolder extends Holder { * * @return Converter */ - Map> converterMap(); + Map> converterMap(); } diff --git a/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java index 987e553b..5cafe90c 100644 --- a/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java +++ b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java @@ -2,14 +2,18 @@ package com.alibaba.excel.metadata; import java.util.Locale; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Global configuration * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class GlobalConfiguration { /** * Automatic trim includes sheet name and content diff --git a/src/main/java/com/alibaba/excel/metadata/Head.java b/src/main/java/com/alibaba/excel/metadata/Head.java index 7e1fd47b..5aa1cc3a 100644 --- a/src/main/java/com/alibaba/excel/metadata/Head.java +++ b/src/main/java/com/alibaba/excel/metadata/Head.java @@ -10,14 +10,18 @@ import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty; import com.alibaba.excel.metadata.property.StyleProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * excel head * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class Head { /** * Column index of head diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java index f0e925e6..72beb30a 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java @@ -10,7 +10,7 @@ import com.alibaba.excel.enums.NumericCellTypeEnum; import com.alibaba.excel.metadata.data.FormulaData; import lombok.AccessLevel; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.apache.poi.ss.SpreadsheetVersion; @@ -29,7 +29,9 @@ import org.apache.poi.ss.util.CellRangeAddress; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvCell extends CellBase { /** diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java index 08b46e3a..a992f213 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java @@ -2,7 +2,9 @@ package com.alibaba.excel.metadata.csv; import com.alibaba.excel.metadata.data.DataFormatData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Color; @@ -16,7 +18,9 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvCellStyle implements CellStyle { /** diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java index 24acd9d9..89e76a71 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java @@ -3,7 +3,9 @@ package com.alibaba.excel.metadata.csv; import java.util.Iterator; import java.util.List; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; import org.apache.poi.ss.usermodel.Cell; @@ -17,7 +19,9 @@ import org.apache.poi.ss.usermodel.Sheet; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvRow implements Row { /** diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java index b002b6b4..79331677 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java @@ -16,7 +16,9 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.NumberDataFormatterUtils; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import org.apache.poi.ss.usermodel.AutoFilter; @@ -45,7 +47,9 @@ import org.apache.poi.ss.util.PaneInformation; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvSheet implements Sheet, Closeable { /** * workbook diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java index e26a2b65..4e713509 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java @@ -6,7 +6,9 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.compress.utils.Lists; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.udf.UDFFinder; @@ -26,7 +28,9 @@ import org.apache.poi.ss.usermodel.Workbook; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvWorkbook implements Workbook { /** * output diff --git a/src/main/java/com/alibaba/excel/metadata/data/CellData.java b/src/main/java/com/alibaba/excel/metadata/data/CellData.java index ed6085f9..2f74ea9b 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/CellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/CellData.java @@ -6,7 +6,9 @@ import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.AbstractCell; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Excel internal cell data. @@ -15,7 +17,9 @@ import lombok.Data; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellData extends AbstractCell { /** * cell type diff --git a/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java b/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java index 2416635d..1963133d 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java @@ -1,7 +1,8 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.util.Internal; @@ -21,7 +22,9 @@ import org.apache.poi.util.Internal; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ClientAnchorData extends CoordinateData { /** * top diff --git a/src/main/java/com/alibaba/excel/metadata/data/CommentData.java b/src/main/java/com/alibaba/excel/metadata/data/CommentData.java index 08cff272..6373b0a6 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/CommentData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/CommentData.java @@ -1,13 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * comment * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CommentData extends ClientAnchorData { /** * Name of the original comment author diff --git a/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java b/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java index 69242e57..0755662b 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java @@ -1,13 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * coordinate. * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CoordinateData { /** * first row index.Priority is higher than {@link #relativeFirstRowIndex}. diff --git a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java index 877ca270..4f447632 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java @@ -2,14 +2,18 @@ package com.alibaba.excel.metadata.data; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * data format * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataFormatData { /** * index diff --git a/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java b/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java index 36d0206b..f71ff2eb 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java @@ -1,13 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * formula * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FormulaData { /** * formula diff --git a/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java b/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java index 3fa23893..03edad13 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java @@ -1,14 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; /** * hyperlink * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HyperlinkData extends CoordinateData { /** * Depending on the hyperlink type it can be URL, e-mail, path to a file, etc diff --git a/src/main/java/com/alibaba/excel/metadata/data/ImageData.java b/src/main/java/com/alibaba/excel/metadata/data/ImageData.java index 20cd43e1..2ca1cdc8 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/ImageData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/ImageData.java @@ -1,14 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; /** * image * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ImageData extends ClientAnchorData { /** diff --git a/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java b/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java index 68231a19..e1bb57ca 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java @@ -4,15 +4,19 @@ import java.math.BigDecimal; import com.alibaba.excel.enums.CellDataTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * read cell data * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class ReadCellData extends CellData { /** diff --git a/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java b/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java index 1717741a..d277614f 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java @@ -6,15 +6,19 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.metadata.style.WriteFont; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * rich text string * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class RichTextStringData { private String textString; @@ -25,7 +29,9 @@ public class RichTextStringData { this.textString = textString; } - @Data + @Getter + @Setter + @EqualsAndHashCode @AllArgsConstructor public static class IntervalFont { private Integer startIndex; diff --git a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java index 5a3487bf..f8ce9064 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java @@ -8,11 +8,12 @@ import java.util.List; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.util.ListUtils; -import com.alibaba.excel.write.metadata.fill.AnalysisCell; import com.alibaba.excel.write.metadata.style.WriteCellStyle; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import org.apache.poi.ss.usermodel.CellStyle; /** @@ -20,7 +21,9 @@ import org.apache.poi.ss.usermodel.CellStyle; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class WriteCellData extends CellData { /** @@ -56,10 +59,6 @@ public class WriteCellData extends CellData { */ private CellStyle originCellStyle; - /** - * Only in the case of the fill is not null - */ - private AnalysisCell analysisCell; public WriteCellData(String stringValue) { this(CellDataTypeEnum.STRING, stringValue); diff --git a/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java b/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java index bf76404f..ba8fee47 100644 --- a/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java +++ b/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java @@ -111,6 +111,8 @@ public class DataFormatter { */ private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})"); + private static final Pattern E_NOTATION_PATTERN = Pattern.compile("E(\\d)"); + /** * Cells formatted with a date or time format and which contain invalid date or time values show 255 pound signs * ("#"). @@ -644,8 +646,7 @@ public class DataFormatter { */ private String getFormattedNumberString(BigDecimal data, Short dataFormat, String dataFormatString) { Format numberFormat = getFormat(data.doubleValue(), dataFormat, dataFormatString); - String formatted = numberFormat.format(data); - return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation + return E_NOTATION_PATTERN.matcher(numberFormat.format(data)).replaceFirst("E+$1"); } /** diff --git a/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java b/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java index d2bb20cd..10752b74 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java @@ -3,14 +3,18 @@ package com.alibaba.excel.metadata.property; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.util.BooleanUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Configuration from annotations * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DateTimeFormatProperty { private String format; private Boolean use1904windowing; diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java index 1c0bb232..f0b72198 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java @@ -4,12 +4,16 @@ import java.lang.reflect.Field; import com.alibaba.excel.converters.Converter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcelContentProperty { public static final ExcelContentProperty EMPTY = new ExcelContentProperty(); diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index a0359fb3..f3a8474a 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -17,7 +17,9 @@ import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +29,9 @@ import org.slf4j.LoggerFactory; * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcelHeadProperty { private static final Logger LOGGER = LoggerFactory.getLogger(ExcelHeadProperty.class); diff --git a/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java b/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java index 619cfe6b..c280b921 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java @@ -4,7 +4,9 @@ import com.alibaba.excel.annotation.write.style.ContentFontStyle; import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.ss.usermodel.Font; @@ -15,7 +17,9 @@ import org.apache.poi.ss.usermodel.IndexedColors; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FontProperty { /** * The name for the font (i.e. Arial) diff --git a/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java b/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java index 4ef87f68..bb469cd3 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java @@ -5,7 +5,9 @@ import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.write.metadata.style.WriteFont; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.FillPatternType; @@ -19,7 +21,9 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class StyleProperty { /** * Set the data format (must be a valid format). Built in formats are defined at {@link BuiltinFormats}. diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 7ba8b88f..5c8bf4bb 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -91,7 +91,6 @@ public class ModelBuildEventListener implements ReadListener headMap = excelReadHeadProperty.getHeadMap(); - Map map = MapUtils.newHashMapWithExpectedSize(headMap.size()); BeanMap dataMap = BeanMapUtils.create(resultModel); for (Map.Entry entry : headMap.entrySet()) { Integer index = entry.getKey(); @@ -105,10 +104,9 @@ public class ModelBuildEventListener implements ReadListener(); } - - public Deque getTagDeque() { - return tagDeque; - } - - public void setTagDeque(Deque tagDeque) { - this.tagDeque = tagDeque; - } - - public Integer getColumnIndex() { - return columnIndex; - } - - public void setColumnIndex(Integer columnIndex) { - this.columnIndex = columnIndex; - } - - public StringBuilder getTempData() { - return tempData; - } - - public void setTempData(StringBuilder tempData) { - this.tempData = tempData; - } - - public StringBuilder getTempFormula() { - return tempFormula; - } - - public void setTempFormula(StringBuilder tempFormula) { - this.tempFormula = tempFormula; - } } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java index 066131d3..4e70aa0e 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java @@ -1,19 +1,31 @@ package com.alibaba.excel.read.metadata.holder.xlsx; -import javax.xml.parsers.SAXParserFactory; +import java.util.Map; -import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.xssf.model.StylesTable; +import javax.xml.parsers.SAXParserFactory; +import com.alibaba.excel.constant.BuiltinFormats; +import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.excel.util.MapUtils; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; /** * Workbook holder * * @author Jiaju Zhuang */ +@Getter +@Setter +@EqualsAndHashCode public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { /** * Package @@ -34,34 +46,30 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { * Current style information */ private StylesTable stylesTable; + /** + * cache data format + */ + private Map dataFormatDataCache; public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook); this.saxParserFactoryName = readWorkbook.getXlsxSAXParserFactoryName(); setExcelType(ExcelTypeEnum.XLSX); + dataFormatDataCache = MapUtils.newHashMap(); } - public OPCPackage getOpcPackage() { - return opcPackage; + public DataFormatData dataFormatData(int dateFormatIndexInteger) { + return dataFormatDataCache.computeIfAbsent(dateFormatIndexInteger, key -> { + DataFormatData dataFormatData = new DataFormatData(); + if (stylesTable == null) { + return null; + } + XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger); + dataFormatData.setIndex(xssfCellStyle.getDataFormat()); + dataFormatData.setFormat(BuiltinFormats.getBuiltinFormat(dataFormatData.getIndex(), + xssfCellStyle.getDataFormatString(), globalConfiguration().getLocale())); + return dataFormatData; + }); } - public void setOpcPackage(OPCPackage opcPackage) { - this.opcPackage = opcPackage; - } - - public String getSaxParserFactoryName() { - return saxParserFactoryName; - } - - public void setSaxParserFactoryName(String saxParserFactoryName) { - this.saxParserFactoryName = saxParserFactoryName; - } - - public StylesTable getStylesTable() { - return stylesTable; - } - - public void setStylesTable(StylesTable stylesTable) { - this.stylesTable = stylesTable; - } } diff --git a/src/main/java/com/alibaba/excel/util/ClassUtils.java b/src/main/java/com/alibaba/excel/util/ClassUtils.java index 9020d56c..82f78af2 100644 --- a/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -31,6 +31,10 @@ import com.alibaba.excel.metadata.property.NumberFormatProperty; import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.metadata.holder.WriteHolder; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import net.sf.cglib.beans.BeanMap; /** @@ -51,7 +55,7 @@ public class ClassUtils { /** * The cache configuration information for each of the class */ - public static final Map CONTENT_CACHE = new ConcurrentHashMap<>(); + public static final Map CONTENT_CACHE = new ConcurrentHashMap<>(); /** * Calculate the configuration information for the class @@ -117,20 +121,8 @@ public class ClassUtils { } } - private static String buildKey(Class> clazz, Class> headClass, String fieldName) { - String key = ""; - if (clazz != null) { - key += clazz.getName(); - } - key += "-"; - if (headClass != null) { - key += headClass.getName(); - } - key += "-"; - if (fieldName != null) { - key += fieldName; - } - return key; + private static ContentPropertyKey buildKey(Class> clazz, Class> headClass, String fieldName) { + return new ContentPropertyKey(clazz, headClass, fieldName); } private static Map declaredFieldContentMap(Class> clazz) { @@ -413,4 +405,13 @@ public class ClassUtils { } } + @Getter + @Setter + @EqualsAndHashCode + @AllArgsConstructor + public static class ContentPropertyKey { + private Class> clazz; + private Class> headClass; + private String fieldName; + } } diff --git a/src/main/java/com/alibaba/excel/util/ConverterUtils.java b/src/main/java/com/alibaba/excel/util/ConverterUtils.java index b666be6e..02ea4a00 100644 --- a/src/main/java/com/alibaba/excel/util/ConverterUtils.java +++ b/src/main/java/com/alibaba/excel/util/ConverterUtils.java @@ -8,10 +8,12 @@ import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKeyBuild; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.ReadConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.exception.ExcelDataConvertException; +import com.alibaba.excel.metadata.data.CellData; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; @@ -80,7 +82,7 @@ public class ConverterUtils { * @return */ public static Object convertToJavaObject(ReadCellData> cellData, Field field, - ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, + ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) { Class> clazz; if (field == null) { @@ -88,7 +90,7 @@ public class ConverterUtils { } else { clazz = field.getType(); } - if (clazz == ReadCellData.class) { + if (clazz == CellData.class || clazz == ReadCellData.class) { Class> classGeneric = getClassGeneric(field.getGenericType()); ReadCellData cellDataReturn = cellData.clone(); cellDataReturn.setData(doConvertToJavaObject(cellData, classGeneric, contentProperty, converterMap, @@ -126,7 +128,7 @@ public class ConverterUtils { * @return */ private static Object doConvertToJavaObject(ReadCellData> cellData, Class> clazz, - ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, + ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) { Converter> converter = null; if (contentProperty != null) { diff --git a/src/main/java/com/alibaba/excel/util/PositionUtils.java b/src/main/java/com/alibaba/excel/util/PositionUtils.java index 48c5cde5..fe7e5d4e 100644 --- a/src/main/java/com/alibaba/excel/util/PositionUtils.java +++ b/src/main/java/com/alibaba/excel/util/PositionUtils.java @@ -1,11 +1,7 @@ package com.alibaba.excel.util; -import java.util.Locale; -import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.poi.ss.util.CellReference; - /** * @author jipengfei */ @@ -18,14 +14,6 @@ public class PositionUtils { private PositionUtils() {} - public static int getRowByRowTagt(String rowTagt) { - int row = 0; - if (rowTagt != null) { - row = Integer.parseInt(rowTagt) - 1; - } - return row; - } - public static int getRowByRowTagt(String rowTagt, Integer before) { int row; if (rowTagt != null) { @@ -37,47 +25,40 @@ public class PositionUtils { } return before + 1; } - } public static int getRow(String currentCellIndex) { - if (currentCellIndex != null) { - int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER); - String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT); - Matcher matcher = CELL_REF_PATTERN.matcher(cell); - if (!matcher.matches()) { - throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex); + if (currentCellIndex == null) { + return -1; + } + int firstNumber = currentCellIndex.length() - 1; + for (; firstNumber >= 0; firstNumber--) { + char c = currentCellIndex.charAt(firstNumber); + if (c < '0' || c > '9') { + break; } - String row = matcher.group(2); - return Integer.parseInt(row) - 1; } - return -1; + return Integer.parseUnsignedInt(currentCellIndex.substring(firstNumber + 1)) - 1; } public static int getCol(String currentCellIndex, Integer before) { - if (currentCellIndex != null) { - int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER); - String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT); - Matcher matcher = CELL_REF_PATTERN.matcher(cell); - if (!matcher.matches()) { - throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex); - } - String col = matcher.group(1); - - if (col.length() > 0 && col.charAt(0) == REDUNDANT_CHARACTERS) { - col = col.substring(1); - } - if (col.length() == 0) { - return -1; - } else { - return CellReference.convertColStringToIndex(col); - } - } else { + if (currentCellIndex == null) { if (before == null) { before = -1; } return before + 1; } + int firstNumber = currentCellIndex.charAt(0) == REDUNDANT_CHARACTERS ? 1 : 0; + int col = 0; + for (; firstNumber < currentCellIndex.length(); firstNumber++) { + char c = currentCellIndex.charAt(firstNumber); + boolean isNotLetter = c == REDUNDANT_CHARACTERS || (c >= '0' && c <= '9'); + if (isNotLetter) { + break; + } + col = col * 26 + Character.toUpperCase(c) - 'A' + 1; + } + return col - 1; } } diff --git a/src/main/java/com/alibaba/excel/util/StyleUtil.java b/src/main/java/com/alibaba/excel/util/StyleUtil.java index 1bafcdc8..e8a503d7 100644 --- a/src/main/java/com/alibaba/excel/util/StyleUtil.java +++ b/src/main/java/com/alibaba/excel/util/StyleUtil.java @@ -1,5 +1,7 @@ package com.alibaba.excel.util; +import java.util.Optional; + import com.alibaba.excel.constant.BuiltinFormats; import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.metadata.data.HyperlinkData; @@ -189,7 +191,11 @@ public class StyleUtil { xssfFont.setStrikeout(xssfOriginFont.getStrikeout()); // Colors cannot be overwritten if (writeFont == null || writeFont.getColor() == null) { - xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null)); + xssfFont.setColor(Optional.of(xssfOriginFont) + .map(XSSFFont::getXSSFColor) + .map(XSSFColor::getRGB) + .map(rgb -> new XSSFColor(rgb, null)) + .orElse(null)); } xssfFont.setTypeOffset(xssfOriginFont.getTypeOffset()); xssfFont.setUnderline(xssfOriginFont.getUnderline()); diff --git a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java index 78ce8d8e..062bfdc7 100644 --- a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java +++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java @@ -1,24 +1,19 @@ package com.alibaba.excel.util; -import java.util.List; -import java.util.Map; - import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; -import com.alibaba.excel.write.handler.CellWriteHandler; -import com.alibaba.excel.write.handler.RowWriteHandler; -import com.alibaba.excel.write.handler.SheetWriteHandler; -import com.alibaba.excel.write.handler.WorkbookWriteHandler; -import com.alibaba.excel.write.handler.WriteHandler; +import com.alibaba.excel.write.handler.chain.CellHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.RowHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.SheetHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.WorkbookHandlerExecutionChain; 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.holder.AbstractWriteHolder; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.poi.ss.usermodel.Cell; +import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.Row; /** @@ -26,228 +21,162 @@ import org.apache.poi.ss.usermodel.Row; * * @author Jiaju Zhuang */ +@Slf4j public class WriteHandlerUtils { private WriteHandlerUtils() {} - public static void beforeWorkbookCreate(WriteContext writeContext) { - beforeWorkbookCreate(writeContext, false); + public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext) { + WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, + writeContext.writeWorkbookHolder()); + writeContext.writeWorkbookHolder().setWorkbookWriteHandlerContext(context); + return context; } - public static void beforeWorkbookCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { - ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(context); - } + public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context) { + beforeWorkbookCreate(context, false); + } + + public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.beforeWorkbookCreate(context); } } - public static void afterWorkbookCreate(WriteContext writeContext) { - afterWorkbookCreate(writeContext, false); + public static void afterWorkbookCreate(WorkbookWriteHandlerContext context) { + afterWorkbookCreate(context, false); } - public static void afterWorkbookCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { - ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(context); - } + public static void afterWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.afterWorkbookCreate(context); } } - public static void afterWorkbookDispose(WriteContext writeContext) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(WorkbookWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { - ((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(context); - } + private static WorkbookHandlerExecutionChain getWorkbookHandlerExecutionChain(WorkbookWriteHandlerContext context, + boolean runOwn) { + AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder(); + if (runOwn) { + return abstractWriteHolder.getOwnWorkbookHandlerExecutionChain(); + } else { + return abstractWriteHolder.getWorkbookHandlerExecutionChain(); } } - public static void beforeSheetCreate(WriteContext writeContext) { - beforeSheetCreate(writeContext, false); + public static void afterWorkbookDispose(WorkbookWriteHandlerContext context) { + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, false); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.afterWorkbookDispose(context); + } } - public static void beforeSheetCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof SheetWriteHandler) { - ((SheetWriteHandler)writeHandler).beforeSheetCreate(context); - } - } + public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext) { + return new SheetWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder()); } - public static void afterSheetCreate(WriteContext writeContext) { - afterSheetCreate(writeContext, false); + public static void beforeSheetCreate(SheetWriteHandlerContext context) { + beforeSheetCreate(context, false); } - public static void afterSheetCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof SheetWriteHandler) { - ((SheetWriteHandler)writeHandler).afterSheetCreate(context); - } + public static void beforeSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { + SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn); + if (sheetHandlerExecutionChain != null) { + sheetHandlerExecutionChain.beforeSheetCreate(context); } } - public static void beforeCellCreate(WriteContext writeContext, Row row, Head head, Integer columnIndex, - Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, null, columnIndex, relativeRowIndex, - head, null, null, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).beforeCellCreate(context); - } - } + public static void afterSheetCreate(SheetWriteHandlerContext context) { + afterSheetCreate(context, false); } - public static void afterCellCreate(WriteContext writeContext, Cell cell, Head head, Integer relativeRowIndex, - Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, null, null, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).afterCellCreate(context); - } + public static void afterSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { + SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn); + if (sheetHandlerExecutionChain != null) { + sheetHandlerExecutionChain.afterSheetCreate(context); } } - public static void afterCellDataConverted(WriteContext writeContext, WriteCellData> cellData, Cell cell, - Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).afterCellDataConverted(context); - } + private static SheetHandlerExecutionChain getSheetHandlerExecutionChain(SheetWriteHandlerContext context, + boolean runOwn) { + AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder(); + if (runOwn) { + return abstractWriteHolder.getOwnSheetHandlerExecutionChain(); + } else { + return abstractWriteHolder.getSheetHandlerExecutionChain(); } } - public static void afterCellDispose(WriteContext writeContext, WriteCellData> cellData, Cell cell, Head head, - Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - afterCellDispose(writeContext, cellDataList, cell, head, relativeRowIndex, isHead, excelContentProperty); + public static CellWriteHandlerContext createCellWriteHandlerContext(WriteContext writeContext, Row row, + Integer rowIndex, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, + ExcelContentProperty excelContentProperty) { + return new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, rowIndex, null, columnIndex, + relativeRowIndex, head, null, null, isHead, excelContentProperty); } - public static void afterCellDispose(WriteContext writeContext, List> cellDataList, Cell cell, - Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; + public static void beforeCellCreate(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.beforeCellCreate(context); } - WriteCellData> cellData = null; - if (CollectionUtils.isNotEmpty(cellDataList)) { - cellData = cellDataList.get(0); + } + + public static void afterCellCreate(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellCreate(context); } - 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) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).afterCellDispose(context); - } + } + + public static void afterCellDataConverted(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellDataConverted(context); } } - public static void beforeRowCreate(WriteContext writeContext, Integer rowIndex, Integer relativeRowIndex, - Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; + public static void afterCellDispose(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellDispose(context); } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + } + + public static RowWriteHandlerContext createRowWriteHandlerContext(WriteContext writeContext, Integer rowIndex, + Integer relativeRowIndex, Boolean isHead) { + return new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { - ((RowWriteHandler)writeHandler).beforeRowCreate(context); - } - } } - public static void afterRowCreate(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, - isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { - ((RowWriteHandler)writeHandler).afterRowCreate(context); - } + public static void beforeRowCreate(RowWriteHandlerContext context) { + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.beforeRowCreate(context); } } - public static void afterRowDispose(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, - isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { - ((RowWriteHandler)writeHandler).afterRowDispose(context); - } + public static void afterRowCreate(RowWriteHandlerContext context) { + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.afterRowCreate(context); } } - private static List getHandlerList(WriteContext writeContext, Class extends WriteHandler> clazz, - boolean runOwn) { - Map, List> writeHandlerMap; - if (runOwn) { - writeHandlerMap = writeContext.currentWriteHolder().ownWriteHandlerMap(); - } else { - writeHandlerMap = writeContext.currentWriteHolder().writeHandlerMap(); + public static void afterRowDispose(RowWriteHandlerContext context) { + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.afterRowDispose(context); } - return writeHandlerMap.get(clazz); } + } diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java index 597d94de..9d10ca1e 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -8,8 +8,7 @@ import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.exception.ExcelDataConvertException; -import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.exception.ExcelWriteDataConvertException; import com.alibaba.excel.metadata.data.CommentData; import com.alibaba.excel.metadata.data.FormulaData; import com.alibaba.excel.metadata.data.HyperlinkData; @@ -19,10 +18,11 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.FileTypeUtils; +import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.StyleUtil; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; -import com.alibaba.excel.write.metadata.holder.WriteHolder; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; @@ -33,6 +33,7 @@ import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; /** @@ -47,124 +48,140 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { this.writeContext = writeContext; } - protected WriteCellData> converterAndSet(WriteHolder currentWriteHolder, Class> clazz, - CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, - Integer relativeRowIndex, int rowIndex, int columnIndex) { - boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() - .getAutoTrim()); - if (needTrim) { - value = ((String)value).trim(); - } - WriteCellData> cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); - WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + /** + * Transform the data and then to set into the cell + * + * @param cellWriteHandlerContext context + * @return + */ + protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) { + + WriteCellData> cellData = convert(cellWriteHandlerContext); + cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(cellData)); + cellWriteHandlerContext.setFirstCellData(cellData); + + WriteHandlerUtils.afterCellDataConverted(cellWriteHandlerContext); // Fill in picture information - fillImage(cell, cellData.getImageDataList()); + fillImage(cellWriteHandlerContext, cellData.getImageDataList()); // Fill in comment information - fillComment(cell, cellData.getCommentData()); + fillComment(cellWriteHandlerContext, cellData.getCommentData()); // Fill in hyper link information - fillHyperLink(cell, cellData.getHyperlinkData()); + fillHyperLink(cellWriteHandlerContext, cellData.getHyperlinkData()); // Fill in formula information - fillFormula(cell, cellData.getFormulaData()); + fillFormula(cellWriteHandlerContext, cellData.getFormulaData()); // Fill index - cellData.setRowIndex(rowIndex); - cellData.setColumnIndex(columnIndex); + cellData.setRowIndex(cellWriteHandlerContext.getRowIndex()); + cellData.setColumnIndex(cellWriteHandlerContext.getColumnIndex()); if (cellData.getType() == null) { cellData.setType(CellDataTypeEnum.EMPTY); } + Cell cell = cellWriteHandlerContext.getCell(); switch (cellData.getType()) { case STRING: cell.setCellValue(cellData.getStringValue()); - return cellData; + return; case BOOLEAN: cell.setCellValue(cellData.getBooleanValue()); - return cellData; + return; case NUMBER: cell.setCellValue(cellData.getNumberValue().doubleValue()); - return cellData; + return; case DATE: cell.setCellValue(cellData.getDateValue()); - return cellData; + return; case RICH_TEXT_STRING: cell.setCellValue(StyleUtil .buildRichTextString(writeContext.writeWorkbookHolder(), cellData.getRichTextStringDataValue())); - return cellData; + return; case EMPTY: - return cellData; + return; default: - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData, - excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType() - + "at row:" + cell.getRow().getRowNum()); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Not supported data:" + cellWriteHandlerContext.getOriginalValue() + " return type:" + + cellData.getType() + + "at row:" + cellWriteHandlerContext.getRowIndex()); } } - private void fillFormula(Cell cell, FormulaData formulaData) { + private void fillFormula(CellWriteHandlerContext cellWriteHandlerContext, FormulaData formulaData) { if (formulaData == null) { return; } + Cell cell = cellWriteHandlerContext.getCell(); if (formulaData.getFormulaValue() != null) { cell.setCellFormula(formulaData.getFormulaValue()); } } - private void fillHyperLink(Cell cell, HyperlinkData hyperlinkData) { + private void fillHyperLink(CellWriteHandlerContext cellWriteHandlerContext, HyperlinkData hyperlinkData) { if (hyperlinkData == null) { return; } - CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper(); + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); + Cell cell = cellWriteHandlerContext.getCell(); + + CreationHelper helper = workbook.getCreationHelper(); Hyperlink hyperlink = helper.createHyperlink(StyleUtil.getHyperlinkType(hyperlinkData.getHyperlinkType())); hyperlink.setAddress(hyperlinkData.getAddress()); - hyperlink.setFirstRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getFirstRowIndex(), + hyperlink.setFirstRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getFirstRowIndex(), hyperlinkData.getRelativeFirstRowIndex())); - hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getFirstColumnIndex(), + hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getFirstColumnIndex(), hyperlinkData.getRelativeFirstColumnIndex())); - hyperlink.setLastRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getLastRowIndex(), + hyperlink.setLastRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getLastRowIndex(), hyperlinkData.getRelativeLastRowIndex())); - hyperlink.setLastColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getLastColumnIndex(), + hyperlink.setLastColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getLastColumnIndex(), hyperlinkData.getRelativeLastColumnIndex())); cell.setHyperlink(hyperlink); } - private void fillComment(Cell cell, CommentData commentData) { + private void fillComment(CellWriteHandlerContext cellWriteHandlerContext, CommentData commentData) { if (commentData == null) { return; } ClientAnchor anchor; + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); + Cell cell = cellWriteHandlerContext.getCell(); + if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.XLSX) { anchor = new XSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getBottom()), - StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), + StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), commentData.getRelativeFirstColumnIndex()), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), commentData.getRelativeFirstRowIndex()), - StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), + StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), commentData.getRelativeLastColumnIndex()) + 1, - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), commentData.getRelativeLastRowIndex()) + 1); } else { anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getBottom()), - (short)StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), + (short)StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), commentData.getRelativeFirstColumnIndex()), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), commentData.getRelativeFirstRowIndex()), - (short)(StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), + (short)(StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), commentData.getRelativeLastColumnIndex()) + 1), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), commentData.getRelativeLastRowIndex()) + 1); } - Comment comment = cell.getSheet().createDrawingPatriarch().createCellComment(anchor); + + Comment comment = sheet.createDrawingPatriarch().createCellComment(anchor); if (commentData.getRichTextStringData() != null) { comment.setString( StyleUtil.buildRichTextString(writeContext.writeWorkbookHolder(), commentData.getRichTextStringData())); @@ -175,18 +192,22 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { cell.setCellComment(comment); } - protected void fillImage(Cell cell, List imageDataList) { + protected void fillImage(CellWriteHandlerContext cellWriteHandlerContext, List imageDataList) { if (CollectionUtils.isEmpty(imageDataList)) { return; } - Sheet sheet = cell.getSheet(); + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); + Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); + Drawing> drawing = sheet.getDrawingPatriarch(); if (drawing == null) { drawing = sheet.createDrawingPatriarch(); } CreationHelper helper = sheet.getWorkbook().getCreationHelper(); for (ImageData imageData : imageDataList) { - int index = sheet.getWorkbook().addPicture(imageData.getImage(), + int index = workbook.addPicture(imageData.getImage(), FileTypeUtils.getImageTypeFormat(imageData.getImage())); ClientAnchor anchor = helper.createClientAnchor(); if (imageData.getTop() != null) { @@ -201,13 +222,13 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { if (imageData.getLeft() != null) { anchor.setDx1(StyleUtil.getCoordinate(imageData.getLeft())); } - anchor.setRow1(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getFirstRowIndex(), + anchor.setRow1(StyleUtil.getCellCoordinate(rowIndex, imageData.getFirstRowIndex(), imageData.getRelativeFirstRowIndex())); - anchor.setCol1(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getFirstColumnIndex(), + anchor.setCol1(StyleUtil.getCellCoordinate(columnIndex, imageData.getFirstColumnIndex(), imageData.getRelativeFirstColumnIndex())); - anchor.setRow2(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getLastRowIndex(), + anchor.setRow2(StyleUtil.getCellCoordinate(rowIndex, imageData.getLastRowIndex(), imageData.getRelativeLastRowIndex()) + 1); - anchor.setCol2(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getLastColumnIndex(), + anchor.setCol2(StyleUtil.getCellCoordinate(columnIndex, imageData.getLastColumnIndex(), imageData.getRelativeLastColumnIndex()) + 1); if (imageData.getAnchorType() != null) { anchor.setAnchorType(imageData.getAnchorType().getValue()); @@ -216,17 +237,16 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } } - protected WriteCellData> convert(WriteHolder currentWriteHolder, Class> clazz, CellDataTypeEnum targetType, - Cell cell, Object value, ExcelContentProperty excelContentProperty) { + protected WriteCellData> convert(CellWriteHandlerContext cellWriteHandlerContext) { // This means that the user has defined the data. - if (clazz == WriteCellData.class) { - if (value == null) { + if (cellWriteHandlerContext.getOriginalFieldClass() == WriteCellData.class) { + if (cellWriteHandlerContext.getOriginalValue() == null) { return new WriteCellData<>(CellDataTypeEnum.EMPTY); } - WriteCellData> cellDataValue = (WriteCellData>)value; + WriteCellData> cellDataValue = (WriteCellData>)cellWriteHandlerContext.getOriginalValue(); if (cellDataValue.getType() != null) { // Configuration information may not be read here - fillProperty(cellDataValue, excelContentProperty); + fillProperty(cellDataValue, cellWriteHandlerContext.getExcelContentProperty()); return cellDataValue; } else { @@ -235,8 +255,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { return cellDataValue; } } - WriteCellData> cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), - targetType, cell, cellDataValue.getData(), excelContentProperty); + WriteCellData> cellDataReturn = doConvert(cellWriteHandlerContext); if (cellDataValue.getImageDataList() != null) { cellDataReturn.setImageDataList(cellDataValue.getImageDataList()); @@ -256,7 +275,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } return cellDataReturn; } - return doConvert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); + return doConvert(cellWriteHandlerContext); } private void fillProperty(WriteCellData> cellDataValue, ExcelContentProperty excelContentProperty) { @@ -280,8 +299,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } } - private WriteCellData> doConvert(WriteHolder currentWriteHolder, Class> clazz, CellDataTypeEnum targetType, - Cell cell, Object value, ExcelContentProperty excelContentProperty) { + private WriteCellData> doConvert(CellWriteHandlerContext cellWriteHandlerContext) { + ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty(); + Converter> converter = null; if (excelContentProperty != null) { converter = excelContentProperty.getConverter(); @@ -289,31 +309,34 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { if (converter == null) { // csv is converted to string by default if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) { - targetType = CellDataTypeEnum.STRING; + cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING); } - converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType)); + converter = writeContext.currentWriteHolder().converterMap().get( + ConverterKeyBuild.buildKey(cellWriteHandlerContext.getOriginalFieldClass(), + cellWriteHandlerContext.getTargetCellDataType())); } - if (value == null && !(converter instanceof NullableObjectConverter)) { + if (cellWriteHandlerContext.getOriginalValue() == null && !(converter instanceof NullableObjectConverter)) { return new WriteCellData<>(CellDataTypeEnum.EMPTY); } if (converter == null) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Can not find 'Converter' support class " + clazz.getSimpleName() + "."); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Can not find 'Converter' support class " + cellWriteHandlerContext.getOriginalFieldClass() + .getSimpleName() + "."); } WriteCellData> cellData; try { cellData = ((Converter)converter).convertToExcelData( - new WriteConverterContext<>(value, excelContentProperty, writeContext)); + new WriteConverterContext<>(cellWriteHandlerContext.getOriginalValue(), excelContentProperty, + writeContext)); } catch (Exception e) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Convert data:" + value + " error, at row:" + cell.getRow().getRowNum(), e); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " error, at row:" + + cellWriteHandlerContext.getRowIndex(), e); } if (cellData == null || cellData.getType() == null) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Convert data:" + value + " return null, at row:" + cell.getRow().getRowNum()); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return null, at row:" + + cellWriteHandlerContext.getRowIndex()); } return cellData; } diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java index bdfea49e..0bb4007c 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -11,13 +11,14 @@ import java.util.TreeMap; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.alibaba.excel.write.metadata.CollectionRowData; import com.alibaba.excel.write.metadata.MapRowData; import com.alibaba.excel.write.metadata.RowData; @@ -64,9 +65,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { if (oneRowData == null) { return; } - WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE); + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, + rowIndex, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex); - WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE); + rowWriteHandlerContext.setRow(row); + + WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext); if (oneRowData instanceof Collection>) { addBasicTypeToExcel(new CollectionRowData((Collection>)oneRowData), row, rowIndex, relativeRowIndex); @@ -75,7 +81,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } else { addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap); } - WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE); + + WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext); } private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) { @@ -114,23 +121,30 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), head == null ? null : head.getFieldName()); - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, + row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, - excelContentProperty); - Object value = oneRowData.get(dataIndex); - WriteCellData> cellData = converterAndSet(writeContext.currentWriteHolder(), - FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex, rowIndex, columnIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(oneRowData.get(dataIndex)); + cellWriteHandlerContext.setOriginalFieldClass( + FieldUtils.getFieldClass(cellWriteHandlerContext.getOriginalValue())); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, Map sortedAllFiledMap) { WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); BeanMap beanMap = BeanMapUtils.create(oneRowData); - Set beanMapHandledSet = new HashSet(); + // Bean the contains of the Map Key method with poor performance,So to create a keySet here + Set beanKeySet = new HashSet<>(beanMap.keySet()); + Set beanMapHandledSet = new HashSet<>(); int maxCellIndex = -1; // If it's a class it needs to be cast by type if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) { @@ -139,21 +153,28 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { int columnIndex = entry.getKey(); Head head = entry.getValue(); String name = head.getFieldName(); - if (!beanMap.containsKey(name)) { + if (!beanKeySet.contains(name)) { continue; } + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, - Boolean.FALSE, excelContentProperty); - Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, 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, + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + + Cell cell = WorkBookUtil.createCell(row, columnIndex); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(beanMap.get(name)); + cellWriteHandlerContext.setOriginalFieldClass(head.getField().getType()); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); + beanMapHandledSet.add(name); maxCellIndex = Math.max(maxCellIndex, columnIndex); } @@ -169,7 +190,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { for (Map.Entry entry : sortedAllFiledMap.entrySet()) { Field field = entry.getValue(); String filedName = FieldUtils.resolveCglibFieldName(field); - boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName) + boolean uselessData = !beanKeySet.contains(filedName) || beanMapHandledSet.contains(filedName) || ignoreMap.containsKey(filedName); if (uselessData) { continue; @@ -177,18 +198,23 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { Object value = beanMap.get(filedName); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), filedName); - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + // fix https://github.com/alibaba/easyexcel/issues/1870 // If there is data, it is written to the next cell - Cell cell = WorkBookUtil.createCell(row, maxCellIndex++); - WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE, - excelContentProperty); - WriteCellData> cellData = converterAndSet(currentWriteHolder, - FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex, - rowIndex, maxCellIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + Cell cell = WorkBookUtil.createCell(row, maxCellIndex); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, filedName, value)); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); + maxCellIndex++; } } diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java index 2fb755a7..af90d8d5 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -7,6 +7,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -24,11 +25,17 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.alibaba.excel.write.metadata.fill.AnalysisCell; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillWrapper; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.hssf.usermodel.PoiUtils; import org.apache.poi.ss.usermodel.Cell; @@ -53,29 +60,30 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { /** * Fields to replace in the template */ - private final Map> templateAnalysisCache = MapUtils.newHashMap(); + private final Map> templateAnalysisCache = MapUtils.newHashMap(); /** * Collection fields to replace in the template */ - private final Map> templateCollectionAnalysisCache = MapUtils.newHashMap(); + private final Map> templateCollectionAnalysisCache = MapUtils.newHashMap(); /** * Style cache for collection fields */ - private final Map> collectionFieldStyleCache = MapUtils.newHashMap(); + private final Map> collectionFieldStyleCache + = MapUtils.newHashMap(); /** * Row height cache for collection */ - private final Map collectionRowHeightCache = MapUtils.newHashMap(); + private final Map collectionRowHeightCache = MapUtils.newHashMap(); /** * Last index cache for collection fields */ - private final Map> collectionLastIndexCache = MapUtils.newHashMap(); + private final Map> collectionLastIndexCache = MapUtils.newHashMap(); - private final Map relativeRowIndexMap = MapUtils.newHashMap(); + private final Map relativeRowIndexMap = MapUtils.newHashMap(); /** * The unique data encoding for this fill */ - private String currentUniqueDataFlag; + private UniqueDataFlagKey currentUniqueDataFlag; public ExcelWriteFillExecutor(WriteContext writeContext) { super(writeContext); @@ -159,15 +167,16 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { sheet.shiftRows(maxRowIndex + 1, lastRowIndex, number, true, false); // The current data is greater than unity rowindex increase - String tablePrefix = tablePrefix(currentUniqueDataFlag); - increaseRowIndex(templateAnalysisCache, number, maxRowIndex, tablePrefix); - increaseRowIndex(templateCollectionAnalysisCache, number, maxRowIndex, tablePrefix); + increaseRowIndex(templateAnalysisCache, number, maxRowIndex); + increaseRowIndex(templateCollectionAnalysisCache, number, maxRowIndex); } - private void increaseRowIndex(Map> templateAnalysisCache, int number, int maxRowIndex, - String tablePrefix) { - for (Map.Entry> entry : templateAnalysisCache.entrySet()) { - if (!tablePrefix.equals(tablePrefix(entry.getKey()))) { + private void increaseRowIndex(Map> templateAnalysisCache, int number, + int maxRowIndex) { + for (Map.Entry> entry : templateAnalysisCache.entrySet()) { + UniqueDataFlagKey uniqueDataFlagKey = entry.getKey(); + if (!Objects.equals(currentUniqueDataFlag.getSheetNo(), uniqueDataFlagKey.getSheetNo()) || !Objects.equals( + currentUniqueDataFlag.getSheetName(), uniqueDataFlagKey.getSheetName())) { continue; } for (AnalysisCell analysisCell : entry.getValue()) { @@ -183,28 +192,35 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) { return; } - Map, ?> dataMap; + Map dataMap; if (oneRowData instanceof Map) { - dataMap = (Map, ?>)oneRowData; + dataMap = (Map)oneRowData; } else { dataMap = BeanMapUtils.create(oneRowData); } - WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder(); + Set dataKeySet = new HashSet<>(dataMap.keySet()); + for (AnalysisCell analysisCell : analysisCellList) { + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(), + relativeRowIndex, Boolean.FALSE, ExcelContentProperty.EMPTY); + if (analysisCell.getOnlyOneVariable()) { String variable = analysisCell.getVariableList().get(0); - if (!dataMap.containsKey(variable)) { + if (!dataKeySet.contains(variable)) { continue; } Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); - Cell cell = getOneCell(analysisCell, fillConfig, excelContentProperty); + cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); + + createCell(analysisCell, fillConfig, cellWriteHandlerContext); + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value)); - WriteCellData> cellData = converterAndSet(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null, - relativeRowIndex, analysisCell.getRowIndex(), analysisCell.getColumnIndex()); - cellData.setAnalysisCell(analysisCell); + converterAndSet(cellWriteHandlerContext); + WriteCellData> cellData = cellWriteHandlerContext.getFirstCellData(); // Restyle if (fillConfig.getAutoStyle()) { @@ -212,28 +228,33 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .ifPresent(cellData::setOriginCellStyle); } - - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, - excelContentProperty); } else { StringBuilder cellValueBuild = new StringBuilder(); int index = 0; List> cellDataList = new ArrayList<>(); - Cell cell = getOneCell(analysisCell, fillConfig, ExcelContentProperty.EMPTY); + + cellWriteHandlerContext.setExcelContentProperty(ExcelContentProperty.EMPTY); + cellWriteHandlerContext.setIgnoreFillStyle(Boolean.TRUE); + + createCell(analysisCell, fillConfig, cellWriteHandlerContext); + Cell cell = cellWriteHandlerContext.getCell(); for (String variable : analysisCell.getVariableList()) { cellValueBuild.append(analysisCell.getPrepareDataList().get(index++)); - if (!dataMap.containsKey(variable)) { + if (!dataKeySet.contains(variable)) { continue; } Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); - WriteCellData> cellData = convert(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable, value), CellDataTypeEnum.STRING, cell, value, - excelContentProperty); - cellData.setAnalysisCell(analysisCell); + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value)); + cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); + cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING); + + WriteCellData> cellData = convert(cellWriteHandlerContext); cellDataList.add(cellData); + CellDataTypeEnum type = cellData.getType(); if (type != null) { switch (type) { @@ -253,6 +274,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } cellValueBuild.append(analysisCell.getPrepareDataList().get(index)); cell.setCellValue(cellValueBuild.toString()); + cellWriteHandlerContext.setCellDataList(cellDataList); + if (CollectionUtils.isNotEmpty(cellDataList)) { + cellWriteHandlerContext.setFirstCellData(cellDataList.get(0)); + } // Restyle if (fillConfig.getAutoStyle()) { @@ -260,10 +285,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .ifPresent(cell::setCellStyle); } - - WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, relativeRowIndex, - Boolean.FALSE, ExcelContentProperty.EMPTY); } + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } } @@ -278,11 +301,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return relativeRowIndex; } - private Cell getOneCell(AnalysisCell analysisCell, FillConfig fillConfig, - ExcelContentProperty excelContentProperty) { + private void createCell(AnalysisCell analysisCell, FillConfig fillConfig, + CellWriteHandlerContext cellWriteHandlerContext) { Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet(); if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) { - return cachedSheet.getRow(analysisCell.getRowIndex()).getCell(analysisCell.getColumnIndex()); + Row row = cachedSheet.getRow(analysisCell.getRowIndex()); + cellWriteHandlerContext.setRow(row); + Cell cell = row.getCell(analysisCell.getColumnIndex()); + cellWriteHandlerContext.setCell(cell); } Sheet sheet = writeContext.writeSheetHolder().getSheet(); @@ -320,25 +346,31 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell); - Cell cell = createCellIfNecessary(row, lastColumnIndex, excelContentProperty); + cellWriteHandlerContext.setRow(row); + + cellWriteHandlerContext.setRowIndex(lastRowIndex); + cellWriteHandlerContext.setColumnIndex(lastColumnIndex); + Cell cell = createCellIfNecessary(row, lastColumnIndex, cellWriteHandlerContext); + cellWriteHandlerContext.setCell(cell); if (isOriginalCell) { Map collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent( currentUniqueDataFlag, key -> MapUtils.newHashMap()); collectionFieldStyleMap.put(analysisCell, cell.getCellStyle()); } - return cell; } - private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, ExcelContentProperty excelContentProperty) { + private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, + CellWriteHandlerContext cellWriteHandlerContext) { Cell cell = row.getCell(lastColumnIndex); if (cell != null) { return cell; } - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, lastColumnIndex, null, Boolean.FALSE, - excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); cell = row.createCell(lastColumnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, null, null, Boolean.FALSE, excelContentProperty); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); return cell; } @@ -351,7 +383,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } row = cachedSheet.getRow(lastRowIndex); if (row == null) { - WriteHandlerUtils.beforeRowCreate(writeContext, lastRowIndex, null, Boolean.FALSE); + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, + lastRowIndex, null, Boolean.FALSE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + if (fillConfig.getForceNewRow()) { row = cachedSheet.createRow(lastRowIndex); } else { @@ -364,8 +399,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { row = cachedSheet.createRow(lastRowIndex); } } + rowWriteHandlerContext.setRow(row); checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); - WriteHandlerUtils.afterRowCreate(writeContext, row, null, Boolean.FALSE); + + WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext); } else { checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); } @@ -389,13 +426,13 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } } - private List readTemplateData(Map> analysisCache) { + private List readTemplateData(Map> analysisCache) { List analysisCellList = analysisCache.get(currentUniqueDataFlag); if (analysisCellList != null) { return analysisCellList; } Sheet sheet = writeContext.writeSheetHolder().getCachedSheet(); - Map> firstRowCache = MapUtils.newHashMapWithExpectedSize(8); + Map> firstRowCache = MapUtils.newHashMapWithExpectedSize(8); for (int i = 0; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) { @@ -425,7 +462,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { * @param firstRowCache first row cache * @return Returns the data that the cell needs to replace */ - private String prepareData(Cell cell, int rowIndex, int columnIndex, Map> firstRowCache) { + private String prepareData(Cell cell, int rowIndex, int columnIndex, + Map> firstRowCache) { if (!CellType.STRING.equals(cell.getCellType())) { return null; } @@ -508,7 +546,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } private String dealAnalysisCell(AnalysisCell analysisCell, String value, int rowIndex, int lastPrepareDataIndex, - int length, Map> firstRowCache, StringBuilder preparedData) { + int length, Map> firstRowCache, StringBuilder preparedData) { if (analysisCell != null) { if (lastPrepareDataIndex == length) { analysisCell.getPrepareDataList().add(StringUtils.EMPTY); @@ -516,7 +554,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { analysisCell.getPrepareDataList().add(convertPrepareData(value.substring(lastPrepareDataIndex))); analysisCell.setOnlyOneVariable(Boolean.FALSE); } - String uniqueDataFlag = uniqueDataFlag(writeContext.writeSheetHolder(), analysisCell.getPrefix()); + UniqueDataFlagKey uniqueDataFlag = uniqueDataFlag(writeContext.writeSheetHolder(), + analysisCell.getPrefix()); if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) { List analysisCellList = templateAnalysisCache.computeIfAbsent(uniqueDataFlag, key -> ListUtils.newArrayList()); @@ -560,21 +599,17 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return prepareData; } - private String uniqueDataFlag(WriteSheetHolder writeSheetHolder, String wrapperName) { - String prefix; - if (writeSheetHolder.getSheetNo() != null) { - prefix = writeSheetHolder.getSheetNo().toString(); - } else { - prefix = writeSheetHolder.getSheetName(); - } - if (StringUtils.isEmpty(wrapperName)) { - return prefix + "-"; - } - return prefix + "-" + wrapperName; + private UniqueDataFlagKey uniqueDataFlag(WriteSheetHolder writeSheetHolder, String wrapperName) { + return new UniqueDataFlagKey(writeSheetHolder.getSheetNo(), writeSheetHolder.getSheetName(), wrapperName); } - private String tablePrefix(String uniqueDataFlag) { - return uniqueDataFlag.substring(0, uniqueDataFlag.indexOf("-") + 1); + @Getter + @Setter + @EqualsAndHashCode + @AllArgsConstructor + public static class UniqueDataFlagKey { + private Integer sheetNo; + private String sheetName; + private String wrapperName; } - } diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java new file mode 100644 index 00000000..35afe6fc --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java @@ -0,0 +1,67 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the cell handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class CellHandlerExecutionChain { + /** + * next chain + */ + private CellHandlerExecutionChain next; + /** + * handler + */ + private CellWriteHandler handler; + + public CellHandlerExecutionChain(CellWriteHandler handler) { + this.handler = handler; + } + + public void beforeCellCreate(CellWriteHandlerContext context) { + this.handler.beforeCellCreate(context); + if (this.next != null) { + this.next.beforeCellCreate(context); + } + } + + public void afterCellCreate(CellWriteHandlerContext context) { + this.handler.afterCellCreate(context); + if (this.next != null) { + this.next.afterCellCreate(context); + } + } + + public void afterCellDataConverted(CellWriteHandlerContext context) { + this.handler.afterCellDataConverted(context); + if (this.next != null) { + this.next.afterCellDataConverted(context); + } + } + + public void afterCellDispose(CellWriteHandlerContext context) { + this.handler.afterCellDispose(context); + if (this.next != null) { + this.next.afterCellDispose(context); + } + } + + public void addLast(CellWriteHandler handler) { + CellHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new CellHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java new file mode 100644 index 00000000..4f32f3b1 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java @@ -0,0 +1,60 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.RowWriteHandler; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the row handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class RowHandlerExecutionChain { + /** + * next chain + */ + private RowHandlerExecutionChain next; + /** + * handler + */ + private RowWriteHandler handler; + + public RowHandlerExecutionChain(RowWriteHandler handler) { + this.handler = handler; + } + + public void beforeRowCreate(RowWriteHandlerContext context) { + this.handler.beforeRowCreate(context); + if (this.next != null) { + this.next.beforeRowCreate(context); + } + } + + public void afterRowCreate(RowWriteHandlerContext context) { + this.handler.afterRowCreate(context); + if (this.next != null) { + this.next.afterRowCreate(context); + } + } + + public void afterRowDispose(RowWriteHandlerContext context) { + this.handler.afterRowDispose(context); + if (this.next != null) { + this.next.afterRowDispose(context); + } + } + + public void addLast(RowWriteHandler handler) { + RowHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new RowHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java new file mode 100644 index 00000000..7e079dd2 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java @@ -0,0 +1,52 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.SheetWriteHandler; +import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the sheet handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class SheetHandlerExecutionChain { + /** + * next chain + */ + private SheetHandlerExecutionChain next; + /** + * handler + */ + private SheetWriteHandler handler; + + public SheetHandlerExecutionChain(SheetWriteHandler handler) { + this.handler = handler; + } + + public void beforeSheetCreate(SheetWriteHandlerContext context) { + this.handler.beforeSheetCreate(context); + if (this.next != null) { + this.next.beforeSheetCreate(context); + } + } + + public void afterSheetCreate(SheetWriteHandlerContext context) { + this.handler.afterSheetCreate(context); + if (this.next != null) { + this.next.afterSheetCreate(context); + } + } + public void addLast(SheetWriteHandler handler) { + SheetHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new SheetHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java new file mode 100644 index 00000000..56e746e0 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java @@ -0,0 +1,61 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.WorkbookWriteHandler; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the workbook handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class WorkbookHandlerExecutionChain { + /** + * next chain + */ + private WorkbookHandlerExecutionChain next; + + /** + * handler + */ + private WorkbookWriteHandler handler; + + public WorkbookHandlerExecutionChain(WorkbookWriteHandler handler) { + this.handler = handler; + } + + public void beforeWorkbookCreate(WorkbookWriteHandlerContext context) { + this.handler.beforeWorkbookCreate(context); + if (this.next != null) { + this.next.beforeWorkbookCreate(context); + } + } + + public void afterWorkbookCreate(WorkbookWriteHandlerContext context) { + this.handler.afterWorkbookCreate(context); + if (this.next != null) { + this.next.afterWorkbookCreate(context); + } + } + + public void afterWorkbookDispose(WorkbookWriteHandlerContext context) { + this.handler.afterWorkbookDispose(context); + if (this.next != null) { + this.next.afterWorkbookDispose(context); + } + } + + public void addLast(WorkbookWriteHandler handler) { + WorkbookHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new WorkbookHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java index 27530f6f..0007e7ac 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java @@ -3,15 +3,18 @@ package com.alibaba.excel.write.handler.context; import java.util.List; import com.alibaba.excel.context.WriteContext; +import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; -import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; @@ -20,8 +23,9 @@ import org.apache.poi.ss.usermodel.Row; * * @author Jiaju Zhuang */ -@Data -@AllArgsConstructor +@Getter +@Setter +@EqualsAndHashCode public class CellWriteHandlerContext { /** * write context @@ -43,6 +47,10 @@ public class CellWriteHandlerContext { * row */ private Row row; + /** + * index + */ + private Integer rowIndex; /** * cell */ @@ -78,4 +86,47 @@ public class CellWriteHandlerContext { * Field annotation configuration information. */ private ExcelContentProperty excelContentProperty; + + /** + * The value of the original + */ + private Object originalValue; + + /** + * The original field type + */ + private Class> originalFieldClass; + + /** + * Target cell data type + */ + private CellDataTypeEnum targetCellDataType; + + /** + * Ignore the filling pattern and the {@code FillStyleCellWriteHandler} will not work. + * + * @see FillStyleCellWriteHandler + */ + private Boolean ignoreFillStyle; + + public CellWriteHandlerContext(WriteContext writeContext, + WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder, + WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Cell cell, Integer columnIndex, + Integer relativeRowIndex, Head headData, List> cellDataList, WriteCellData> firstCellData, + Boolean head, ExcelContentProperty excelContentProperty) { + 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; + } } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java index 6578d3a2..c01c8b5b 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java @@ -6,7 +6,9 @@ import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.Row; /** @@ -14,7 +16,9 @@ import org.apache.poi.ss.usermodel.Row; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor public class RowWriteHandlerContext { /** diff --git a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java index c1164eb0..f3c024f4 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java @@ -5,14 +5,18 @@ import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * sheet context * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor public class SheetWriteHandlerContext { /** diff --git a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java index f955d4bf..a7ec289c 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java @@ -4,14 +4,18 @@ import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * workbook context * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor public class WorkbookWriteHandlerContext { /** diff --git a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java index c8116b4d..d3680e24 100644 --- a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java @@ -1,16 +1,14 @@ package com.alibaba.excel.write.handler.impl; -import java.util.List; - import com.alibaba.excel.constant.OrderConstant; import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.CellStyle; /** @@ -28,12 +26,12 @@ public class FillStyleCellWriteHandler implements CellWriteHandler { @Override public void afterCellDispose(CellWriteHandlerContext context) { - List> cellDataList = context.getCellDataList(); - if (CollectionUtils.size(cellDataList) != 1) { + if (BooleanUtils.isTrue(context.getIgnoreFillStyle())) { return; } + WriteCellData> cellData = context.getFirstCellData(); - if (cellData.getAnalysisCell() != null && !cellData.getAnalysisCell().getOnlyOneVariable()) { + if (cellData == null) { return; } WriteCellStyle writeCellStyle = cellData.getWriteCellStyle(); diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java b/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java index 91527cc1..b0745d38 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java @@ -7,14 +7,18 @@ import java.util.List; import com.alibaba.excel.metadata.BasicParameter; import com.alibaba.excel.write.handler.WriteHandler; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Write basic parameter * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteBasicParameter extends BasicParameter { /** * Writes the head relative to the existing contents of the sheet. Indexes are zero-based. diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java index 04bfb822..1f41376c 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java @@ -1,13 +1,17 @@ package com.alibaba.excel.write.metadata; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Write sheet * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteSheet extends WriteBasicParameter { /** * Starting from 0 diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java b/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java index a935f326..6681e1b8 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java @@ -1,13 +1,17 @@ package com.alibaba.excel.write.metadata; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * table * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteTable extends WriteBasicParameter { /** * Starting from 0 diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java index bcc39211..38b368ec 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java @@ -7,14 +7,18 @@ import java.nio.charset.Charset; import com.alibaba.excel.support.ExcelTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Workbook * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteWorkbook extends WriteBasicParameter { /** * Excel type.The default is xlsx diff --git a/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java b/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java index b9b377ed..783c7d89 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java +++ b/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java @@ -4,14 +4,18 @@ import java.util.List; import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Read the cells of the template while populating the data. * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class AnalysisCell { private int columnIndex; private int rowIndex; diff --git a/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java b/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java index 8fdb240a..ac5581ee 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java +++ b/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java @@ -4,15 +4,19 @@ import com.alibaba.excel.enums.WriteDirectionEnum; import lombok.AllArgsConstructor; import lombok.Builder; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * Fill config * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode @Builder @NoArgsConstructor @AllArgsConstructor diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index 5059894a..0b546703 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -15,7 +15,6 @@ import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.event.NotRepeatExecutor; -import com.alibaba.excel.event.Order; import com.alibaba.excel.metadata.AbstractHolder; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.property.ExcelContentProperty; @@ -28,6 +27,10 @@ import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.WriteHandler; +import com.alibaba.excel.write.handler.chain.CellHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.RowHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.SheetHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.WorkbookHandlerExecutionChain; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; @@ -38,8 +41,10 @@ import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy; import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; /** @@ -47,7 +52,9 @@ import org.apache.commons.collections4.CollectionUtils; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public abstract class AbstractWriteHolder extends AbstractHolder implements WriteHolder { /** @@ -62,15 +69,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ * Excel head property */ private ExcelWriteHeadProperty excelWriteHeadProperty; - /** - * Write handler - */ - private Map, List> writeHandlerMap; - /** - * Own write handler.Created in the sheet in the workbook interceptors will not be executed because the workbook to - * create an event long past. So when initializing sheet, supplementary workbook event. - */ - private Map, List> ownWriteHandlerMap; /** * Use the default style.Default is true. */ @@ -97,6 +95,43 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ */ private Collection includeColumnFieldNames; + /** + * Write handler + */ + private List writeHandlerList; + + /** + * Execute the workbook handler chain + * Created in the sheet in the workbook interceptors will not be executed because the workbook to + * create an event long past. So when initializing sheet, supplementary workbook event. + */ + public WorkbookHandlerExecutionChain ownWorkbookHandlerExecutionChain; + /** + * Execute the sheet handler chain + * Created in the sheet in the workbook interceptors will not be executed because the workbook to + * create an event long past. So when initializing sheet, supplementary workbook event. + */ + public SheetHandlerExecutionChain ownSheetHandlerExecutionChain; + + /** + * Execute the workbook handler chain + */ + public WorkbookHandlerExecutionChain workbookHandlerExecutionChain; + /** + * Execute the sheet handler chain + */ + public SheetHandlerExecutionChain sheetHandlerExecutionChain; + + /** + * Execute the row handler chain + */ + public RowHandlerExecutionChain rowHandlerExecutionChain; + + /** + * Execute the cell handler chain + */ + public CellHandlerExecutionChain cellHandlerExecutionChain; + public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder) { super(writeBasicParameter, parentAbstractWriteHolder); @@ -169,7 +204,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead()); // Set writeHandlerMap - List handlerList = new ArrayList(); + List handlerList = new ArrayList<>(); // Initialization Annotation initAnnotationConfig(handlerList, writeBasicParameter); @@ -178,16 +213,16 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ && !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) { handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList()); } + sortAndClearUpHandler(handlerList, true); - this.ownWriteHandlerMap = sortAndClearUpHandler(handlerList); - - Map, List> parentWriteHandlerMap = null; if (parentAbstractWriteHolder != null) { - parentWriteHandlerMap = parentAbstractWriteHolder.getWriteHandlerMap(); + if (CollectionUtils.isNotEmpty(parentAbstractWriteHolder.getWriteHandlerList())) { + handlerList.addAll(parentAbstractWriteHolder.getWriteHandlerList()); + } } else { handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle)); } - this.writeHandlerMap = sortAndClearUpAllHandler(handlerList, parentWriteHandlerMap); + sortAndClearUpHandler(handlerList, false); // Set converterMap if (parentAbstractWriteHolder == null) { @@ -212,15 +247,11 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } Map headMap = getExcelWriteHeadProperty().getHeadMap(); boolean hasColumnWidth = false; - boolean hasStyle = false; for (Head head : headMap.values()) { if (head.getColumnWidthProperty() != null) { hasColumnWidth = true; } - if (head.getHeadStyleProperty() != null || head.getHeadFontProperty() != null) { - hasStyle = true; - } dealLoopMerge(handlerList, head); } @@ -228,10 +259,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ dealColumnWidth(handlerList); } - //if (hasStyle) { - dealStyle(handlerList); - //} - + dealStyle(handlerList); dealRowHigh(handlerList); dealOnceAbsoluteMerge(handlerList); } @@ -310,38 +338,22 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ handlerList.add(columnWidthStyleStrategy); } - protected Map, List> sortAndClearUpAllHandler( - List handlerList, Map, List> parentHandlerMap) { - // add - if (parentHandlerMap != null) { - List parentWriteHandler = parentHandlerMap.get(WriteHandler.class); - if (!CollectionUtils.isEmpty(parentWriteHandler)) { - handlerList.addAll(parentWriteHandler); - } - } - return sortAndClearUpHandler(handlerList); - } - - protected Map, List> sortAndClearUpHandler( - List handlerList) { + protected void sortAndClearUpHandler(List handlerList, boolean runOwn) { // sort - Map> orderExcelWriteHandlerMap = new TreeMap>(); + Map> orderExcelWriteHandlerMap = new TreeMap<>(); for (WriteHandler handler : handlerList) { - int order = Integer.MIN_VALUE; - if (handler instanceof Order) { - order = ((Order)handler).order(); - } + int order = handler.order(); if (orderExcelWriteHandlerMap.containsKey(order)) { orderExcelWriteHandlerMap.get(order).add(handler); } else { - List tempHandlerList = new ArrayList(); + List tempHandlerList = new ArrayList<>(); tempHandlerList.add(handler); orderExcelWriteHandlerMap.put(order, tempHandlerList); } } // clean up - Set alreadyExistedHandlerSet = new HashSet(); - List cleanUpHandlerList = new ArrayList(); + Set alreadyExistedHandlerSet = new HashSet<>(); + List cleanUpHandlerList = new ArrayList<>(); for (Map.Entry> entry : orderExcelWriteHandlerMap.entrySet()) { for (WriteHandler handler : entry.getValue()) { if (handler instanceof NotRepeatExecutor) { @@ -354,30 +366,70 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ cleanUpHandlerList.add(handler); } } - // classify - Map, List> result = - new HashMap, List>(16); - result.put(WriteHandler.class, new ArrayList()); - result.put(WorkbookWriteHandler.class, new ArrayList()); - result.put(SheetWriteHandler.class, new ArrayList()); - result.put(RowWriteHandler.class, new ArrayList()); - result.put(CellWriteHandler.class, new ArrayList()); + + // build chain + if (!runOwn) { + this.writeHandlerList = new ArrayList<>(); + } for (WriteHandler writeHandler : cleanUpHandlerList) { - if (writeHandler instanceof CellWriteHandler) { - result.get(CellWriteHandler.class).add(writeHandler); + buildChain(writeHandler, runOwn); + } + } + + protected void buildChain(WriteHandler writeHandler, boolean runOwn) { + if (writeHandler instanceof CellWriteHandler) { + if (!runOwn) { + if (cellHandlerExecutionChain == null) { + cellHandlerExecutionChain = new CellHandlerExecutionChain((CellWriteHandler)writeHandler); + } else { + cellHandlerExecutionChain.addLast((CellWriteHandler)writeHandler); + } } - if (writeHandler instanceof RowWriteHandler) { - result.get(RowWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof RowWriteHandler) { + if (!runOwn) { + if (rowHandlerExecutionChain == null) { + rowHandlerExecutionChain = new RowHandlerExecutionChain((RowWriteHandler)writeHandler); + } else { + rowHandlerExecutionChain.addLast((RowWriteHandler)writeHandler); + } } - if (writeHandler instanceof SheetWriteHandler) { - result.get(SheetWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof SheetWriteHandler) { + if (!runOwn) { + if (sheetHandlerExecutionChain == null) { + sheetHandlerExecutionChain = new SheetHandlerExecutionChain((SheetWriteHandler)writeHandler); + } else { + sheetHandlerExecutionChain.addLast((SheetWriteHandler)writeHandler); + } + } else { + if (ownSheetHandlerExecutionChain == null) { + ownSheetHandlerExecutionChain = new SheetHandlerExecutionChain((SheetWriteHandler)writeHandler); + } else { + ownSheetHandlerExecutionChain.addLast((SheetWriteHandler)writeHandler); + } } - if (writeHandler instanceof WorkbookWriteHandler) { - result.get(WorkbookWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof WorkbookWriteHandler) { + if (!runOwn) { + if (workbookHandlerExecutionChain == null) { + workbookHandlerExecutionChain = new WorkbookHandlerExecutionChain( + (WorkbookWriteHandler)writeHandler); + } else { + workbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler); + } + } else { + if (ownWorkbookHandlerExecutionChain == null) { + ownWorkbookHandlerExecutionChain = new WorkbookHandlerExecutionChain( + (WorkbookWriteHandler)writeHandler); + } else { + ownWorkbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler); + } } - result.get(WriteHandler.class).add(writeHandler); } - return result; + if (!runOwn) { + this.writeHandlerList.add(writeHandler); + } } @Override @@ -406,16 +458,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ return getExcelWriteHeadProperty(); } - @Override - public Map, List> writeHandlerMap() { - return getWriteHandlerMap(); - } - - @Override - public Map, List> ownWriteHandlerMap() { - return getOwnWriteHandlerMap(); - } - @Override public boolean needHead() { return getNeedHead(); @@ -430,4 +472,5 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ public boolean automaticMergeHead() { return getAutomaticMergeHead(); } + } diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java index 4896b034..77f3c14f 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java @@ -1,10 +1,6 @@ package com.alibaba.excel.write.metadata.holder; -import java.util.List; -import java.util.Map; - import com.alibaba.excel.metadata.ConfigurationHolder; -import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.property.ExcelWriteHeadProperty; /** @@ -21,20 +17,6 @@ public interface WriteHolder extends ConfigurationHolder { */ ExcelWriteHeadProperty excelWriteHeadProperty(); - /** - * What handler does the currently operated cell need to execute - * - * @return - */ - Map, List> writeHandlerMap(); - - /** - * create your own write handler. - * - * @return - */ - Map, List> ownWriteHandlerMap(); - /** * Is to determine if a field needs to be ignored * diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java index 580eeb97..ffc6f759 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java @@ -8,7 +8,7 @@ import com.alibaba.excel.enums.WriteLastRowTypeEnum; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.write.metadata.WriteSheet; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -22,7 +22,9 @@ import org.apache.poi.xssf.usermodel.XSSFSheet; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class WriteSheetHolder extends AbstractWriteHolder { /** diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java index e353a44c..3a46e784 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java @@ -3,14 +3,18 @@ package com.alibaba.excel.write.metadata.holder; import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.write.metadata.WriteTable; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * sheet holder * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteTableHolder extends AbstractWriteHolder { /*** * poi sheet diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index 374b5b7a..1353ddc4 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -19,11 +19,15 @@ import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.IoUtils; import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StyleUtil; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString.Exclude; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -39,7 +43,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @Slf4j public class WriteWorkbookHolder extends AbstractWriteHolder { /*** @@ -143,6 +149,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { */ private Map dataFormatMap; + /** + * handler context + */ + @Exclude + private WorkbookWriteHandlerContext workbookWriteHandlerContext; + public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { super(writeWorkbook, null); this.writeWorkbook = writeWorkbook; @@ -253,8 +265,6 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { if (writeCellStyle == null) { return originCellStyle; } - WriteCellStyle tempWriteCellStyle = new WriteCellStyle(); - WriteCellStyle.merge(writeCellStyle, tempWriteCellStyle); short styleIndex = -1; Font originFont = null; @@ -271,14 +281,17 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { Map cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex, key -> MapUtils.newHashMap()); - CellStyle cellStyle = cellStyleMap.get(tempWriteCellStyle); + CellStyle cellStyle = cellStyleMap.get(writeCellStyle); if (cellStyle != null) { return cellStyle; } if (log.isDebugEnabled()) { - log.info("create new style:{},{}", tempWriteCellStyle, originCellStyle); + log.info("create new style:{},{}", writeCellStyle, originCellStyle); } - cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle); + WriteCellStyle tempWriteCellStyle = new WriteCellStyle(); + WriteCellStyle.merge(writeCellStyle, tempWriteCellStyle); + + cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, tempWriteCellStyle); Short dataFormat = createDataFormat(tempWriteCellStyle.getDataFormatData(), useCache); if (dataFormat != null) { cellStyle.setDataFormat(dataFormat); diff --git a/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java b/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java index 9298f9da..bebd210e 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java +++ b/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java @@ -6,7 +6,9 @@ import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.HorizontalAlignment; @@ -19,7 +21,9 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteCellStyle { /** * Set the data format (must be a valid format). Built in formats are defined at {@link BuiltinFormats}. diff --git a/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java b/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java index d1f42f01..a42899e1 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java +++ b/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java @@ -2,7 +2,9 @@ package com.alibaba.excel.write.metadata.style; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.ss.usermodel.Font; @@ -13,7 +15,9 @@ import org.apache.poi.ss.usermodel.IndexedColors; * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteFont { /** * The name for the font (i.e. Arial) diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index 7f52cd1d..4c11010a 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentLoopMerge; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadFontStyle; @@ -17,6 +18,7 @@ import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.CellRange; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Holder; +import com.alibaba.excel.metadata.property.ColumnWidthProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty; @@ -24,14 +26,18 @@ import com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty; import com.alibaba.excel.metadata.property.RowHeightProperty; import com.alibaba.excel.metadata.property.StyleProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Define the header attribute of excel * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcelWriteHeadProperty extends ExcelHeadProperty { private RowHeightProperty headRowHeightProperty; @@ -50,9 +56,11 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { this.onceAbsoluteMergeProperty = OnceAbsoluteMergeProperty.build(headClazz.getAnnotation(OnceAbsoluteMerge.class)); + ColumnWidth parentColumnWidth = headClazz.getAnnotation(ColumnWidth.class); HeadStyle parentHeadStyle = headClazz.getAnnotation(HeadStyle.class); HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class); + for (Map.Entry entry : getHeadMap().entrySet()) { Head headData = entry.getValue(); if (headData == null) { @@ -61,6 +69,13 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { } Field field = headData.getField(); + ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class); + if (columnWidth == null) { + columnWidth = parentColumnWidth; + } + headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth)); + + HeadStyle headStyle = field.getAnnotation(HeadStyle.class); if (headStyle == null) { headStyle = parentHeadStyle; diff --git a/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java index 8f033710..df04d248 100644 --- a/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java @@ -7,7 +7,9 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.style.WriteCellStyle; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; /** @@ -15,7 +17,9 @@ import org.apache.commons.collections4.CollectionUtils; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy { private WriteCellStyle headWriteCellStyle; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java index d1c2a4f7..0ed28a29 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java @@ -10,22 +10,28 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data -@ColumnWidth(30) -@HeadRowHeight(15) -@ContentRowHeight(20) +@Getter +@Setter +@EqualsAndHashCode +@ColumnWidth(50) +@HeadRowHeight(50) +@ContentRowHeight(100) public class AnnotationData { @ExcelProperty("日期") @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") private Date date; + @ExcelProperty(value = "数字") @NumberFormat("#.##%") private Double number; + @ExcelIgnore private String ignore; private static final String staticFinal = "test"; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java index 7a4dfe21..02bd3159 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java @@ -8,6 +8,11 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.util.DateUtils; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -46,6 +51,20 @@ public class AnnotationDataTest { EasyExcel.write().file(file).head(AnnotationData.class).sheet().doWrite(data()); EasyExcel.read().file(file).head(AnnotationData.class).registerReadListener(new AnnotationDataListener()) .sheet().doRead(); + + if (file == fileCsv) { + return; + } + + Workbook workbook = WorkbookFactory.create(file); + Sheet sheet = workbook.getSheetAt(0); + Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); + + Row row0 = sheet.getRow(0); + Assert.assertEquals(1000, row0.getHeight(), 0); + + Row row1 = sheet.getRow(1); + Assert.assertEquals(2000, row1.getHeight(), 0); } private List data() throws Exception { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java index b41f093d..fa93f2ed 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.annotation; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class AnnotationIndexAndNameData { @ExcelProperty(value = "第四个", index = 4) private String index4; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java index b8322073..ad50e4a0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java @@ -3,12 +3,16 @@ package com.alibaba.easyexcel.test.core.celldata; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.metadata.data.ReadCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellDataReadData { @DateTimeFormat("yyyy年MM月dd日") private ReadCellData date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java index 69ed7bbf..cf10b0d7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java @@ -5,12 +5,16 @@ import java.util.Date; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellDataWriteData { @DateTimeFormat("yyyy年MM月dd日") private WriteCellData date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java index 8fa78f7e..f9a6bee6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java @@ -8,12 +8,16 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.data.ReadCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterReadData { @ExcelProperty("日期") private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java index 68c34423..6782aa1a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java @@ -8,12 +8,16 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterWriteData { @ExcelProperty("日期") private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java index f8d6ac21..fb31e127 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java @@ -8,12 +8,16 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.converters.string.StringImageConverter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(500) @ColumnWidth(500 / 8) public class ImageData { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java index cccee710..dac73127 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java @@ -5,12 +5,16 @@ import java.math.BigInteger; import java.time.LocalDateTime; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ReadAllConverterData { private BigDecimal bigDecimalBoolean; private BigDecimal bigDecimalNumber; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java b/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java index 1930bce5..5958161c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.dataformat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DateFormatData { private String date; private String dateStringCn; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java index 19ef8d03..6cba3a71 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.encrypt; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class EncryptData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java index 28d273e4..c989ce99 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.exception; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExceptionData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java b/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java index 7c49b01d..737bd61b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.excludeorinclude; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcludeOrIncludeData { @ExcelProperty(order = 1) private String column1; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java b/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java index 61822fa8..0424509e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.extra; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExtraData { private String row1; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java index a1dd56a3..9a33351c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java @@ -4,12 +4,16 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.NumberFormat; import com.alibaba.excel.converters.doubleconverter.DoubleStringConverter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData { private String name; @NumberFormat("#") diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java index 52b1cc04..c568e027 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java @@ -7,12 +7,16 @@ import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.enums.BooleanEnum; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillStyleAnnotatedData { @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 13) @ContentFontStyle(bold = BooleanEnum.TRUE, color = 19) diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java index 98c29fb2..20af16a3 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.fill.style; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillStyleData { private String name; private Double number; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java index ad2188aa..ec81e596 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.handler; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteHandlerData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java index 30e7e814..38eb2b8a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.head; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ComplexHeadData { @ExcelProperty({"顶格", "顶格", "两格"}) private String string0; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java index 7e19af15..a45bcf9c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.head; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class NoHeadData { @ExcelProperty("字符串") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java index 246f9cbd..1bd5a748 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.large; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class LargeData { private String str1; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index 3da8500d..a438a2ee 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -32,6 +32,7 @@ public class LargeDataTest { private static File template07; private static File fileCsv; private static File fileWrite07; + private static File fileWriteTemp07; private static File fileWritePoi07; private int i = 0; @@ -40,13 +41,14 @@ public class LargeDataTest { public static void init() { fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx"); + fileWriteTemp07 = TestFileUtil.createNewFile("large" + File.separator + "fileWriteTemp07.xlsx"); fileWritePoi07 = TestFileUtil.createNewFile("large" + File.separator + "fileWritePoi07.xlsx"); template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx"); fileCsv = TestFileUtil.createNewFile("largefileCsv.csv"); } @Test - public void t01Read() { + public void t01Read() throws Exception { long start = System.currentTimeMillis(); EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class, new LargeDataListener()).headRowNumber(2).sheet().doRead(); @@ -85,9 +87,16 @@ public class LargeDataTest { @Test public void t04Write() throws Exception { - long start = System.currentTimeMillis(); - ExcelWriter excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build(); + ExcelWriter excelWriter = EasyExcel.write(fileWriteTemp07, LargeData.class).build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); + for (int j = 0; j < 2; j++) { + excelWriter.write(data(), writeSheet); + } + excelWriter.finish(); + + long start = System.currentTimeMillis(); + excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build(); + writeSheet = EasyExcel.writerSheet().build(); for (int j = 0; j < 100; j++) { excelWriter.write(data(), writeSheet); LOGGER.info("{} write success.", j); @@ -115,7 +124,8 @@ public class LargeDataTest { } long costPoi = System.currentTimeMillis() - start; LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start); - Assert.assertTrue(costPoi * 3 > cost); + LOGGER.info("{} vs {}", cost, costPoi); + Assert.assertTrue(costPoi * 2 > cost); } private List data() { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java b/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java index 73da1c31..22b00e19 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.multiplesheets; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class MultipleSheetsData { private String title; } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java b/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java index dd010fc7..286ef8fe 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.noncamel; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class UnCamelData { private String string1; private String String2; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java b/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java index 7e22a2e7..fedabaa0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.parameter; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ParameterData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java b/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java index 0a7378f2..e77a995e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.repetition; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class RepetitionData { @ExcelProperty("字符串") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java index 6c541a24..0e43b3c8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.simple; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class SimpleData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java index 1476448b..8e0d55ee 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.skip; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class SkipData { @ExcelProperty("姓名") diff --git a/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java b/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java index 870abe87..668dd899 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.sort; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class SortData { private String column5; private String column6; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java index cc30893e..b6b67c9d 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java @@ -4,12 +4,16 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.annotation.write.style.HeadStyle; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @HeadStyle @HeadFontStyle public class StyleData { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java index a48c3238..e9323761 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java @@ -209,7 +209,10 @@ public class StyleDataTest { Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.getSheetAt(0); + Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); + Row row0 = sheet.getRow(0); + Assert.assertEquals(800, row0.getHeight(), 0); Cell cell00 = row0.getCell(0); Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00)); Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell00, workbook)); @@ -221,6 +224,7 @@ public class StyleDataTest { Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell01, workbook)); Row row1 = sheet.getRow(1); + Assert.assertEquals(1000, row1.getHeight(), 0); Cell cell10 = row1.getCell(0); Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10)); Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook)); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java b/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java index 31778a21..02d01215 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.template; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class TemplateData { @ExcelProperty("字符串0") private String string0; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java b/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java index 589f838c..6eb34ada 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.demo.fill; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData { private String name; private double number; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java index 357dbfe9..b29c39bf 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.metadata.data.CellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellDataReadDemoData { private CellData string; // 这里注意 虽然是日期 但是 类型 存储的是number 因为excel 存储的就是number diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java index bc27e1f8..2e87616f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java @@ -4,14 +4,18 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.NumberFormat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterData { /** * 我自定义 转换器,不管数据库传过来什么 。我给他加上“自定义:” diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java index a7a7519b..6c3321d2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.read; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData { private String string; private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java index e780bfa7..b0d9a03c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.demo.read; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoExtraData { private String row1; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java index 8040728d..b202311d 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.read; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExceptionDemoData { /** * 用日期去接字符串 肯定报错 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java index 1521b8cd..a545ffa8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class IndexOrNameData { /** * 强制读取第三个 这里不建议 index 和 name 同时用,要么一个对象只用index,要么一个对象只用name去匹配 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java index 4fa3a780..501ecc78 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DownloadData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java index 80a0e658..2993969a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.web; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class UploadData { private String string; private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java index 44ce5cc7..74a157d1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 复杂头数据.这里最终效果是第一行就一个主标题,第二行分类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ComplexHeadData { @ExcelProperty({"主标题", "字符串标题"}) private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java index 864cfcbf..c120e47a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java @@ -6,14 +6,18 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.NumberFormat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterData { /** * 我想所有的 字符串起前面加上"自定义:"三个字 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java index 8a7902b5..76b50584 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java @@ -3,17 +3,20 @@ package com.alibaba.easyexcel.test.demo.write; import java.util.Date; import com.alibaba.excel.annotation.ExcelIgnore; -import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java index ad94321e..44279db6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java @@ -5,14 +5,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ContentLoopMerge; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 样式的数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode // 将第6-7行的2-3列合并成一个单元格 // @OnceAbsoluteMerge(firstRowIndex = 5, lastRowIndex = 6, firstColumnIndex = 1, lastColumnIndex = 2) public class DemoMergeData { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java index 6dc60ed1..eded587b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java @@ -9,14 +9,18 @@ import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 样式的数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode // 头背景设置成红色 IndexedColors.RED.getIndex() @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 10) // 头字体设置成20 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java index a7bd7caa..4c39ffb1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java @@ -9,13 +9,17 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.converters.string.StringImageConverter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 图片导出类 * */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(100) @ColumnWidth(100 / 8) public class ImageDataWithAnnotation { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java index ba80d6f1..9f1ebca3 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java @@ -10,14 +10,18 @@ import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.converters.string.StringImageConverter; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 图片导出类 * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(100) @ColumnWidth(100 / 8) public class ImageDemoData { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java index 81c2d4e1..47c8c3b4 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class IndexData { @ExcelProperty(value = "字符串标题", index = 0) private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java index 69ad51dc..645be062 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class LongestMatchColumnWidthData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java index 42a7f883..a3a9b7f2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java @@ -7,14 +7,18 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(10) @HeadRowHeight(20) @ColumnWidth(25) diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java index 2dfed31d..0cd50ddd 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.write; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 根据WriteCellData写 * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteCellDemoData { /** * 超链接 diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java b/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java index 9c3f39e4..86b9f4f7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java @@ -1,13 +1,17 @@ package com.alibaba.easyexcel.test.temp; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author 是仪 */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CamlData { private String string1; private String String2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 3082362a..8c9badfe 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -8,6 +8,7 @@ import java.util.List; import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; @@ -147,4 +148,20 @@ public class Lock2Test { Thread.sleep(500 * 1000); } + @Test + public void test335() throws Exception { + + + LOGGER.info("reslut:{}", PositionUtils.getCol("A10",null)); + LOGGER.info("reslut:{}", PositionUtils.getRow("A10")); + LOGGER.info("reslut:{}", PositionUtils.getCol("AB10",null)); + LOGGER.info("reslut:{}", PositionUtils.getRow("AB10")); + + //LOGGER.info("reslut:{}", PositionUtils2.getCol("A10",null)); + //LOGGER.info("reslut:{}", PositionUtils2.getRow("A10")); + //LOGGER.info("reslut:{}", PositionUtils2.getCol("AB10",null)); + //LOGGER.info("reslut:{}", PositionUtils2.getRow("AB10")); + } + + } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java b/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java index 14fa65e3..88195cf9 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.temp; import com.alibaba.excel.annotation.format.NumberFormat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class LockData { @NumberFormat("#.##%") private Double string0; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java b/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java index 89214922..89453e5e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java @@ -2,13 +2,17 @@ package com.alibaba.easyexcel.test.temp; import java.util.List; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class StyleData { private byte[] byteValue; private Byte[] byteValue2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java b/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java index 55984a73..dbbb7058 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.temp; import com.alibaba.excel.annotation.write.style.ContentRowHeight; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(30) public class TempFillData { private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java b/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java index 37b8dc2b..b781f9fa 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java @@ -1,14 +1,19 @@ package com.alibaba.easyexcel.test.temp.bug; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jiaosong * @desc * @date 2021/4/6 */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataType { /** * 任务id diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java b/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java index 15ee006d..bcfa101c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java @@ -1,14 +1,19 @@ package com.alibaba.easyexcel.test.temp.bug; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jiaosong * @desc * @date 2021/4/6 */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HeadType { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java index 2f645339..1cc91c72 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java @@ -5,14 +5,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java b/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java index 8a4b1fb7..ef92e62c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java @@ -2,9 +2,13 @@ package com.alibaba.easyexcel.test.temp.data; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataType { /** * 任务id diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java b/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java index 32bd065e..2eaf0a27 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java @@ -2,9 +2,13 @@ package com.alibaba.easyexcel.test.temp.data; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class HeadType { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java b/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java index 86303729..bd7a7acb 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java @@ -1,16 +1,19 @@ package com.alibaba.easyexcel.test.temp.dataformat; import com.alibaba.excel.metadata.data.ReadCellData; -import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author 罗成 **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataFormatData { private ReadCellData date; private ReadCellData num; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java b/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java index 17c34cf1..0ddda379 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.temp.fill; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData2 { private String test; } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java index 53a1a5c8..818aa524 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java @@ -1,13 +1,18 @@ package com.alibaba.easyexcel.test.temp.issue1662; +import java.util.Date; + import com.alibaba.excel.annotation.ExcelProperty; + import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; -import java.util.Date; - -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor @NoArgsConstructor public class Data1662 { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java b/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java index 57b54107..0dfd27f7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java @@ -1,8 +1,12 @@ package com.alibaba.easyexcel.test.temp.issue1663; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData { private String name; private double number; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java b/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java index 43726ad3..59444b38 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java @@ -1,12 +1,16 @@ package com.alibaba.easyexcel.test.temp.large; import lombok.Builder; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @Builder public class LargeData { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java index 0edc995d..99540f2a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java @@ -4,14 +4,18 @@ import java.util.List; import com.alibaba.excel.metadata.data.CellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author 罗成 **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class TestCell { private CellData> c1; private CellData> c2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java index 62d5e317..90a721a2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java @@ -2,15 +2,18 @@ package com.alibaba.easyexcel.test.temp.read; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; -import lombok.experimental.Accessors; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 临时测试 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HeadReadData { @ExcelProperty({"主标题","数据1"}) private String h1; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java index 8de542d1..97c26ed9 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java @@ -7,9 +7,13 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData1 { @ExcelProperty("字符串标题") @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 42) diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java index 00d99041..ddc0e3db 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java @@ -7,9 +7,13 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData2 { @ExcelProperty("字符串标题") @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 42) diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java index a9ba7fa4..34a60bc8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java @@ -1,13 +1,17 @@ package com.alibaba.easyexcel.test.temp.simple; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class JsonData { private String SS1; private String sS2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java index 27967b9f..d9a035ce 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java @@ -2,16 +2,18 @@ package com.alibaba.easyexcel.test.temp.simple; import java.util.Date; -import com.alibaba.excel.annotation.write.style.ContentStyle; - -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * write data * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteData { // @ContentStyle(locked = true) private Date dd; diff --git a/update.md b/update.md index fdec0ee3..f1f90470 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,9 @@ +# 3.0.2 +* 大幅提升读写性能 +* 修复列宽注解没用的bug [Issue #2151](https://github.com/alibaba/easyexcel/issues/2151) +* 修复`CellData`接收失败的的bug [Issue #2147](https://github.com/alibaba/easyexcel/issues/2147) + + # 3.0.1 * 升级到正式版 * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124)
* Write key: */ - private Map> converterMap; + private Map> converterMap; public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) { this.newInitialization = Boolean.TRUE; @@ -81,7 +86,7 @@ public abstract class AbstractHolder implements ConfigurationHolder { } @Override - public Map> converterMap() { + public Map> converterMap() { return getConverterMap(); } diff --git a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java index f574f10e..0c11f942 100644 --- a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java +++ b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java @@ -5,14 +5,18 @@ import java.util.Locale; import com.alibaba.excel.converters.Converter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Basic parameter * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class BasicParameter { /** * You can only choose one of the {@link BasicParameter#head} and {@link BasicParameter#clazz} diff --git a/src/main/java/com/alibaba/excel/metadata/CellRange.java b/src/main/java/com/alibaba/excel/metadata/CellRange.java index bd0f54f9..8552d09f 100644 --- a/src/main/java/com/alibaba/excel/metadata/CellRange.java +++ b/src/main/java/com/alibaba/excel/metadata/CellRange.java @@ -1,11 +1,15 @@ package com.alibaba.excel.metadata; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellRange { private int firstRow; diff --git a/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java b/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java index ee4d7edf..e842466d 100644 --- a/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java @@ -3,9 +3,9 @@ package com.alibaba.excel.metadata; import java.util.Map; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; /** - * * Get the corresponding holder * * @author Jiaju Zhuang @@ -13,7 +13,6 @@ import com.alibaba.excel.converters.Converter; public interface ConfigurationHolder extends Holder { /** - * * Record whether it's new or from cache * * @return Record whether it's new or from cache @@ -32,5 +31,5 @@ public interface ConfigurationHolder extends Holder { * * @return Converter */ - Map> converterMap(); + Map> converterMap(); } diff --git a/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java index 987e553b..5cafe90c 100644 --- a/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java +++ b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java @@ -2,14 +2,18 @@ package com.alibaba.excel.metadata; import java.util.Locale; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Global configuration * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class GlobalConfiguration { /** * Automatic trim includes sheet name and content diff --git a/src/main/java/com/alibaba/excel/metadata/Head.java b/src/main/java/com/alibaba/excel/metadata/Head.java index 7e1fd47b..5aa1cc3a 100644 --- a/src/main/java/com/alibaba/excel/metadata/Head.java +++ b/src/main/java/com/alibaba/excel/metadata/Head.java @@ -10,14 +10,18 @@ import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty; import com.alibaba.excel.metadata.property.StyleProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * excel head * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class Head { /** * Column index of head diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java index f0e925e6..72beb30a 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java @@ -10,7 +10,7 @@ import com.alibaba.excel.enums.NumericCellTypeEnum; import com.alibaba.excel.metadata.data.FormulaData; import lombok.AccessLevel; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.apache.poi.ss.SpreadsheetVersion; @@ -29,7 +29,9 @@ import org.apache.poi.ss.util.CellRangeAddress; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvCell extends CellBase { /** diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java index 08b46e3a..a992f213 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java @@ -2,7 +2,9 @@ package com.alibaba.excel.metadata.csv; import com.alibaba.excel.metadata.data.DataFormatData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Color; @@ -16,7 +18,9 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvCellStyle implements CellStyle { /** diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java index 24acd9d9..89e76a71 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java @@ -3,7 +3,9 @@ package com.alibaba.excel.metadata.csv; import java.util.Iterator; import java.util.List; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; import org.apache.poi.ss.usermodel.Cell; @@ -17,7 +19,9 @@ import org.apache.poi.ss.usermodel.Sheet; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvRow implements Row { /** diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java index b002b6b4..79331677 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java @@ -16,7 +16,9 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.NumberDataFormatterUtils; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import org.apache.poi.ss.usermodel.AutoFilter; @@ -45,7 +47,9 @@ import org.apache.poi.ss.util.PaneInformation; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvSheet implements Sheet, Closeable { /** * workbook diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java index e26a2b65..4e713509 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java @@ -6,7 +6,9 @@ import java.util.Iterator; import java.util.List; import java.util.Locale; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.compress.utils.Lists; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.udf.UDFFinder; @@ -26,7 +28,9 @@ import org.apache.poi.ss.usermodel.Workbook; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvWorkbook implements Workbook { /** * output diff --git a/src/main/java/com/alibaba/excel/metadata/data/CellData.java b/src/main/java/com/alibaba/excel/metadata/data/CellData.java index ed6085f9..2f74ea9b 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/CellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/CellData.java @@ -6,7 +6,9 @@ import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.AbstractCell; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Excel internal cell data. @@ -15,7 +17,9 @@ import lombok.Data; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellData extends AbstractCell { /** * cell type diff --git a/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java b/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java index 2416635d..1963133d 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/ClientAnchorData.java @@ -1,7 +1,8 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.util.Internal; @@ -21,7 +22,9 @@ import org.apache.poi.util.Internal; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ClientAnchorData extends CoordinateData { /** * top diff --git a/src/main/java/com/alibaba/excel/metadata/data/CommentData.java b/src/main/java/com/alibaba/excel/metadata/data/CommentData.java index 08cff272..6373b0a6 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/CommentData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/CommentData.java @@ -1,13 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * comment * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CommentData extends ClientAnchorData { /** * Name of the original comment author diff --git a/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java b/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java index 69242e57..0755662b 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/CoordinateData.java @@ -1,13 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * coordinate. * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CoordinateData { /** * first row index.Priority is higher than {@link #relativeFirstRowIndex}. diff --git a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java index 877ca270..4f447632 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java @@ -2,14 +2,18 @@ package com.alibaba.excel.metadata.data; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * data format * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataFormatData { /** * index diff --git a/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java b/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java index 36d0206b..f71ff2eb 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/FormulaData.java @@ -1,13 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * formula * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FormulaData { /** * formula diff --git a/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java b/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java index 3fa23893..03edad13 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/HyperlinkData.java @@ -1,14 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; /** * hyperlink * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HyperlinkData extends CoordinateData { /** * Depending on the hyperlink type it can be URL, e-mail, path to a file, etc diff --git a/src/main/java/com/alibaba/excel/metadata/data/ImageData.java b/src/main/java/com/alibaba/excel/metadata/data/ImageData.java index 20cd43e1..2ca1cdc8 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/ImageData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/ImageData.java @@ -1,14 +1,17 @@ package com.alibaba.excel.metadata.data; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; +import lombok.Setter; /** * image * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ImageData extends ClientAnchorData { /** diff --git a/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java b/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java index 68231a19..e1bb57ca 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/ReadCellData.java @@ -4,15 +4,19 @@ import java.math.BigDecimal; import com.alibaba.excel.enums.CellDataTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * read cell data * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class ReadCellData extends CellData { /** diff --git a/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java b/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java index 1717741a..d277614f 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/RichTextStringData.java @@ -6,15 +6,19 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.metadata.style.WriteFont; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * rich text string * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class RichTextStringData { private String textString; @@ -25,7 +29,9 @@ public class RichTextStringData { this.textString = textString; } - @Data + @Getter + @Setter + @EqualsAndHashCode @AllArgsConstructor public static class IntervalFont { private Integer startIndex; diff --git a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java index 5a3487bf..f8ce9064 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java @@ -8,11 +8,12 @@ import java.util.List; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.util.ListUtils; -import com.alibaba.excel.write.metadata.fill.AnalysisCell; import com.alibaba.excel.write.metadata.style.WriteCellStyle; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import org.apache.poi.ss.usermodel.CellStyle; /** @@ -20,7 +21,9 @@ import org.apache.poi.ss.usermodel.CellStyle; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class WriteCellData extends CellData { /** @@ -56,10 +59,6 @@ public class WriteCellData extends CellData { */ private CellStyle originCellStyle; - /** - * Only in the case of the fill is not null - */ - private AnalysisCell analysisCell; public WriteCellData(String stringValue) { this(CellDataTypeEnum.STRING, stringValue); diff --git a/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java b/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java index bf76404f..ba8fee47 100644 --- a/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java +++ b/src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java @@ -111,6 +111,8 @@ public class DataFormatter { */ private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})"); + private static final Pattern E_NOTATION_PATTERN = Pattern.compile("E(\\d)"); + /** * Cells formatted with a date or time format and which contain invalid date or time values show 255 pound signs * ("#"). @@ -644,8 +646,7 @@ public class DataFormatter { */ private String getFormattedNumberString(BigDecimal data, Short dataFormat, String dataFormatString) { Format numberFormat = getFormat(data.doubleValue(), dataFormat, dataFormatString); - String formatted = numberFormat.format(data); - return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation + return E_NOTATION_PATTERN.matcher(numberFormat.format(data)).replaceFirst("E+$1"); } /** diff --git a/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java b/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java index d2bb20cd..10752b74 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/DateTimeFormatProperty.java @@ -3,14 +3,18 @@ package com.alibaba.excel.metadata.property; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.util.BooleanUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Configuration from annotations * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DateTimeFormatProperty { private String format; private Boolean use1904windowing; diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java index 1c0bb232..f0b72198 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java @@ -4,12 +4,16 @@ import java.lang.reflect.Field; import com.alibaba.excel.converters.Converter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcelContentProperty { public static final ExcelContentProperty EMPTY = new ExcelContentProperty(); diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index a0359fb3..f3a8474a 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -17,7 +17,9 @@ import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.write.metadata.holder.AbstractWriteHolder; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,7 +29,9 @@ import org.slf4j.LoggerFactory; * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcelHeadProperty { private static final Logger LOGGER = LoggerFactory.getLogger(ExcelHeadProperty.class); diff --git a/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java b/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java index 619cfe6b..c280b921 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/FontProperty.java @@ -4,7 +4,9 @@ import com.alibaba.excel.annotation.write.style.ContentFontStyle; import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.ss.usermodel.Font; @@ -15,7 +17,9 @@ import org.apache.poi.ss.usermodel.IndexedColors; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FontProperty { /** * The name for the font (i.e. Arial) diff --git a/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java b/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java index 4ef87f68..bb469cd3 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/StyleProperty.java @@ -5,7 +5,9 @@ import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.write.metadata.style.WriteFont; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.FillPatternType; @@ -19,7 +21,9 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class StyleProperty { /** * Set the data format (must be a valid format). Built in formats are defined at {@link BuiltinFormats}. diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 7ba8b88f..5c8bf4bb 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -91,7 +91,6 @@ public class ModelBuildEventListener implements ReadListener headMap = excelReadHeadProperty.getHeadMap(); - Map map = MapUtils.newHashMapWithExpectedSize(headMap.size()); BeanMap dataMap = BeanMapUtils.create(resultModel); for (Map.Entry entry : headMap.entrySet()) { Integer index = entry.getKey(); @@ -105,10 +104,9 @@ public class ModelBuildEventListener implements ReadListener(); } - - public Deque getTagDeque() { - return tagDeque; - } - - public void setTagDeque(Deque tagDeque) { - this.tagDeque = tagDeque; - } - - public Integer getColumnIndex() { - return columnIndex; - } - - public void setColumnIndex(Integer columnIndex) { - this.columnIndex = columnIndex; - } - - public StringBuilder getTempData() { - return tempData; - } - - public void setTempData(StringBuilder tempData) { - this.tempData = tempData; - } - - public StringBuilder getTempFormula() { - return tempFormula; - } - - public void setTempFormula(StringBuilder tempFormula) { - this.tempFormula = tempFormula; - } } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java index 066131d3..4e70aa0e 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java @@ -1,19 +1,31 @@ package com.alibaba.excel.read.metadata.holder.xlsx; -import javax.xml.parsers.SAXParserFactory; +import java.util.Map; -import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.xssf.model.StylesTable; +import javax.xml.parsers.SAXParserFactory; +import com.alibaba.excel.constant.BuiltinFormats; +import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.excel.util.MapUtils; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; /** * Workbook holder * * @author Jiaju Zhuang */ +@Getter +@Setter +@EqualsAndHashCode public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { /** * Package @@ -34,34 +46,30 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { * Current style information */ private StylesTable stylesTable; + /** + * cache data format + */ + private Map dataFormatDataCache; public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook); this.saxParserFactoryName = readWorkbook.getXlsxSAXParserFactoryName(); setExcelType(ExcelTypeEnum.XLSX); + dataFormatDataCache = MapUtils.newHashMap(); } - public OPCPackage getOpcPackage() { - return opcPackage; + public DataFormatData dataFormatData(int dateFormatIndexInteger) { + return dataFormatDataCache.computeIfAbsent(dateFormatIndexInteger, key -> { + DataFormatData dataFormatData = new DataFormatData(); + if (stylesTable == null) { + return null; + } + XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger); + dataFormatData.setIndex(xssfCellStyle.getDataFormat()); + dataFormatData.setFormat(BuiltinFormats.getBuiltinFormat(dataFormatData.getIndex(), + xssfCellStyle.getDataFormatString(), globalConfiguration().getLocale())); + return dataFormatData; + }); } - public void setOpcPackage(OPCPackage opcPackage) { - this.opcPackage = opcPackage; - } - - public String getSaxParserFactoryName() { - return saxParserFactoryName; - } - - public void setSaxParserFactoryName(String saxParserFactoryName) { - this.saxParserFactoryName = saxParserFactoryName; - } - - public StylesTable getStylesTable() { - return stylesTable; - } - - public void setStylesTable(StylesTable stylesTable) { - this.stylesTable = stylesTable; - } } diff --git a/src/main/java/com/alibaba/excel/util/ClassUtils.java b/src/main/java/com/alibaba/excel/util/ClassUtils.java index 9020d56c..82f78af2 100644 --- a/src/main/java/com/alibaba/excel/util/ClassUtils.java +++ b/src/main/java/com/alibaba/excel/util/ClassUtils.java @@ -31,6 +31,10 @@ import com.alibaba.excel.metadata.property.NumberFormatProperty; import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.write.metadata.holder.WriteHolder; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import net.sf.cglib.beans.BeanMap; /** @@ -51,7 +55,7 @@ public class ClassUtils { /** * The cache configuration information for each of the class */ - public static final Map CONTENT_CACHE = new ConcurrentHashMap<>(); + public static final Map CONTENT_CACHE = new ConcurrentHashMap<>(); /** * Calculate the configuration information for the class @@ -117,20 +121,8 @@ public class ClassUtils { } } - private static String buildKey(Class> clazz, Class> headClass, String fieldName) { - String key = ""; - if (clazz != null) { - key += clazz.getName(); - } - key += "-"; - if (headClass != null) { - key += headClass.getName(); - } - key += "-"; - if (fieldName != null) { - key += fieldName; - } - return key; + private static ContentPropertyKey buildKey(Class> clazz, Class> headClass, String fieldName) { + return new ContentPropertyKey(clazz, headClass, fieldName); } private static Map declaredFieldContentMap(Class> clazz) { @@ -413,4 +405,13 @@ public class ClassUtils { } } + @Getter + @Setter + @EqualsAndHashCode + @AllArgsConstructor + public static class ContentPropertyKey { + private Class> clazz; + private Class> headClass; + private String fieldName; + } } diff --git a/src/main/java/com/alibaba/excel/util/ConverterUtils.java b/src/main/java/com/alibaba/excel/util/ConverterUtils.java index b666be6e..02ea4a00 100644 --- a/src/main/java/com/alibaba/excel/util/ConverterUtils.java +++ b/src/main/java/com/alibaba/excel/util/ConverterUtils.java @@ -8,10 +8,12 @@ import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKeyBuild; +import com.alibaba.excel.converters.ConverterKeyBuild.ConverterKey; import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.ReadConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.exception.ExcelDataConvertException; +import com.alibaba.excel.metadata.data.CellData; import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; @@ -80,7 +82,7 @@ public class ConverterUtils { * @return */ public static Object convertToJavaObject(ReadCellData> cellData, Field field, - ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, + ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) { Class> clazz; if (field == null) { @@ -88,7 +90,7 @@ public class ConverterUtils { } else { clazz = field.getType(); } - if (clazz == ReadCellData.class) { + if (clazz == CellData.class || clazz == ReadCellData.class) { Class> classGeneric = getClassGeneric(field.getGenericType()); ReadCellData cellDataReturn = cellData.clone(); cellDataReturn.setData(doConvertToJavaObject(cellData, classGeneric, contentProperty, converterMap, @@ -126,7 +128,7 @@ public class ConverterUtils { * @return */ private static Object doConvertToJavaObject(ReadCellData> cellData, Class> clazz, - ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, + ExcelContentProperty contentProperty, Map> converterMap, AnalysisContext context, Integer rowIndex, Integer columnIndex) { Converter> converter = null; if (contentProperty != null) { diff --git a/src/main/java/com/alibaba/excel/util/PositionUtils.java b/src/main/java/com/alibaba/excel/util/PositionUtils.java index 48c5cde5..fe7e5d4e 100644 --- a/src/main/java/com/alibaba/excel/util/PositionUtils.java +++ b/src/main/java/com/alibaba/excel/util/PositionUtils.java @@ -1,11 +1,7 @@ package com.alibaba.excel.util; -import java.util.Locale; -import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.poi.ss.util.CellReference; - /** * @author jipengfei */ @@ -18,14 +14,6 @@ public class PositionUtils { private PositionUtils() {} - public static int getRowByRowTagt(String rowTagt) { - int row = 0; - if (rowTagt != null) { - row = Integer.parseInt(rowTagt) - 1; - } - return row; - } - public static int getRowByRowTagt(String rowTagt, Integer before) { int row; if (rowTagt != null) { @@ -37,47 +25,40 @@ public class PositionUtils { } return before + 1; } - } public static int getRow(String currentCellIndex) { - if (currentCellIndex != null) { - int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER); - String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT); - Matcher matcher = CELL_REF_PATTERN.matcher(cell); - if (!matcher.matches()) { - throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex); + if (currentCellIndex == null) { + return -1; + } + int firstNumber = currentCellIndex.length() - 1; + for (; firstNumber >= 0; firstNumber--) { + char c = currentCellIndex.charAt(firstNumber); + if (c < '0' || c > '9') { + break; } - String row = matcher.group(2); - return Integer.parseInt(row) - 1; } - return -1; + return Integer.parseUnsignedInt(currentCellIndex.substring(firstNumber + 1)) - 1; } public static int getCol(String currentCellIndex, Integer before) { - if (currentCellIndex != null) { - int plingPos = currentCellIndex.lastIndexOf(SHEET_NAME_DELIMITER); - String cell = currentCellIndex.substring(plingPos + 1).toUpperCase(Locale.ROOT); - Matcher matcher = CELL_REF_PATTERN.matcher(cell); - if (!matcher.matches()) { - throw new IllegalArgumentException("Invalid CellReference: " + currentCellIndex); - } - String col = matcher.group(1); - - if (col.length() > 0 && col.charAt(0) == REDUNDANT_CHARACTERS) { - col = col.substring(1); - } - if (col.length() == 0) { - return -1; - } else { - return CellReference.convertColStringToIndex(col); - } - } else { + if (currentCellIndex == null) { if (before == null) { before = -1; } return before + 1; } + int firstNumber = currentCellIndex.charAt(0) == REDUNDANT_CHARACTERS ? 1 : 0; + int col = 0; + for (; firstNumber < currentCellIndex.length(); firstNumber++) { + char c = currentCellIndex.charAt(firstNumber); + boolean isNotLetter = c == REDUNDANT_CHARACTERS || (c >= '0' && c <= '9'); + if (isNotLetter) { + break; + } + col = col * 26 + Character.toUpperCase(c) - 'A' + 1; + } + return col - 1; } } diff --git a/src/main/java/com/alibaba/excel/util/StyleUtil.java b/src/main/java/com/alibaba/excel/util/StyleUtil.java index 1bafcdc8..e8a503d7 100644 --- a/src/main/java/com/alibaba/excel/util/StyleUtil.java +++ b/src/main/java/com/alibaba/excel/util/StyleUtil.java @@ -1,5 +1,7 @@ package com.alibaba.excel.util; +import java.util.Optional; + import com.alibaba.excel.constant.BuiltinFormats; import com.alibaba.excel.metadata.data.DataFormatData; import com.alibaba.excel.metadata.data.HyperlinkData; @@ -189,7 +191,11 @@ public class StyleUtil { xssfFont.setStrikeout(xssfOriginFont.getStrikeout()); // Colors cannot be overwritten if (writeFont == null || writeFont.getColor() == null) { - xssfFont.setColor(new XSSFColor(xssfOriginFont.getXSSFColor().getRGB(), null)); + xssfFont.setColor(Optional.of(xssfOriginFont) + .map(XSSFFont::getXSSFColor) + .map(XSSFColor::getRGB) + .map(rgb -> new XSSFColor(rgb, null)) + .orElse(null)); } xssfFont.setTypeOffset(xssfOriginFont.getTypeOffset()); xssfFont.setUnderline(xssfOriginFont.getUnderline()); diff --git a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java index 78ce8d8e..062bfdc7 100644 --- a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java +++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java @@ -1,24 +1,19 @@ package com.alibaba.excel.util; -import java.util.List; -import java.util.Map; - import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; -import com.alibaba.excel.write.handler.CellWriteHandler; -import com.alibaba.excel.write.handler.RowWriteHandler; -import com.alibaba.excel.write.handler.SheetWriteHandler; -import com.alibaba.excel.write.handler.WorkbookWriteHandler; -import com.alibaba.excel.write.handler.WriteHandler; +import com.alibaba.excel.write.handler.chain.CellHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.RowHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.SheetHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.WorkbookHandlerExecutionChain; 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.holder.AbstractWriteHolder; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.poi.ss.usermodel.Cell; +import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.Row; /** @@ -26,228 +21,162 @@ import org.apache.poi.ss.usermodel.Row; * * @author Jiaju Zhuang */ +@Slf4j public class WriteHandlerUtils { private WriteHandlerUtils() {} - public static void beforeWorkbookCreate(WriteContext writeContext) { - beforeWorkbookCreate(writeContext, false); + public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext) { + WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, + writeContext.writeWorkbookHolder()); + writeContext.writeWorkbookHolder().setWorkbookWriteHandlerContext(context); + return context; } - public static void beforeWorkbookCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { - ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(context); - } + public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context) { + beforeWorkbookCreate(context, false); + } + + public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.beforeWorkbookCreate(context); } } - public static void afterWorkbookCreate(WriteContext writeContext) { - afterWorkbookCreate(writeContext, false); + public static void afterWorkbookCreate(WorkbookWriteHandlerContext context) { + afterWorkbookCreate(context, false); } - public static void afterWorkbookCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { - ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(context); - } + public static void afterWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.afterWorkbookCreate(context); } } - public static void afterWorkbookDispose(WriteContext writeContext) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(WorkbookWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof WorkbookWriteHandler) { - ((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(context); - } + private static WorkbookHandlerExecutionChain getWorkbookHandlerExecutionChain(WorkbookWriteHandlerContext context, + boolean runOwn) { + AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder(); + if (runOwn) { + return abstractWriteHolder.getOwnWorkbookHandlerExecutionChain(); + } else { + return abstractWriteHolder.getWorkbookHandlerExecutionChain(); } } - public static void beforeSheetCreate(WriteContext writeContext) { - beforeSheetCreate(writeContext, false); + public static void afterWorkbookDispose(WorkbookWriteHandlerContext context) { + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, false); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.afterWorkbookDispose(context); + } } - public static void beforeSheetCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof SheetWriteHandler) { - ((SheetWriteHandler)writeHandler).beforeSheetCreate(context); - } - } + public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext) { + return new SheetWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder()); } - public static void afterSheetCreate(WriteContext writeContext) { - afterSheetCreate(writeContext, false); + public static void beforeSheetCreate(SheetWriteHandlerContext context) { + beforeSheetCreate(context, false); } - public static void afterSheetCreate(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder()); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof SheetWriteHandler) { - ((SheetWriteHandler)writeHandler).afterSheetCreate(context); - } + public static void beforeSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { + SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn); + if (sheetHandlerExecutionChain != null) { + sheetHandlerExecutionChain.beforeSheetCreate(context); } } - public static void beforeCellCreate(WriteContext writeContext, Row row, Head head, Integer columnIndex, - Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, null, columnIndex, relativeRowIndex, - head, null, null, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).beforeCellCreate(context); - } - } + public static void afterSheetCreate(SheetWriteHandlerContext context) { + afterSheetCreate(context, false); } - public static void afterCellCreate(WriteContext writeContext, Cell cell, Head head, Integer relativeRowIndex, - Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, null, null, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).afterCellCreate(context); - } + public static void afterSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { + SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn); + if (sheetHandlerExecutionChain != null) { + sheetHandlerExecutionChain.afterSheetCreate(context); } } - public static void afterCellDataConverted(WriteContext writeContext, WriteCellData> cellData, Cell cell, - Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - CellWriteHandlerContext context = new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), cell.getRow(), cell, - cell.getColumnIndex(), relativeRowIndex, head, cellDataList, cellData, isHead, excelContentProperty); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).afterCellDataConverted(context); - } + private static SheetHandlerExecutionChain getSheetHandlerExecutionChain(SheetWriteHandlerContext context, + boolean runOwn) { + AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder(); + if (runOwn) { + return abstractWriteHolder.getOwnSheetHandlerExecutionChain(); + } else { + return abstractWriteHolder.getSheetHandlerExecutionChain(); } } - public static void afterCellDispose(WriteContext writeContext, WriteCellData> cellData, Cell cell, Head head, - Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List> cellDataList = cellData == null ? null : ListUtils.newArrayList(cellData); - afterCellDispose(writeContext, cellDataList, cell, head, relativeRowIndex, isHead, excelContentProperty); + public static CellWriteHandlerContext createCellWriteHandlerContext(WriteContext writeContext, Row row, + Integer rowIndex, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, + ExcelContentProperty excelContentProperty) { + return new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, rowIndex, null, columnIndex, + relativeRowIndex, head, null, null, isHead, excelContentProperty); } - public static void afterCellDispose(WriteContext writeContext, List> cellDataList, Cell cell, - Head head, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = - writeContext.currentWriteHolder().writeHandlerMap().get(CellWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; + public static void beforeCellCreate(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.beforeCellCreate(context); } - WriteCellData> cellData = null; - if (CollectionUtils.isNotEmpty(cellDataList)) { - cellData = cellDataList.get(0); + } + + public static void afterCellCreate(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellCreate(context); } - 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) { - if (writeHandler instanceof CellWriteHandler) { - ((CellWriteHandler)writeHandler).afterCellDispose(context); - } + } + + public static void afterCellDataConverted(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellDataConverted(context); } } - public static void beforeRowCreate(WriteContext writeContext, Integer rowIndex, Integer relativeRowIndex, - Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; + public static void afterCellDispose(CellWriteHandlerContext context) { + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellDispose(context); } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + } + + public static RowWriteHandlerContext createRowWriteHandlerContext(WriteContext writeContext, Integer rowIndex, + Integer relativeRowIndex, Boolean isHead) { + return new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { - ((RowWriteHandler)writeHandler).beforeRowCreate(context); - } - } } - public static void afterRowCreate(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, - isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { - ((RowWriteHandler)writeHandler).afterRowCreate(context); - } + public static void beforeRowCreate(RowWriteHandlerContext context) { + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.beforeRowCreate(context); } } - public static void afterRowDispose(WriteContext writeContext, Row row, Integer relativeRowIndex, Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get(RowWriteHandler.class); - if (handlerList == null || handlerList.isEmpty()) { - return; - } - RowWriteHandlerContext context = new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row.getRowNum(), row, relativeRowIndex, - isHead); - for (WriteHandler writeHandler : handlerList) { - if (writeHandler instanceof RowWriteHandler) { - ((RowWriteHandler)writeHandler).afterRowDispose(context); - } + public static void afterRowCreate(RowWriteHandlerContext context) { + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.afterRowCreate(context); } } - private static List getHandlerList(WriteContext writeContext, Class extends WriteHandler> clazz, - boolean runOwn) { - Map, List> writeHandlerMap; - if (runOwn) { - writeHandlerMap = writeContext.currentWriteHolder().ownWriteHandlerMap(); - } else { - writeHandlerMap = writeContext.currentWriteHolder().writeHandlerMap(); + public static void afterRowDispose(RowWriteHandlerContext context) { + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.afterRowDispose(context); } - return writeHandlerMap.get(clazz); } + } diff --git a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java index 597d94de..9d10ca1e 100644 --- a/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/AbstractExcelWriteExecutor.java @@ -8,8 +8,7 @@ import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.NullableObjectConverter; import com.alibaba.excel.converters.WriteConverterContext; import com.alibaba.excel.enums.CellDataTypeEnum; -import com.alibaba.excel.exception.ExcelDataConvertException; -import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.exception.ExcelWriteDataConvertException; import com.alibaba.excel.metadata.data.CommentData; import com.alibaba.excel.metadata.data.FormulaData; import com.alibaba.excel.metadata.data.HyperlinkData; @@ -19,10 +18,11 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.FileTypeUtils; +import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.StyleUtil; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; -import com.alibaba.excel.write.metadata.holder.WriteHolder; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; @@ -33,6 +33,7 @@ import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFClientAnchor; /** @@ -47,124 +48,140 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { this.writeContext = writeContext; } - protected WriteCellData> converterAndSet(WriteHolder currentWriteHolder, Class> clazz, - CellDataTypeEnum targetType, Cell cell, Object value, ExcelContentProperty excelContentProperty, Head head, - Integer relativeRowIndex, int rowIndex, int columnIndex) { - boolean needTrim = value != null && (value instanceof String && currentWriteHolder.globalConfiguration() - .getAutoTrim()); - if (needTrim) { - value = ((String)value).trim(); - } - WriteCellData> cellData = convert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); - WriteHandlerUtils.afterCellDataConverted(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + /** + * Transform the data and then to set into the cell + * + * @param cellWriteHandlerContext context + * @return + */ + protected void converterAndSet(CellWriteHandlerContext cellWriteHandlerContext) { + + WriteCellData> cellData = convert(cellWriteHandlerContext); + cellWriteHandlerContext.setCellDataList(ListUtils.newArrayList(cellData)); + cellWriteHandlerContext.setFirstCellData(cellData); + + WriteHandlerUtils.afterCellDataConverted(cellWriteHandlerContext); // Fill in picture information - fillImage(cell, cellData.getImageDataList()); + fillImage(cellWriteHandlerContext, cellData.getImageDataList()); // Fill in comment information - fillComment(cell, cellData.getCommentData()); + fillComment(cellWriteHandlerContext, cellData.getCommentData()); // Fill in hyper link information - fillHyperLink(cell, cellData.getHyperlinkData()); + fillHyperLink(cellWriteHandlerContext, cellData.getHyperlinkData()); // Fill in formula information - fillFormula(cell, cellData.getFormulaData()); + fillFormula(cellWriteHandlerContext, cellData.getFormulaData()); // Fill index - cellData.setRowIndex(rowIndex); - cellData.setColumnIndex(columnIndex); + cellData.setRowIndex(cellWriteHandlerContext.getRowIndex()); + cellData.setColumnIndex(cellWriteHandlerContext.getColumnIndex()); if (cellData.getType() == null) { cellData.setType(CellDataTypeEnum.EMPTY); } + Cell cell = cellWriteHandlerContext.getCell(); switch (cellData.getType()) { case STRING: cell.setCellValue(cellData.getStringValue()); - return cellData; + return; case BOOLEAN: cell.setCellValue(cellData.getBooleanValue()); - return cellData; + return; case NUMBER: cell.setCellValue(cellData.getNumberValue().doubleValue()); - return cellData; + return; case DATE: cell.setCellValue(cellData.getDateValue()); - return cellData; + return; case RICH_TEXT_STRING: cell.setCellValue(StyleUtil .buildRichTextString(writeContext.writeWorkbookHolder(), cellData.getRichTextStringDataValue())); - return cellData; + return; case EMPTY: - return cellData; + return; default: - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), cellData, - excelContentProperty, "Not supported data:" + value + " return type:" + cell.getCellType() - + "at row:" + cell.getRow().getRowNum()); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Not supported data:" + cellWriteHandlerContext.getOriginalValue() + " return type:" + + cellData.getType() + + "at row:" + cellWriteHandlerContext.getRowIndex()); } } - private void fillFormula(Cell cell, FormulaData formulaData) { + private void fillFormula(CellWriteHandlerContext cellWriteHandlerContext, FormulaData formulaData) { if (formulaData == null) { return; } + Cell cell = cellWriteHandlerContext.getCell(); if (formulaData.getFormulaValue() != null) { cell.setCellFormula(formulaData.getFormulaValue()); } } - private void fillHyperLink(Cell cell, HyperlinkData hyperlinkData) { + private void fillHyperLink(CellWriteHandlerContext cellWriteHandlerContext, HyperlinkData hyperlinkData) { if (hyperlinkData == null) { return; } - CreationHelper helper = cell.getSheet().getWorkbook().getCreationHelper(); + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); + Cell cell = cellWriteHandlerContext.getCell(); + + CreationHelper helper = workbook.getCreationHelper(); Hyperlink hyperlink = helper.createHyperlink(StyleUtil.getHyperlinkType(hyperlinkData.getHyperlinkType())); hyperlink.setAddress(hyperlinkData.getAddress()); - hyperlink.setFirstRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getFirstRowIndex(), + hyperlink.setFirstRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getFirstRowIndex(), hyperlinkData.getRelativeFirstRowIndex())); - hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getFirstColumnIndex(), + hyperlink.setFirstColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getFirstColumnIndex(), hyperlinkData.getRelativeFirstColumnIndex())); - hyperlink.setLastRow(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), hyperlinkData.getLastRowIndex(), + hyperlink.setLastRow(StyleUtil.getCellCoordinate(rowIndex, hyperlinkData.getLastRowIndex(), hyperlinkData.getRelativeLastRowIndex())); - hyperlink.setLastColumn(StyleUtil.getCellCoordinate(cell.getColumnIndex(), hyperlinkData.getLastColumnIndex(), + hyperlink.setLastColumn(StyleUtil.getCellCoordinate(columnIndex, hyperlinkData.getLastColumnIndex(), hyperlinkData.getRelativeLastColumnIndex())); cell.setHyperlink(hyperlink); } - private void fillComment(Cell cell, CommentData commentData) { + private void fillComment(CellWriteHandlerContext cellWriteHandlerContext, CommentData commentData) { if (commentData == null) { return; } ClientAnchor anchor; + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); + Cell cell = cellWriteHandlerContext.getCell(); + if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.XLSX) { anchor = new XSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getBottom()), - StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), + StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), commentData.getRelativeFirstColumnIndex()), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), commentData.getRelativeFirstRowIndex()), - StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), + StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), commentData.getRelativeLastColumnIndex()) + 1, - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), commentData.getRelativeLastRowIndex()) + 1); } else { anchor = new HSSFClientAnchor(StyleUtil.getCoordinate(commentData.getLeft()), StyleUtil.getCoordinate(commentData.getTop()), StyleUtil.getCoordinate(commentData.getRight()), StyleUtil.getCoordinate(commentData.getBottom()), - (short)StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getFirstColumnIndex(), + (short)StyleUtil.getCellCoordinate(columnIndex, commentData.getFirstColumnIndex(), commentData.getRelativeFirstColumnIndex()), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getFirstRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getFirstRowIndex(), commentData.getRelativeFirstRowIndex()), - (short)(StyleUtil.getCellCoordinate(cell.getColumnIndex(), commentData.getLastColumnIndex(), + (short)(StyleUtil.getCellCoordinate(columnIndex, commentData.getLastColumnIndex(), commentData.getRelativeLastColumnIndex()) + 1), - StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), commentData.getLastRowIndex(), + StyleUtil.getCellCoordinate(rowIndex, commentData.getLastRowIndex(), commentData.getRelativeLastRowIndex()) + 1); } - Comment comment = cell.getSheet().createDrawingPatriarch().createCellComment(anchor); + + Comment comment = sheet.createDrawingPatriarch().createCellComment(anchor); if (commentData.getRichTextStringData() != null) { comment.setString( StyleUtil.buildRichTextString(writeContext.writeWorkbookHolder(), commentData.getRichTextStringData())); @@ -175,18 +192,22 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { cell.setCellComment(comment); } - protected void fillImage(Cell cell, List imageDataList) { + protected void fillImage(CellWriteHandlerContext cellWriteHandlerContext, List imageDataList) { if (CollectionUtils.isEmpty(imageDataList)) { return; } - Sheet sheet = cell.getSheet(); + Integer rowIndex = cellWriteHandlerContext.getRowIndex(); + Integer columnIndex = cellWriteHandlerContext.getColumnIndex(); + Sheet sheet = cellWriteHandlerContext.getWriteSheetHolder().getSheet(); + Workbook workbook = cellWriteHandlerContext.getWriteWorkbookHolder().getWorkbook(); + Drawing> drawing = sheet.getDrawingPatriarch(); if (drawing == null) { drawing = sheet.createDrawingPatriarch(); } CreationHelper helper = sheet.getWorkbook().getCreationHelper(); for (ImageData imageData : imageDataList) { - int index = sheet.getWorkbook().addPicture(imageData.getImage(), + int index = workbook.addPicture(imageData.getImage(), FileTypeUtils.getImageTypeFormat(imageData.getImage())); ClientAnchor anchor = helper.createClientAnchor(); if (imageData.getTop() != null) { @@ -201,13 +222,13 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { if (imageData.getLeft() != null) { anchor.setDx1(StyleUtil.getCoordinate(imageData.getLeft())); } - anchor.setRow1(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getFirstRowIndex(), + anchor.setRow1(StyleUtil.getCellCoordinate(rowIndex, imageData.getFirstRowIndex(), imageData.getRelativeFirstRowIndex())); - anchor.setCol1(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getFirstColumnIndex(), + anchor.setCol1(StyleUtil.getCellCoordinate(columnIndex, imageData.getFirstColumnIndex(), imageData.getRelativeFirstColumnIndex())); - anchor.setRow2(StyleUtil.getCellCoordinate(cell.getRow().getRowNum(), imageData.getLastRowIndex(), + anchor.setRow2(StyleUtil.getCellCoordinate(rowIndex, imageData.getLastRowIndex(), imageData.getRelativeLastRowIndex()) + 1); - anchor.setCol2(StyleUtil.getCellCoordinate(cell.getColumnIndex(), imageData.getLastColumnIndex(), + anchor.setCol2(StyleUtil.getCellCoordinate(columnIndex, imageData.getLastColumnIndex(), imageData.getRelativeLastColumnIndex()) + 1); if (imageData.getAnchorType() != null) { anchor.setAnchorType(imageData.getAnchorType().getValue()); @@ -216,17 +237,16 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } } - protected WriteCellData> convert(WriteHolder currentWriteHolder, Class> clazz, CellDataTypeEnum targetType, - Cell cell, Object value, ExcelContentProperty excelContentProperty) { + protected WriteCellData> convert(CellWriteHandlerContext cellWriteHandlerContext) { // This means that the user has defined the data. - if (clazz == WriteCellData.class) { - if (value == null) { + if (cellWriteHandlerContext.getOriginalFieldClass() == WriteCellData.class) { + if (cellWriteHandlerContext.getOriginalValue() == null) { return new WriteCellData<>(CellDataTypeEnum.EMPTY); } - WriteCellData> cellDataValue = (WriteCellData>)value; + WriteCellData> cellDataValue = (WriteCellData>)cellWriteHandlerContext.getOriginalValue(); if (cellDataValue.getType() != null) { // Configuration information may not be read here - fillProperty(cellDataValue, excelContentProperty); + fillProperty(cellDataValue, cellWriteHandlerContext.getExcelContentProperty()); return cellDataValue; } else { @@ -235,8 +255,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { return cellDataValue; } } - WriteCellData> cellDataReturn = doConvert(currentWriteHolder, cellDataValue.getData().getClass(), - targetType, cell, cellDataValue.getData(), excelContentProperty); + WriteCellData> cellDataReturn = doConvert(cellWriteHandlerContext); if (cellDataValue.getImageDataList() != null) { cellDataReturn.setImageDataList(cellDataValue.getImageDataList()); @@ -256,7 +275,7 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } return cellDataReturn; } - return doConvert(currentWriteHolder, clazz, targetType, cell, value, excelContentProperty); + return doConvert(cellWriteHandlerContext); } private void fillProperty(WriteCellData> cellDataValue, ExcelContentProperty excelContentProperty) { @@ -280,8 +299,9 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { } } - private WriteCellData> doConvert(WriteHolder currentWriteHolder, Class> clazz, CellDataTypeEnum targetType, - Cell cell, Object value, ExcelContentProperty excelContentProperty) { + private WriteCellData> doConvert(CellWriteHandlerContext cellWriteHandlerContext) { + ExcelContentProperty excelContentProperty = cellWriteHandlerContext.getExcelContentProperty(); + Converter> converter = null; if (excelContentProperty != null) { converter = excelContentProperty.getConverter(); @@ -289,31 +309,34 @@ public abstract class AbstractExcelWriteExecutor implements ExcelWriteExecutor { if (converter == null) { // csv is converted to string by default if (writeContext.writeWorkbookHolder().getExcelType() == ExcelTypeEnum.CSV) { - targetType = CellDataTypeEnum.STRING; + cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING); } - converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz, targetType)); + converter = writeContext.currentWriteHolder().converterMap().get( + ConverterKeyBuild.buildKey(cellWriteHandlerContext.getOriginalFieldClass(), + cellWriteHandlerContext.getTargetCellDataType())); } - if (value == null && !(converter instanceof NullableObjectConverter)) { + if (cellWriteHandlerContext.getOriginalValue() == null && !(converter instanceof NullableObjectConverter)) { return new WriteCellData<>(CellDataTypeEnum.EMPTY); } if (converter == null) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Can not find 'Converter' support class " + clazz.getSimpleName() + "."); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Can not find 'Converter' support class " + cellWriteHandlerContext.getOriginalFieldClass() + .getSimpleName() + "."); } WriteCellData> cellData; try { cellData = ((Converter)converter).convertToExcelData( - new WriteConverterContext<>(value, excelContentProperty, writeContext)); + new WriteConverterContext<>(cellWriteHandlerContext.getOriginalValue(), excelContentProperty, + writeContext)); } catch (Exception e) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Convert data:" + value + " error, at row:" + cell.getRow().getRowNum(), e); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " error, at row:" + + cellWriteHandlerContext.getRowIndex(), e); } if (cellData == null || cellData.getType() == null) { - throw new ExcelDataConvertException(cell.getRow().getRowNum(), cell.getColumnIndex(), - new WriteCellData<>(CellDataTypeEnum.EMPTY), excelContentProperty, - "Convert data:" + value + " return null, at row:" + cell.getRow().getRowNum()); + throw new ExcelWriteDataConvertException(cellWriteHandlerContext, + "Convert data:" + cellWriteHandlerContext.getOriginalValue() + " return null, at row:" + + cellWriteHandlerContext.getRowIndex()); } return cellData; } diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java index bdfea49e..0bb4007c 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -11,13 +11,14 @@ import java.util.TreeMap; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.ClassUtils; import com.alibaba.excel.util.FieldUtils; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.alibaba.excel.write.metadata.CollectionRowData; import com.alibaba.excel.write.metadata.MapRowData; import com.alibaba.excel.write.metadata.RowData; @@ -64,9 +65,14 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { if (oneRowData == null) { return; } - WriteHandlerUtils.beforeRowCreate(writeContext, rowIndex, relativeRowIndex, Boolean.FALSE); + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, + rowIndex, relativeRowIndex, Boolean.FALSE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + Row row = WorkBookUtil.createRow(writeContext.writeSheetHolder().getSheet(), rowIndex); - WriteHandlerUtils.afterRowCreate(writeContext, row, relativeRowIndex, Boolean.FALSE); + rowWriteHandlerContext.setRow(row); + + WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext); if (oneRowData instanceof Collection>) { addBasicTypeToExcel(new CollectionRowData((Collection>)oneRowData), row, rowIndex, relativeRowIndex); @@ -75,7 +81,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { } else { addJavaObjectToExcel(oneRowData, row, rowIndex, relativeRowIndex, sortedAllFiledMap); } - WriteHandlerUtils.afterRowDispose(writeContext, row, relativeRowIndex, Boolean.FALSE); + + WriteHandlerUtils.afterRowDispose(rowWriteHandlerContext); } private void addBasicTypeToExcel(RowData oneRowData, Row row, int rowIndex, int relativeRowIndex) { @@ -114,23 +121,30 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), head == null ? null : head.getFieldName()); - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext(writeContext, + row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, head, relativeRowIndex, Boolean.FALSE, - excelContentProperty); - Object value = oneRowData.get(dataIndex); - WriteCellData> cellData = converterAndSet(writeContext.currentWriteHolder(), - FieldUtils.getFieldClass(value), null, cell, value, null, head, relativeRowIndex, rowIndex, columnIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, head, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(oneRowData.get(dataIndex)); + cellWriteHandlerContext.setOriginalFieldClass( + FieldUtils.getFieldClass(cellWriteHandlerContext.getOriginalValue())); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } private void addJavaObjectToExcel(Object oneRowData, Row row, int rowIndex, int relativeRowIndex, Map sortedAllFiledMap) { WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); BeanMap beanMap = BeanMapUtils.create(oneRowData); - Set beanMapHandledSet = new HashSet(); + // Bean the contains of the Map Key method with poor performance,So to create a keySet here + Set beanKeySet = new HashSet<>(beanMap.keySet()); + Set beanMapHandledSet = new HashSet<>(); int maxCellIndex = -1; // If it's a class it needs to be cast by type if (HeadKindEnum.CLASS.equals(writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadKind())) { @@ -139,21 +153,28 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { int columnIndex = entry.getKey(); Head head = entry.getValue(); String name = head.getFieldName(); - if (!beanMap.containsKey(name)) { + if (!beanKeySet.contains(name)) { continue; } + ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), name); - WriteHandlerUtils.beforeCellCreate(writeContext, row, head, columnIndex, relativeRowIndex, - Boolean.FALSE, excelContentProperty); - Cell cell = WorkBookUtil.createCell(row, columnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, 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, + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, row, rowIndex, head, columnIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + + Cell cell = WorkBookUtil.createCell(row, columnIndex); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(beanMap.get(name)); + cellWriteHandlerContext.setOriginalFieldClass(head.getField().getType()); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); + beanMapHandledSet.add(name); maxCellIndex = Math.max(maxCellIndex, columnIndex); } @@ -169,7 +190,7 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { for (Map.Entry entry : sortedAllFiledMap.entrySet()) { Field field = entry.getValue(); String filedName = FieldUtils.resolveCglibFieldName(field); - boolean uselessData = !beanMap.containsKey(filedName) || beanMapHandledSet.contains(filedName) + boolean uselessData = !beanKeySet.contains(filedName) || beanMapHandledSet.contains(filedName) || ignoreMap.containsKey(filedName); if (uselessData) { continue; @@ -177,18 +198,23 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { Object value = beanMap.get(filedName); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(beanMap, currentWriteHolder.excelWriteHeadProperty().getHeadClazz(), filedName); - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, row, rowIndex, null, maxCellIndex, relativeRowIndex, Boolean.FALSE, excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); + // fix https://github.com/alibaba/easyexcel/issues/1870 // If there is data, it is written to the next cell - Cell cell = WorkBookUtil.createCell(row, maxCellIndex++); - WriteHandlerUtils.afterCellCreate(writeContext, cell, null, relativeRowIndex, Boolean.FALSE, - excelContentProperty); - WriteCellData> cellData = converterAndSet(currentWriteHolder, - FieldUtils.getFieldClass(beanMap, filedName, value), null, cell, value, null, null, relativeRowIndex, - rowIndex, maxCellIndex); - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, - excelContentProperty); + Cell cell = WorkBookUtil.createCell(row, maxCellIndex); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); + + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(beanMap, filedName, value)); + converterAndSet(cellWriteHandlerContext); + + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); + maxCellIndex++; } } diff --git a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java index 2fb755a7..af90d8d5 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -7,6 +7,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -24,11 +25,17 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.WriteHandlerUtils; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; import com.alibaba.excel.write.metadata.fill.AnalysisCell; import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillWrapper; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.hssf.usermodel.PoiUtils; import org.apache.poi.ss.usermodel.Cell; @@ -53,29 +60,30 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { /** * Fields to replace in the template */ - private final Map> templateAnalysisCache = MapUtils.newHashMap(); + private final Map> templateAnalysisCache = MapUtils.newHashMap(); /** * Collection fields to replace in the template */ - private final Map> templateCollectionAnalysisCache = MapUtils.newHashMap(); + private final Map> templateCollectionAnalysisCache = MapUtils.newHashMap(); /** * Style cache for collection fields */ - private final Map> collectionFieldStyleCache = MapUtils.newHashMap(); + private final Map> collectionFieldStyleCache + = MapUtils.newHashMap(); /** * Row height cache for collection */ - private final Map collectionRowHeightCache = MapUtils.newHashMap(); + private final Map collectionRowHeightCache = MapUtils.newHashMap(); /** * Last index cache for collection fields */ - private final Map> collectionLastIndexCache = MapUtils.newHashMap(); + private final Map> collectionLastIndexCache = MapUtils.newHashMap(); - private final Map relativeRowIndexMap = MapUtils.newHashMap(); + private final Map relativeRowIndexMap = MapUtils.newHashMap(); /** * The unique data encoding for this fill */ - private String currentUniqueDataFlag; + private UniqueDataFlagKey currentUniqueDataFlag; public ExcelWriteFillExecutor(WriteContext writeContext) { super(writeContext); @@ -159,15 +167,16 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { sheet.shiftRows(maxRowIndex + 1, lastRowIndex, number, true, false); // The current data is greater than unity rowindex increase - String tablePrefix = tablePrefix(currentUniqueDataFlag); - increaseRowIndex(templateAnalysisCache, number, maxRowIndex, tablePrefix); - increaseRowIndex(templateCollectionAnalysisCache, number, maxRowIndex, tablePrefix); + increaseRowIndex(templateAnalysisCache, number, maxRowIndex); + increaseRowIndex(templateCollectionAnalysisCache, number, maxRowIndex); } - private void increaseRowIndex(Map> templateAnalysisCache, int number, int maxRowIndex, - String tablePrefix) { - for (Map.Entry> entry : templateAnalysisCache.entrySet()) { - if (!tablePrefix.equals(tablePrefix(entry.getKey()))) { + private void increaseRowIndex(Map> templateAnalysisCache, int number, + int maxRowIndex) { + for (Map.Entry> entry : templateAnalysisCache.entrySet()) { + UniqueDataFlagKey uniqueDataFlagKey = entry.getKey(); + if (!Objects.equals(currentUniqueDataFlag.getSheetNo(), uniqueDataFlagKey.getSheetNo()) || !Objects.equals( + currentUniqueDataFlag.getSheetName(), uniqueDataFlagKey.getSheetName())) { continue; } for (AnalysisCell analysisCell : entry.getValue()) { @@ -183,28 +192,35 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { if (CollectionUtils.isEmpty(analysisCellList) || oneRowData == null) { return; } - Map, ?> dataMap; + Map dataMap; if (oneRowData instanceof Map) { - dataMap = (Map, ?>)oneRowData; + dataMap = (Map)oneRowData; } else { dataMap = BeanMapUtils.create(oneRowData); } - WriteSheetHolder writeSheetHolder = writeContext.writeSheetHolder(); + Set dataKeySet = new HashSet<>(dataMap.keySet()); + for (AnalysisCell analysisCell : analysisCellList) { + CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( + writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(), + relativeRowIndex, Boolean.FALSE, ExcelContentProperty.EMPTY); + if (analysisCell.getOnlyOneVariable()) { String variable = analysisCell.getVariableList().get(0); - if (!dataMap.containsKey(variable)) { + if (!dataKeySet.contains(variable)) { continue; } Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); - Cell cell = getOneCell(analysisCell, fillConfig, excelContentProperty); + cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); + + createCell(analysisCell, fillConfig, cellWriteHandlerContext); + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value)); - WriteCellData> cellData = converterAndSet(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable, value), null, cell, value, excelContentProperty, null, - relativeRowIndex, analysisCell.getRowIndex(), analysisCell.getColumnIndex()); - cellData.setAnalysisCell(analysisCell); + converterAndSet(cellWriteHandlerContext); + WriteCellData> cellData = cellWriteHandlerContext.getFirstCellData(); // Restyle if (fillConfig.getAutoStyle()) { @@ -212,28 +228,33 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .ifPresent(cellData::setOriginCellStyle); } - - WriteHandlerUtils.afterCellDispose(writeContext, cellData, cell, null, relativeRowIndex, Boolean.FALSE, - excelContentProperty); } else { StringBuilder cellValueBuild = new StringBuilder(); int index = 0; List> cellDataList = new ArrayList<>(); - Cell cell = getOneCell(analysisCell, fillConfig, ExcelContentProperty.EMPTY); + + cellWriteHandlerContext.setExcelContentProperty(ExcelContentProperty.EMPTY); + cellWriteHandlerContext.setIgnoreFillStyle(Boolean.TRUE); + + createCell(analysisCell, fillConfig, cellWriteHandlerContext); + Cell cell = cellWriteHandlerContext.getCell(); for (String variable : analysisCell.getVariableList()) { cellValueBuild.append(analysisCell.getPrepareDataList().get(index++)); - if (!dataMap.containsKey(variable)) { + if (!dataKeySet.contains(variable)) { continue; } Object value = dataMap.get(variable); ExcelContentProperty excelContentProperty = ClassUtils.declaredExcelContentProperty(dataMap, writeContext.currentWriteHolder().excelWriteHeadProperty().getHeadClazz(), variable); - WriteCellData> cellData = convert(writeSheetHolder, - FieldUtils.getFieldClass(dataMap, variable, value), CellDataTypeEnum.STRING, cell, value, - excelContentProperty); - cellData.setAnalysisCell(analysisCell); + cellWriteHandlerContext.setOriginalValue(value); + cellWriteHandlerContext.setOriginalFieldClass(FieldUtils.getFieldClass(dataMap, variable, value)); + cellWriteHandlerContext.setExcelContentProperty(excelContentProperty); + cellWriteHandlerContext.setTargetCellDataType(CellDataTypeEnum.STRING); + + WriteCellData> cellData = convert(cellWriteHandlerContext); cellDataList.add(cellData); + CellDataTypeEnum type = cellData.getType(); if (type != null) { switch (type) { @@ -253,6 +274,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } cellValueBuild.append(analysisCell.getPrepareDataList().get(index)); cell.setCellValue(cellValueBuild.toString()); + cellWriteHandlerContext.setCellDataList(cellDataList); + if (CollectionUtils.isNotEmpty(cellDataList)) { + cellWriteHandlerContext.setFirstCellData(cellDataList.get(0)); + } // Restyle if (fillConfig.getAutoStyle()) { @@ -260,10 +285,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { .map(collectionFieldStyleMap -> collectionFieldStyleMap.get(analysisCell)) .ifPresent(cell::setCellStyle); } - - WriteHandlerUtils.afterCellDispose(writeContext, cellDataList, cell, null, relativeRowIndex, - Boolean.FALSE, ExcelContentProperty.EMPTY); } + WriteHandlerUtils.afterCellDispose(cellWriteHandlerContext); } } @@ -278,11 +301,14 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return relativeRowIndex; } - private Cell getOneCell(AnalysisCell analysisCell, FillConfig fillConfig, - ExcelContentProperty excelContentProperty) { + private void createCell(AnalysisCell analysisCell, FillConfig fillConfig, + CellWriteHandlerContext cellWriteHandlerContext) { Sheet cachedSheet = writeContext.writeSheetHolder().getCachedSheet(); if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) { - return cachedSheet.getRow(analysisCell.getRowIndex()).getCell(analysisCell.getColumnIndex()); + Row row = cachedSheet.getRow(analysisCell.getRowIndex()); + cellWriteHandlerContext.setRow(row); + Cell cell = row.getCell(analysisCell.getColumnIndex()); + cellWriteHandlerContext.setCell(cell); } Sheet sheet = writeContext.writeSheetHolder().getSheet(); @@ -320,25 +346,31 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } Row row = createRowIfNecessary(sheet, cachedSheet, lastRowIndex, fillConfig, analysisCell, isOriginalCell); - Cell cell = createCellIfNecessary(row, lastColumnIndex, excelContentProperty); + cellWriteHandlerContext.setRow(row); + + cellWriteHandlerContext.setRowIndex(lastRowIndex); + cellWriteHandlerContext.setColumnIndex(lastColumnIndex); + Cell cell = createCellIfNecessary(row, lastColumnIndex, cellWriteHandlerContext); + cellWriteHandlerContext.setCell(cell); if (isOriginalCell) { Map collectionFieldStyleMap = collectionFieldStyleCache.computeIfAbsent( currentUniqueDataFlag, key -> MapUtils.newHashMap()); collectionFieldStyleMap.put(analysisCell, cell.getCellStyle()); } - return cell; } - private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, ExcelContentProperty excelContentProperty) { + private Cell createCellIfNecessary(Row row, Integer lastColumnIndex, + CellWriteHandlerContext cellWriteHandlerContext) { Cell cell = row.getCell(lastColumnIndex); if (cell != null) { return cell; } - WriteHandlerUtils.beforeCellCreate(writeContext, row, null, lastColumnIndex, null, Boolean.FALSE, - excelContentProperty); + WriteHandlerUtils.beforeCellCreate(cellWriteHandlerContext); cell = row.createCell(lastColumnIndex); - WriteHandlerUtils.afterCellCreate(writeContext, cell, null, null, Boolean.FALSE, excelContentProperty); + cellWriteHandlerContext.setCell(cell); + + WriteHandlerUtils.afterCellCreate(cellWriteHandlerContext); return cell; } @@ -351,7 +383,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } row = cachedSheet.getRow(lastRowIndex); if (row == null) { - WriteHandlerUtils.beforeRowCreate(writeContext, lastRowIndex, null, Boolean.FALSE); + RowWriteHandlerContext rowWriteHandlerContext = WriteHandlerUtils.createRowWriteHandlerContext(writeContext, + lastRowIndex, null, Boolean.FALSE); + WriteHandlerUtils.beforeRowCreate(rowWriteHandlerContext); + if (fillConfig.getForceNewRow()) { row = cachedSheet.createRow(lastRowIndex); } else { @@ -364,8 +399,10 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { row = cachedSheet.createRow(lastRowIndex); } } + rowWriteHandlerContext.setRow(row); checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); - WriteHandlerUtils.afterRowCreate(writeContext, row, null, Boolean.FALSE); + + WriteHandlerUtils.afterRowCreate(rowWriteHandlerContext); } else { checkRowHeight(analysisCell, fillConfig, isOriginalCell, row); } @@ -389,13 +426,13 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } } - private List readTemplateData(Map> analysisCache) { + private List readTemplateData(Map> analysisCache) { List analysisCellList = analysisCache.get(currentUniqueDataFlag); if (analysisCellList != null) { return analysisCellList; } Sheet sheet = writeContext.writeSheetHolder().getCachedSheet(); - Map> firstRowCache = MapUtils.newHashMapWithExpectedSize(8); + Map> firstRowCache = MapUtils.newHashMapWithExpectedSize(8); for (int i = 0; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i); if (row == null) { @@ -425,7 +462,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { * @param firstRowCache first row cache * @return Returns the data that the cell needs to replace */ - private String prepareData(Cell cell, int rowIndex, int columnIndex, Map> firstRowCache) { + private String prepareData(Cell cell, int rowIndex, int columnIndex, + Map> firstRowCache) { if (!CellType.STRING.equals(cell.getCellType())) { return null; } @@ -508,7 +546,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { } private String dealAnalysisCell(AnalysisCell analysisCell, String value, int rowIndex, int lastPrepareDataIndex, - int length, Map> firstRowCache, StringBuilder preparedData) { + int length, Map> firstRowCache, StringBuilder preparedData) { if (analysisCell != null) { if (lastPrepareDataIndex == length) { analysisCell.getPrepareDataList().add(StringUtils.EMPTY); @@ -516,7 +554,8 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { analysisCell.getPrepareDataList().add(convertPrepareData(value.substring(lastPrepareDataIndex))); analysisCell.setOnlyOneVariable(Boolean.FALSE); } - String uniqueDataFlag = uniqueDataFlag(writeContext.writeSheetHolder(), analysisCell.getPrefix()); + UniqueDataFlagKey uniqueDataFlag = uniqueDataFlag(writeContext.writeSheetHolder(), + analysisCell.getPrefix()); if (WriteTemplateAnalysisCellTypeEnum.COMMON.equals(analysisCell.getCellType())) { List analysisCellList = templateAnalysisCache.computeIfAbsent(uniqueDataFlag, key -> ListUtils.newArrayList()); @@ -560,21 +599,17 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { return prepareData; } - private String uniqueDataFlag(WriteSheetHolder writeSheetHolder, String wrapperName) { - String prefix; - if (writeSheetHolder.getSheetNo() != null) { - prefix = writeSheetHolder.getSheetNo().toString(); - } else { - prefix = writeSheetHolder.getSheetName(); - } - if (StringUtils.isEmpty(wrapperName)) { - return prefix + "-"; - } - return prefix + "-" + wrapperName; + private UniqueDataFlagKey uniqueDataFlag(WriteSheetHolder writeSheetHolder, String wrapperName) { + return new UniqueDataFlagKey(writeSheetHolder.getSheetNo(), writeSheetHolder.getSheetName(), wrapperName); } - private String tablePrefix(String uniqueDataFlag) { - return uniqueDataFlag.substring(0, uniqueDataFlag.indexOf("-") + 1); + @Getter + @Setter + @EqualsAndHashCode + @AllArgsConstructor + public static class UniqueDataFlagKey { + private Integer sheetNo; + private String sheetName; + private String wrapperName; } - } diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java new file mode 100644 index 00000000..35afe6fc --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java @@ -0,0 +1,67 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the cell handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class CellHandlerExecutionChain { + /** + * next chain + */ + private CellHandlerExecutionChain next; + /** + * handler + */ + private CellWriteHandler handler; + + public CellHandlerExecutionChain(CellWriteHandler handler) { + this.handler = handler; + } + + public void beforeCellCreate(CellWriteHandlerContext context) { + this.handler.beforeCellCreate(context); + if (this.next != null) { + this.next.beforeCellCreate(context); + } + } + + public void afterCellCreate(CellWriteHandlerContext context) { + this.handler.afterCellCreate(context); + if (this.next != null) { + this.next.afterCellCreate(context); + } + } + + public void afterCellDataConverted(CellWriteHandlerContext context) { + this.handler.afterCellDataConverted(context); + if (this.next != null) { + this.next.afterCellDataConverted(context); + } + } + + public void afterCellDispose(CellWriteHandlerContext context) { + this.handler.afterCellDispose(context); + if (this.next != null) { + this.next.afterCellDispose(context); + } + } + + public void addLast(CellWriteHandler handler) { + CellHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new CellHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java new file mode 100644 index 00000000..4f32f3b1 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java @@ -0,0 +1,60 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.RowWriteHandler; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the row handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class RowHandlerExecutionChain { + /** + * next chain + */ + private RowHandlerExecutionChain next; + /** + * handler + */ + private RowWriteHandler handler; + + public RowHandlerExecutionChain(RowWriteHandler handler) { + this.handler = handler; + } + + public void beforeRowCreate(RowWriteHandlerContext context) { + this.handler.beforeRowCreate(context); + if (this.next != null) { + this.next.beforeRowCreate(context); + } + } + + public void afterRowCreate(RowWriteHandlerContext context) { + this.handler.afterRowCreate(context); + if (this.next != null) { + this.next.afterRowCreate(context); + } + } + + public void afterRowDispose(RowWriteHandlerContext context) { + this.handler.afterRowDispose(context); + if (this.next != null) { + this.next.afterRowDispose(context); + } + } + + public void addLast(RowWriteHandler handler) { + RowHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new RowHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java new file mode 100644 index 00000000..7e079dd2 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java @@ -0,0 +1,52 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.SheetWriteHandler; +import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the sheet handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class SheetHandlerExecutionChain { + /** + * next chain + */ + private SheetHandlerExecutionChain next; + /** + * handler + */ + private SheetWriteHandler handler; + + public SheetHandlerExecutionChain(SheetWriteHandler handler) { + this.handler = handler; + } + + public void beforeSheetCreate(SheetWriteHandlerContext context) { + this.handler.beforeSheetCreate(context); + if (this.next != null) { + this.next.beforeSheetCreate(context); + } + } + + public void afterSheetCreate(SheetWriteHandlerContext context) { + this.handler.afterSheetCreate(context); + if (this.next != null) { + this.next.afterSheetCreate(context); + } + } + public void addLast(SheetWriteHandler handler) { + SheetHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new SheetHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java b/src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java new file mode 100644 index 00000000..56e746e0 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java @@ -0,0 +1,61 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.WorkbookWriteHandler; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +/** + * Execute the workbook handler chain + * + * @author Jiaju Zhuang + */ +@Getter +@Setter +@EqualsAndHashCode +public class WorkbookHandlerExecutionChain { + /** + * next chain + */ + private WorkbookHandlerExecutionChain next; + + /** + * handler + */ + private WorkbookWriteHandler handler; + + public WorkbookHandlerExecutionChain(WorkbookWriteHandler handler) { + this.handler = handler; + } + + public void beforeWorkbookCreate(WorkbookWriteHandlerContext context) { + this.handler.beforeWorkbookCreate(context); + if (this.next != null) { + this.next.beforeWorkbookCreate(context); + } + } + + public void afterWorkbookCreate(WorkbookWriteHandlerContext context) { + this.handler.afterWorkbookCreate(context); + if (this.next != null) { + this.next.afterWorkbookCreate(context); + } + } + + public void afterWorkbookDispose(WorkbookWriteHandlerContext context) { + this.handler.afterWorkbookDispose(context); + if (this.next != null) { + this.next.afterWorkbookDispose(context); + } + } + + public void addLast(WorkbookWriteHandler handler) { + WorkbookHandlerExecutionChain context = this; + while (context.next != null) { + context = context.next; + } + context.next = new WorkbookHandlerExecutionChain(handler); + } +} diff --git a/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java index 27530f6f..0007e7ac 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/CellWriteHandlerContext.java @@ -3,15 +3,18 @@ package com.alibaba.excel.write.handler.context; import java.util.List; import com.alibaba.excel.context.WriteContext; +import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; -import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; @@ -20,8 +23,9 @@ import org.apache.poi.ss.usermodel.Row; * * @author Jiaju Zhuang */ -@Data -@AllArgsConstructor +@Getter +@Setter +@EqualsAndHashCode public class CellWriteHandlerContext { /** * write context @@ -43,6 +47,10 @@ public class CellWriteHandlerContext { * row */ private Row row; + /** + * index + */ + private Integer rowIndex; /** * cell */ @@ -78,4 +86,47 @@ public class CellWriteHandlerContext { * Field annotation configuration information. */ private ExcelContentProperty excelContentProperty; + + /** + * The value of the original + */ + private Object originalValue; + + /** + * The original field type + */ + private Class> originalFieldClass; + + /** + * Target cell data type + */ + private CellDataTypeEnum targetCellDataType; + + /** + * Ignore the filling pattern and the {@code FillStyleCellWriteHandler} will not work. + * + * @see FillStyleCellWriteHandler + */ + private Boolean ignoreFillStyle; + + public CellWriteHandlerContext(WriteContext writeContext, + WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder, + WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Cell cell, Integer columnIndex, + Integer relativeRowIndex, Head headData, List> cellDataList, WriteCellData> firstCellData, + Boolean head, ExcelContentProperty excelContentProperty) { + 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; + } } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java index 6578d3a2..c01c8b5b 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java @@ -6,7 +6,9 @@ import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.Row; /** @@ -14,7 +16,9 @@ import org.apache.poi.ss.usermodel.Row; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor public class RowWriteHandlerContext { /** diff --git a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java index c1164eb0..f3c024f4 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java @@ -5,14 +5,18 @@ import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * sheet context * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor public class SheetWriteHandlerContext { /** diff --git a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java index f955d4bf..a7ec289c 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java @@ -4,14 +4,18 @@ import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * workbook context * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor public class WorkbookWriteHandlerContext { /** diff --git a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java index c8116b4d..d3680e24 100644 --- a/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/impl/FillStyleCellWriteHandler.java @@ -1,16 +1,14 @@ package com.alibaba.excel.write.handler.impl; -import java.util.List; - import com.alibaba.excel.constant.OrderConstant; import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.collections4.CollectionUtils; import org.apache.poi.ss.usermodel.CellStyle; /** @@ -28,12 +26,12 @@ public class FillStyleCellWriteHandler implements CellWriteHandler { @Override public void afterCellDispose(CellWriteHandlerContext context) { - List> cellDataList = context.getCellDataList(); - if (CollectionUtils.size(cellDataList) != 1) { + if (BooleanUtils.isTrue(context.getIgnoreFillStyle())) { return; } + WriteCellData> cellData = context.getFirstCellData(); - if (cellData.getAnalysisCell() != null && !cellData.getAnalysisCell().getOnlyOneVariable()) { + if (cellData == null) { return; } WriteCellStyle writeCellStyle = cellData.getWriteCellStyle(); diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java b/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java index 91527cc1..b0745d38 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java @@ -7,14 +7,18 @@ import java.util.List; import com.alibaba.excel.metadata.BasicParameter; import com.alibaba.excel.write.handler.WriteHandler; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Write basic parameter * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteBasicParameter extends BasicParameter { /** * Writes the head relative to the existing contents of the sheet. Indexes are zero-based. diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java index 04bfb822..1f41376c 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java @@ -1,13 +1,17 @@ package com.alibaba.excel.write.metadata; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Write sheet * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteSheet extends WriteBasicParameter { /** * Starting from 0 diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java b/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java index a935f326..6681e1b8 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java @@ -1,13 +1,17 @@ package com.alibaba.excel.write.metadata; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * table * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteTable extends WriteBasicParameter { /** * Starting from 0 diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java index bcc39211..38b368ec 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java @@ -7,14 +7,18 @@ import java.nio.charset.Charset; import com.alibaba.excel.support.ExcelTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Workbook * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteWorkbook extends WriteBasicParameter { /** * Excel type.The default is xlsx diff --git a/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java b/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java index b9b377ed..783c7d89 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java +++ b/src/main/java/com/alibaba/excel/write/metadata/fill/AnalysisCell.java @@ -4,14 +4,18 @@ import java.util.List; import com.alibaba.excel.enums.WriteTemplateAnalysisCellTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Read the cells of the template while populating the data. * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class AnalysisCell { private int columnIndex; private int rowIndex; diff --git a/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java b/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java index 8fdb240a..ac5581ee 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java +++ b/src/main/java/com/alibaba/excel/write/metadata/fill/FillConfig.java @@ -4,15 +4,19 @@ import com.alibaba.excel.enums.WriteDirectionEnum; import lombok.AllArgsConstructor; import lombok.Builder; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; /** * Fill config * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode @Builder @NoArgsConstructor @AllArgsConstructor diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index 5059894a..0b546703 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -15,7 +15,6 @@ import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.event.NotRepeatExecutor; -import com.alibaba.excel.event.Order; import com.alibaba.excel.metadata.AbstractHolder; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.property.ExcelContentProperty; @@ -28,6 +27,10 @@ import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.WriteHandler; +import com.alibaba.excel.write.handler.chain.CellHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.RowHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.SheetHandlerExecutionChain; +import com.alibaba.excel.write.handler.chain.WorkbookHandlerExecutionChain; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; @@ -38,8 +41,10 @@ import com.alibaba.excel.write.style.AbstractVerticalCellStyleStrategy; import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; /** @@ -47,7 +52,9 @@ import org.apache.commons.collections4.CollectionUtils; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public abstract class AbstractWriteHolder extends AbstractHolder implements WriteHolder { /** @@ -62,15 +69,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ * Excel head property */ private ExcelWriteHeadProperty excelWriteHeadProperty; - /** - * Write handler - */ - private Map, List> writeHandlerMap; - /** - * Own write handler.Created in the sheet in the workbook interceptors will not be executed because the workbook to - * create an event long past. So when initializing sheet, supplementary workbook event. - */ - private Map, List> ownWriteHandlerMap; /** * Use the default style.Default is true. */ @@ -97,6 +95,43 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ */ private Collection includeColumnFieldNames; + /** + * Write handler + */ + private List writeHandlerList; + + /** + * Execute the workbook handler chain + * Created in the sheet in the workbook interceptors will not be executed because the workbook to + * create an event long past. So when initializing sheet, supplementary workbook event. + */ + public WorkbookHandlerExecutionChain ownWorkbookHandlerExecutionChain; + /** + * Execute the sheet handler chain + * Created in the sheet in the workbook interceptors will not be executed because the workbook to + * create an event long past. So when initializing sheet, supplementary workbook event. + */ + public SheetHandlerExecutionChain ownSheetHandlerExecutionChain; + + /** + * Execute the workbook handler chain + */ + public WorkbookHandlerExecutionChain workbookHandlerExecutionChain; + /** + * Execute the sheet handler chain + */ + public SheetHandlerExecutionChain sheetHandlerExecutionChain; + + /** + * Execute the row handler chain + */ + public RowHandlerExecutionChain rowHandlerExecutionChain; + + /** + * Execute the cell handler chain + */ + public CellHandlerExecutionChain cellHandlerExecutionChain; + public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder) { super(writeBasicParameter, parentAbstractWriteHolder); @@ -169,7 +204,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ this.excelWriteHeadProperty = new ExcelWriteHeadProperty(this, getClazz(), getHead()); // Set writeHandlerMap - List handlerList = new ArrayList(); + List handlerList = new ArrayList<>(); // Initialization Annotation initAnnotationConfig(handlerList, writeBasicParameter); @@ -178,16 +213,16 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ && !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) { handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList()); } + sortAndClearUpHandler(handlerList, true); - this.ownWriteHandlerMap = sortAndClearUpHandler(handlerList); - - Map, List> parentWriteHandlerMap = null; if (parentAbstractWriteHolder != null) { - parentWriteHandlerMap = parentAbstractWriteHolder.getWriteHandlerMap(); + if (CollectionUtils.isNotEmpty(parentAbstractWriteHolder.getWriteHandlerList())) { + handlerList.addAll(parentAbstractWriteHolder.getWriteHandlerList()); + } } else { handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler(useDefaultStyle)); } - this.writeHandlerMap = sortAndClearUpAllHandler(handlerList, parentWriteHandlerMap); + sortAndClearUpHandler(handlerList, false); // Set converterMap if (parentAbstractWriteHolder == null) { @@ -212,15 +247,11 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } Map headMap = getExcelWriteHeadProperty().getHeadMap(); boolean hasColumnWidth = false; - boolean hasStyle = false; for (Head head : headMap.values()) { if (head.getColumnWidthProperty() != null) { hasColumnWidth = true; } - if (head.getHeadStyleProperty() != null || head.getHeadFontProperty() != null) { - hasStyle = true; - } dealLoopMerge(handlerList, head); } @@ -228,10 +259,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ dealColumnWidth(handlerList); } - //if (hasStyle) { - dealStyle(handlerList); - //} - + dealStyle(handlerList); dealRowHigh(handlerList); dealOnceAbsoluteMerge(handlerList); } @@ -310,38 +338,22 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ handlerList.add(columnWidthStyleStrategy); } - protected Map, List> sortAndClearUpAllHandler( - List handlerList, Map, List> parentHandlerMap) { - // add - if (parentHandlerMap != null) { - List parentWriteHandler = parentHandlerMap.get(WriteHandler.class); - if (!CollectionUtils.isEmpty(parentWriteHandler)) { - handlerList.addAll(parentWriteHandler); - } - } - return sortAndClearUpHandler(handlerList); - } - - protected Map, List> sortAndClearUpHandler( - List handlerList) { + protected void sortAndClearUpHandler(List handlerList, boolean runOwn) { // sort - Map> orderExcelWriteHandlerMap = new TreeMap>(); + Map> orderExcelWriteHandlerMap = new TreeMap<>(); for (WriteHandler handler : handlerList) { - int order = Integer.MIN_VALUE; - if (handler instanceof Order) { - order = ((Order)handler).order(); - } + int order = handler.order(); if (orderExcelWriteHandlerMap.containsKey(order)) { orderExcelWriteHandlerMap.get(order).add(handler); } else { - List tempHandlerList = new ArrayList(); + List tempHandlerList = new ArrayList<>(); tempHandlerList.add(handler); orderExcelWriteHandlerMap.put(order, tempHandlerList); } } // clean up - Set alreadyExistedHandlerSet = new HashSet(); - List cleanUpHandlerList = new ArrayList(); + Set alreadyExistedHandlerSet = new HashSet<>(); + List cleanUpHandlerList = new ArrayList<>(); for (Map.Entry> entry : orderExcelWriteHandlerMap.entrySet()) { for (WriteHandler handler : entry.getValue()) { if (handler instanceof NotRepeatExecutor) { @@ -354,30 +366,70 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ cleanUpHandlerList.add(handler); } } - // classify - Map, List> result = - new HashMap, List>(16); - result.put(WriteHandler.class, new ArrayList()); - result.put(WorkbookWriteHandler.class, new ArrayList()); - result.put(SheetWriteHandler.class, new ArrayList()); - result.put(RowWriteHandler.class, new ArrayList()); - result.put(CellWriteHandler.class, new ArrayList()); + + // build chain + if (!runOwn) { + this.writeHandlerList = new ArrayList<>(); + } for (WriteHandler writeHandler : cleanUpHandlerList) { - if (writeHandler instanceof CellWriteHandler) { - result.get(CellWriteHandler.class).add(writeHandler); + buildChain(writeHandler, runOwn); + } + } + + protected void buildChain(WriteHandler writeHandler, boolean runOwn) { + if (writeHandler instanceof CellWriteHandler) { + if (!runOwn) { + if (cellHandlerExecutionChain == null) { + cellHandlerExecutionChain = new CellHandlerExecutionChain((CellWriteHandler)writeHandler); + } else { + cellHandlerExecutionChain.addLast((CellWriteHandler)writeHandler); + } } - if (writeHandler instanceof RowWriteHandler) { - result.get(RowWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof RowWriteHandler) { + if (!runOwn) { + if (rowHandlerExecutionChain == null) { + rowHandlerExecutionChain = new RowHandlerExecutionChain((RowWriteHandler)writeHandler); + } else { + rowHandlerExecutionChain.addLast((RowWriteHandler)writeHandler); + } } - if (writeHandler instanceof SheetWriteHandler) { - result.get(SheetWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof SheetWriteHandler) { + if (!runOwn) { + if (sheetHandlerExecutionChain == null) { + sheetHandlerExecutionChain = new SheetHandlerExecutionChain((SheetWriteHandler)writeHandler); + } else { + sheetHandlerExecutionChain.addLast((SheetWriteHandler)writeHandler); + } + } else { + if (ownSheetHandlerExecutionChain == null) { + ownSheetHandlerExecutionChain = new SheetHandlerExecutionChain((SheetWriteHandler)writeHandler); + } else { + ownSheetHandlerExecutionChain.addLast((SheetWriteHandler)writeHandler); + } } - if (writeHandler instanceof WorkbookWriteHandler) { - result.get(WorkbookWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof WorkbookWriteHandler) { + if (!runOwn) { + if (workbookHandlerExecutionChain == null) { + workbookHandlerExecutionChain = new WorkbookHandlerExecutionChain( + (WorkbookWriteHandler)writeHandler); + } else { + workbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler); + } + } else { + if (ownWorkbookHandlerExecutionChain == null) { + ownWorkbookHandlerExecutionChain = new WorkbookHandlerExecutionChain( + (WorkbookWriteHandler)writeHandler); + } else { + ownWorkbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler); + } } - result.get(WriteHandler.class).add(writeHandler); } - return result; + if (!runOwn) { + this.writeHandlerList.add(writeHandler); + } } @Override @@ -406,16 +458,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ return getExcelWriteHeadProperty(); } - @Override - public Map, List> writeHandlerMap() { - return getWriteHandlerMap(); - } - - @Override - public Map, List> ownWriteHandlerMap() { - return getOwnWriteHandlerMap(); - } - @Override public boolean needHead() { return getNeedHead(); @@ -430,4 +472,5 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ public boolean automaticMergeHead() { return getAutomaticMergeHead(); } + } diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java index 4896b034..77f3c14f 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java @@ -1,10 +1,6 @@ package com.alibaba.excel.write.metadata.holder; -import java.util.List; -import java.util.Map; - import com.alibaba.excel.metadata.ConfigurationHolder; -import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.property.ExcelWriteHeadProperty; /** @@ -21,20 +17,6 @@ public interface WriteHolder extends ConfigurationHolder { */ ExcelWriteHeadProperty excelWriteHeadProperty(); - /** - * What handler does the currently operated cell need to execute - * - * @return - */ - Map, List> writeHandlerMap(); - - /** - * create your own write handler. - * - * @return - */ - Map, List> ownWriteHandlerMap(); - /** * Is to determine if a field needs to be ignored * diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java index 580eeb97..ffc6f759 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java @@ -8,7 +8,7 @@ import com.alibaba.excel.enums.WriteLastRowTypeEnum; import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.write.metadata.WriteSheet; -import lombok.Data; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -22,7 +22,9 @@ import org.apache.poi.xssf.usermodel.XSSFSheet; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @NoArgsConstructor public class WriteSheetHolder extends AbstractWriteHolder { /** diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java index e353a44c..3a46e784 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java @@ -3,14 +3,18 @@ package com.alibaba.excel.write.metadata.holder; import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.write.metadata.WriteTable; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * sheet holder * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteTableHolder extends AbstractWriteHolder { /*** * poi sheet diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index 374b5b7a..1353ddc4 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -19,11 +19,15 @@ import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.IoUtils; import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.StyleUtil; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString.Exclude; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -39,7 +43,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @Slf4j public class WriteWorkbookHolder extends AbstractWriteHolder { /*** @@ -143,6 +149,12 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { */ private Map dataFormatMap; + /** + * handler context + */ + @Exclude + private WorkbookWriteHandlerContext workbookWriteHandlerContext; + public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { super(writeWorkbook, null); this.writeWorkbook = writeWorkbook; @@ -253,8 +265,6 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { if (writeCellStyle == null) { return originCellStyle; } - WriteCellStyle tempWriteCellStyle = new WriteCellStyle(); - WriteCellStyle.merge(writeCellStyle, tempWriteCellStyle); short styleIndex = -1; Font originFont = null; @@ -271,14 +281,17 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { Map cellStyleMap = cellStyleIndexMap.computeIfAbsent(styleIndex, key -> MapUtils.newHashMap()); - CellStyle cellStyle = cellStyleMap.get(tempWriteCellStyle); + CellStyle cellStyle = cellStyleMap.get(writeCellStyle); if (cellStyle != null) { return cellStyle; } if (log.isDebugEnabled()) { - log.info("create new style:{},{}", tempWriteCellStyle, originCellStyle); + log.info("create new style:{},{}", writeCellStyle, originCellStyle); } - cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, writeCellStyle); + WriteCellStyle tempWriteCellStyle = new WriteCellStyle(); + WriteCellStyle.merge(writeCellStyle, tempWriteCellStyle); + + cellStyle = StyleUtil.buildCellStyle(workbook, originCellStyle, tempWriteCellStyle); Short dataFormat = createDataFormat(tempWriteCellStyle.getDataFormatData(), useCache); if (dataFormat != null) { cellStyle.setDataFormat(dataFormat); diff --git a/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java b/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java index 9298f9da..bebd210e 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java +++ b/src/main/java/com/alibaba/excel/write/metadata/style/WriteCellStyle.java @@ -6,7 +6,9 @@ import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.StyleProperty; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.HorizontalAlignment; @@ -19,7 +21,9 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteCellStyle { /** * Set the data format (must be a valid format). Built in formats are defined at {@link BuiltinFormats}. diff --git a/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java b/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java index d1f42f01..a42899e1 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java +++ b/src/main/java/com/alibaba/excel/write/metadata/style/WriteFont.java @@ -2,7 +2,9 @@ package com.alibaba.excel.write.metadata.style; import com.alibaba.excel.util.StringUtils; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.common.usermodel.fonts.FontCharset; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.ss.usermodel.Font; @@ -13,7 +15,9 @@ import org.apache.poi.ss.usermodel.IndexedColors; * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteFont { /** * The name for the font (i.e. Arial) diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java index 7f52cd1d..4c11010a 100644 --- a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentLoopMerge; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadFontStyle; @@ -17,6 +18,7 @@ import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.metadata.CellRange; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Holder; +import com.alibaba.excel.metadata.property.ColumnWidthProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.FontProperty; import com.alibaba.excel.metadata.property.LoopMergeProperty; @@ -24,14 +26,18 @@ import com.alibaba.excel.metadata.property.OnceAbsoluteMergeProperty; import com.alibaba.excel.metadata.property.RowHeightProperty; import com.alibaba.excel.metadata.property.StyleProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * Define the header attribute of excel * * @author jipengfei */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcelWriteHeadProperty extends ExcelHeadProperty { private RowHeightProperty headRowHeightProperty; @@ -50,9 +56,11 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { this.onceAbsoluteMergeProperty = OnceAbsoluteMergeProperty.build(headClazz.getAnnotation(OnceAbsoluteMerge.class)); + ColumnWidth parentColumnWidth = headClazz.getAnnotation(ColumnWidth.class); HeadStyle parentHeadStyle = headClazz.getAnnotation(HeadStyle.class); HeadFontStyle parentHeadFontStyle = headClazz.getAnnotation(HeadFontStyle.class); + for (Map.Entry entry : getHeadMap().entrySet()) { Head headData = entry.getValue(); if (headData == null) { @@ -61,6 +69,13 @@ public class ExcelWriteHeadProperty extends ExcelHeadProperty { } Field field = headData.getField(); + ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class); + if (columnWidth == null) { + columnWidth = parentColumnWidth; + } + headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth)); + + HeadStyle headStyle = field.getAnnotation(HeadStyle.class); if (headStyle == null) { headStyle = parentHeadStyle; diff --git a/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java index 8f033710..df04d248 100644 --- a/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/HorizontalCellStyleStrategy.java @@ -7,7 +7,9 @@ import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; import com.alibaba.excel.write.metadata.style.WriteCellStyle; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; /** @@ -15,7 +17,9 @@ import org.apache.commons.collections4.CollectionUtils; * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy { private WriteCellStyle headWriteCellStyle; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java index d1c2a4f7..0ed28a29 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationData.java @@ -10,22 +10,28 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data -@ColumnWidth(30) -@HeadRowHeight(15) -@ContentRowHeight(20) +@Getter +@Setter +@EqualsAndHashCode +@ColumnWidth(50) +@HeadRowHeight(50) +@ContentRowHeight(100) public class AnnotationData { @ExcelProperty("日期") @DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒") private Date date; + @ExcelProperty(value = "数字") @NumberFormat("#.##%") private Double number; + @ExcelIgnore private String ignore; private static final String staticFinal = "test"; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java index 7a4dfe21..02bd3159 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java @@ -8,6 +8,11 @@ import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.util.DateUtils; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -46,6 +51,20 @@ public class AnnotationDataTest { EasyExcel.write().file(file).head(AnnotationData.class).sheet().doWrite(data()); EasyExcel.read().file(file).head(AnnotationData.class).registerReadListener(new AnnotationDataListener()) .sheet().doRead(); + + if (file == fileCsv) { + return; + } + + Workbook workbook = WorkbookFactory.create(file); + Sheet sheet = workbook.getSheetAt(0); + Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); + + Row row0 = sheet.getRow(0); + Assert.assertEquals(1000, row0.getHeight(), 0); + + Row row1 = sheet.getRow(1); + Assert.assertEquals(2000, row1.getHeight(), 0); } private List data() throws Exception { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java index b41f093d..fa93f2ed 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.annotation; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class AnnotationIndexAndNameData { @ExcelProperty(value = "第四个", index = 4) private String index4; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java index b8322073..ad50e4a0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataReadData.java @@ -3,12 +3,16 @@ package com.alibaba.easyexcel.test.core.celldata; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.metadata.data.ReadCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellDataReadData { @DateTimeFormat("yyyy年MM月dd日") private ReadCellData date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java index 69ed7bbf..cf10b0d7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataWriteData.java @@ -5,12 +5,16 @@ import java.util.Date; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellDataWriteData { @DateTimeFormat("yyyy年MM月dd日") private WriteCellData date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java index 8fa78f7e..f9a6bee6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java @@ -8,12 +8,16 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.data.ReadCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterReadData { @ExcelProperty("日期") private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java index 68c34423..6782aa1a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java @@ -8,12 +8,16 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterWriteData { @ExcelProperty("日期") private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java index f8d6ac21..fb31e127 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ImageData.java @@ -8,12 +8,16 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.converters.string.StringImageConverter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(500) @ColumnWidth(500 / 8) public class ImageData { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java index cccee710..dac73127 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java @@ -5,12 +5,16 @@ import java.math.BigInteger; import java.time.LocalDateTime; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ReadAllConverterData { private BigDecimal bigDecimalBoolean; private BigDecimal bigDecimalNumber; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java b/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java index 1930bce5..5958161c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.dataformat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DateFormatData { private String date; private String dateStringCn; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java index 19ef8d03..6cba3a71 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.encrypt; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class EncryptData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java index 28d273e4..c989ce99 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.exception; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExceptionData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java b/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java index 7c49b01d..737bd61b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/excludeorinclude/ExcludeOrIncludeData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.excludeorinclude; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExcludeOrIncludeData { @ExcelProperty(order = 1) private String column1; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java b/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java index 61822fa8..0424509e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.extra; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExtraData { private String row1; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java index a1dd56a3..9a33351c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/FillData.java @@ -4,12 +4,16 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.NumberFormat; import com.alibaba.excel.converters.doubleconverter.DoubleStringConverter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData { private String name; @NumberFormat("#") diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java index 52b1cc04..c568e027 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleAnnotatedData.java @@ -7,12 +7,16 @@ import com.alibaba.excel.annotation.write.style.ContentStyle; import com.alibaba.excel.enums.BooleanEnum; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillStyleAnnotatedData { @ContentStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 13) @ContentFontStyle(bold = BooleanEnum.TRUE, color = 19) diff --git a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java index 98c29fb2..20af16a3 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/fill/style/FillStyleData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.fill.style; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillStyleData { private String name; private Double number; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java index ad2188aa..ec81e596 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/handler/WriteHandlerData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.handler; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteHandlerData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java index 30e7e814..38eb2b8a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.head; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ComplexHeadData { @ExcelProperty({"顶格", "顶格", "两格"}) private String string0; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java index 7e19af15..a45bcf9c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.head; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class NoHeadData { @ExcelProperty("字符串") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java index 246f9cbd..1bd5a748 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.large; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class LargeData { private String str1; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index 3da8500d..a438a2ee 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -32,6 +32,7 @@ public class LargeDataTest { private static File template07; private static File fileCsv; private static File fileWrite07; + private static File fileWriteTemp07; private static File fileWritePoi07; private int i = 0; @@ -40,13 +41,14 @@ public class LargeDataTest { public static void init() { fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx"); + fileWriteTemp07 = TestFileUtil.createNewFile("large" + File.separator + "fileWriteTemp07.xlsx"); fileWritePoi07 = TestFileUtil.createNewFile("large" + File.separator + "fileWritePoi07.xlsx"); template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx"); fileCsv = TestFileUtil.createNewFile("largefileCsv.csv"); } @Test - public void t01Read() { + public void t01Read() throws Exception { long start = System.currentTimeMillis(); EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class, new LargeDataListener()).headRowNumber(2).sheet().doRead(); @@ -85,9 +87,16 @@ public class LargeDataTest { @Test public void t04Write() throws Exception { - long start = System.currentTimeMillis(); - ExcelWriter excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build(); + ExcelWriter excelWriter = EasyExcel.write(fileWriteTemp07, LargeData.class).build(); WriteSheet writeSheet = EasyExcel.writerSheet().build(); + for (int j = 0; j < 2; j++) { + excelWriter.write(data(), writeSheet); + } + excelWriter.finish(); + + long start = System.currentTimeMillis(); + excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build(); + writeSheet = EasyExcel.writerSheet().build(); for (int j = 0; j < 100; j++) { excelWriter.write(data(), writeSheet); LOGGER.info("{} write success.", j); @@ -115,7 +124,8 @@ public class LargeDataTest { } long costPoi = System.currentTimeMillis() - start; LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start); - Assert.assertTrue(costPoi * 3 > cost); + LOGGER.info("{} vs {}", cost, costPoi); + Assert.assertTrue(costPoi * 2 > cost); } private List data() { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java b/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java index 73da1c31..22b00e19 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.multiplesheets; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class MultipleSheetsData { private String title; } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java b/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java index dd010fc7..286ef8fe 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.core.noncamel; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class UnCamelData { private String string1; private String String2; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java b/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java index 7e22a2e7..fedabaa0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.parameter; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ParameterData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java b/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java index 0a7378f2..e77a995e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.repetition; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class RepetitionData { @ExcelProperty("字符串") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java index 6c541a24..0e43b3c8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.simple; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class SimpleData { @ExcelProperty("姓名") private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java index 1476448b..8e0d55ee 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.skip; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class SkipData { @ExcelProperty("姓名") diff --git a/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java b/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java index 870abe87..668dd899 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/sort/SortData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.sort; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class SortData { private String column5; private String column6; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java index cc30893e..b6b67c9d 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleData.java @@ -4,12 +4,16 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.annotation.write.style.HeadStyle; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @HeadStyle @HeadFontStyle public class StyleData { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java index a48c3238..e9323761 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java @@ -209,7 +209,10 @@ public class StyleDataTest { Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.getSheetAt(0); + Assert.assertEquals(50 * 256, sheet.getColumnWidth(0), 0); + Row row0 = sheet.getRow(0); + Assert.assertEquals(800, row0.getHeight(), 0); Cell cell00 = row0.getCell(0); Assert.assertArrayEquals(new byte[] {-1, -1, 0}, StyleTestUtils.getFillForegroundColor(cell00)); Assert.assertArrayEquals(new byte[] {-128, -128, 0}, StyleTestUtils.getFontColor(cell00, workbook)); @@ -221,6 +224,7 @@ public class StyleDataTest { Assert.assertEquals(20, StyleTestUtils.getFontHeightInPoints(cell01, workbook)); Row row1 = sheet.getRow(1); + Assert.assertEquals(1000, row1.getHeight(), 0); Cell cell10 = row1.getCell(0); Assert.assertArrayEquals(new byte[] {0, -128, -128}, StyleTestUtils.getFillForegroundColor(cell10)); Assert.assertArrayEquals(new byte[] {0, 51, 102}, StyleTestUtils.getFontColor(cell10, workbook)); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java b/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java index 31778a21..02d01215 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.core.template; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class TemplateData { @ExcelProperty("字符串0") private String string0; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java b/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java index 589f838c..6eb34ada 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.demo.fill; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData { private String name; private double number; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java index 357dbfe9..b29c39bf 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataReadDemoData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.metadata.data.CellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CellDataReadDemoData { private CellData string; // 这里注意 虽然是日期 但是 类型 存储的是number 因为excel 存储的就是number diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java index bc27e1f8..2e87616f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java @@ -4,14 +4,18 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.NumberFormat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterData { /** * 我自定义 转换器,不管数据库传过来什么 。我给他加上“自定义:” diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java index a7a7519b..6c3321d2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.read; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData { private String string; private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java index e780bfa7..b0d9a03c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraData.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.demo.read; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoExtraData { private String row1; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java index 8040728d..b202311d 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/ExceptionDemoData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.read; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ExceptionDemoData { /** * 用日期去接字符串 肯定报错 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java index 1521b8cd..a545ffa8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class IndexOrNameData { /** * 强制读取第三个 这里不建议 index 和 name 同时用,要么一个对象只用index,要么一个对象只用name去匹配 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java index 4fa3a780..501ecc78 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/DownloadData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DownloadData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java index 80a0e658..2993969a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.web; import java.util.Date; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class UploadData { private String string; private Date date; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java index 44ce5cc7..74a157d1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 复杂头数据.这里最终效果是第一行就一个主标题,第二行分类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ComplexHeadData { @ExcelProperty({"主标题", "字符串标题"}) private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java index 864cfcbf..c120e47a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ConverterData.java @@ -6,14 +6,18 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.NumberFormat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class ConverterData { /** * 我想所有的 字符串起前面加上"自定义:"三个字 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java index 8a7902b5..76b50584 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java @@ -3,17 +3,20 @@ package com.alibaba.easyexcel.test.demo.write; import java.util.Date; import com.alibaba.excel.annotation.ExcelIgnore; -import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java index ad94321e..44279db6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoMergeData.java @@ -5,14 +5,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ContentLoopMerge; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 样式的数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode // 将第6-7行的2-3列合并成一个单元格 // @OnceAbsoluteMerge(firstRowIndex = 5, lastRowIndex = 6, firstColumnIndex = 1, lastColumnIndex = 2) public class DemoMergeData { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java index 6dc60ed1..eded587b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoStyleData.java @@ -9,14 +9,18 @@ import com.alibaba.excel.annotation.write.style.HeadFontStyle; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 样式的数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode // 头背景设置成红色 IndexedColors.RED.getIndex() @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 10) // 头字体设置成20 diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java index a7bd7caa..4c39ffb1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDataWithAnnotation.java @@ -9,13 +9,17 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.converters.string.StringImageConverter; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 图片导出类 * */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(100) @ColumnWidth(100 / 8) public class ImageDataWithAnnotation { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java index ba80d6f1..9f1ebca3 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ImageDemoData.java @@ -10,14 +10,18 @@ import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.converters.string.StringImageConverter; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 图片导出类 * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(100) @ColumnWidth(100 / 8) public class ImageDemoData { diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java index 81c2d4e1..47c8c3b4 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/IndexData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class IndexData { @ExcelProperty(value = "字符串标题", index = 0) private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java index 69ad51dc..645be062 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/LongestMatchColumnWidthData.java @@ -4,14 +4,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class LongestMatchColumnWidthData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java index 42a7f883..a3a9b7f2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WidthAndHeightData.java @@ -7,14 +7,18 @@ import com.alibaba.excel.annotation.write.style.ColumnWidth; import com.alibaba.excel.annotation.write.style.ContentRowHeight; import com.alibaba.excel.annotation.write.style.HeadRowHeight; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(10) @HeadRowHeight(20) @ColumnWidth(25) diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java index 2dfed31d..0cd50ddd 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteCellDemoData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.demo.write; import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 根据WriteCellData写 * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteCellDemoData { /** * 超链接 diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java b/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java index 9c3f39e4..86b9f4f7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/CamlData.java @@ -1,13 +1,17 @@ package com.alibaba.easyexcel.test.temp; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author 是仪 */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CamlData { private String string1; private String String2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 3082362a..8c9badfe 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java @@ -8,6 +8,7 @@ import java.util.List; import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; @@ -147,4 +148,20 @@ public class Lock2Test { Thread.sleep(500 * 1000); } + @Test + public void test335() throws Exception { + + + LOGGER.info("reslut:{}", PositionUtils.getCol("A10",null)); + LOGGER.info("reslut:{}", PositionUtils.getRow("A10")); + LOGGER.info("reslut:{}", PositionUtils.getCol("AB10",null)); + LOGGER.info("reslut:{}", PositionUtils.getRow("AB10")); + + //LOGGER.info("reslut:{}", PositionUtils2.getCol("A10",null)); + //LOGGER.info("reslut:{}", PositionUtils2.getRow("A10")); + //LOGGER.info("reslut:{}", PositionUtils2.getCol("AB10",null)); + //LOGGER.info("reslut:{}", PositionUtils2.getRow("AB10")); + } + + } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java b/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java index 14fa65e3..88195cf9 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/LockData.java @@ -2,14 +2,18 @@ package com.alibaba.easyexcel.test.temp; import com.alibaba.excel.annotation.format.NumberFormat; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 基础数据类.这里的排序和excel里面的排序一致 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class LockData { @NumberFormat("#.##%") private Double string0; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java b/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java index 89214922..89453e5e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/StyleData.java @@ -2,13 +2,17 @@ package com.alibaba.easyexcel.test.temp; import java.util.List; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class StyleData { private byte[] byteValue; private Byte[] byteValue2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java b/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java index 55984a73..dbbb7058 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/TempFillData.java @@ -2,12 +2,16 @@ package com.alibaba.easyexcel.test.temp; import com.alibaba.excel.annotation.write.style.ContentRowHeight; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @ContentRowHeight(30) public class TempFillData { private String name; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java b/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java index 37b8dc2b..b781f9fa 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/bug/DataType.java @@ -1,14 +1,19 @@ package com.alibaba.easyexcel.test.temp.bug; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jiaosong * @desc * @date 2021/4/6 */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataType { /** * 任务id diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java b/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java index 15ee006d..bcfa101c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/bug/HeadType.java @@ -1,14 +1,19 @@ package com.alibaba.easyexcel.test.temp.bug; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author jiaosong * @desc * @date 2021/4/6 */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HeadType { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java index 2f645339..1cc91c72 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvData.java @@ -5,14 +5,18 @@ import java.util.Date; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class CsvData { @ExcelProperty("字符串标题") private String string; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java b/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java index 8a4b1fb7..ef92e62c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/data/DataType.java @@ -2,9 +2,13 @@ package com.alibaba.easyexcel.test.temp.data; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataType { /** * 任务id diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java b/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java index 32bd065e..2eaf0a27 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/data/HeadType.java @@ -2,9 +2,13 @@ package com.alibaba.easyexcel.test.temp.data; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class HeadType { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java b/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java index 86303729..bd7a7acb 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatData.java @@ -1,16 +1,19 @@ package com.alibaba.easyexcel.test.temp.dataformat; import com.alibaba.excel.metadata.data.ReadCellData; -import com.alibaba.excel.metadata.data.WriteCellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author 罗成 **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class DataFormatData { private ReadCellData date; private ReadCellData num; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java b/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java index 17c34cf1..0ddda379 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/fill/FillData2.java @@ -1,11 +1,15 @@ package com.alibaba.easyexcel.test.temp.fill; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData2 { private String test; } diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java index 53a1a5c8..818aa524 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/issue1662/Data1662.java @@ -1,13 +1,18 @@ package com.alibaba.easyexcel.test.temp.issue1662; +import java.util.Date; + import com.alibaba.excel.annotation.ExcelProperty; + import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; -import java.util.Date; - -@Data +@Getter +@Setter +@EqualsAndHashCode @AllArgsConstructor @NoArgsConstructor public class Data1662 { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java b/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java index 57b54107..0dfd27f7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillData.java @@ -1,8 +1,12 @@ package com.alibaba.easyexcel.test.temp.issue1663; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class FillData { private String name; private double number; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java b/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java index 43726ad3..59444b38 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeData.java @@ -1,12 +1,16 @@ package com.alibaba.easyexcel.test.temp.large; import lombok.Builder; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * @author Jiaju Zhuang */ -@Data +@Getter +@Setter +@EqualsAndHashCode @Builder public class LargeData { diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java b/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java index 0edc995d..99540f2a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/poi/TestCell.java @@ -4,14 +4,18 @@ import java.util.List; import com.alibaba.excel.metadata.data.CellData; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author 罗成 **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class TestCell { private CellData> c1; private CellData> c2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java index 62d5e317..90a721a2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadReadData.java @@ -2,15 +2,18 @@ package com.alibaba.easyexcel.test.temp.read; import com.alibaba.excel.annotation.ExcelProperty; -import lombok.Data; -import lombok.experimental.Accessors; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * 临时测试 * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class HeadReadData { @ExcelProperty({"主标题","数据1"}) private String h1; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java index 8de542d1..97c26ed9 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData1.java @@ -7,9 +7,13 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData1 { @ExcelProperty("字符串标题") @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 42) diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java index 00d99041..ddc0e3db 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/DemoData2.java @@ -7,9 +7,13 @@ import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.enums.poi.FillPatternTypeEnum; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; -@Data +@Getter +@Setter +@EqualsAndHashCode public class DemoData2 { @ExcelProperty("字符串标题") @HeadStyle(fillPatternType = FillPatternTypeEnum.SOLID_FOREGROUND, fillForegroundColor = 42) diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java index a9ba7fa4..34a60bc8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/JsonData.java @@ -1,13 +1,17 @@ package com.alibaba.easyexcel.test.temp.simple; -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * TODO * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class JsonData { private String SS1; private String sS2; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java b/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java index 27967b9f..d9a035ce 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/simple/WriteData.java @@ -2,16 +2,18 @@ package com.alibaba.easyexcel.test.temp.simple; import java.util.Date; -import com.alibaba.excel.annotation.write.style.ContentStyle; - -import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; /** * write data * * @author Jiaju Zhuang **/ -@Data +@Getter +@Setter +@EqualsAndHashCode public class WriteData { // @ContentStyle(locked = true) private Date dd; diff --git a/update.md b/update.md index fdec0ee3..f1f90470 100644 --- a/update.md +++ b/update.md @@ -1,3 +1,9 @@ +# 3.0.2 +* 大幅提升读写性能 +* 修复列宽注解没用的bug [Issue #2151](https://github.com/alibaba/easyexcel/issues/2151) +* 修复`CellData`接收失败的的bug [Issue #2147](https://github.com/alibaba/easyexcel/issues/2147) + + # 3.0.1 * 升级到正式版 * 修复填充样式可能丢失的问题 [Issue #2124](https://github.com/alibaba/easyexcel/issues/2124)