From 031fefde92300094ea286913f923d8a77e88797a Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 29 Oct 2021 16:30:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AF=BB=E5=86=99=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analysis/v07/handlers/CellTagHandler.java | 18 +- .../excel/context/WriteContextImpl.java | 7 +- .../listener/ModelBuildEventListener.java | 4 +- .../holder/xlsx/XlsxReadWorkbookHolder.java | 50 ++--- .../com/alibaba/excel/util/PositionUtils.java | 61 ++---- .../alibaba/excel/util/WriteHandlerUtils.java | 203 +++++++----------- .../write/executor/ExcelWriteAddExecutor.java | 6 +- .../executor/ExcelWriteFillExecutor.java | 10 +- .../chain/CellHandlerExecutionChain.java | 63 ++++++ .../chain/RowHandlerExecutionChain.java | 56 +++++ .../chain/SheetHandlerExecutionChain.java | 48 +++++ .../chain/WorkbookHandlerExecutionChain.java | 57 +++++ .../context/CellWriteHandlerContext.java | 9 +- .../context/RowWriteHandlerContext.java | 8 - .../context/SheetWriteHandlerContext.java | 12 -- .../context/WorkbookWriteHandlerContext.java | 13 -- .../metadata/holder/AbstractWriteHolder.java | 183 ++++++++++------ .../write/metadata/holder/WriteHolder.java | 18 -- .../metadata/holder/WriteWorkbookHolder.java | 11 +- .../easyexcel/test/temp/Lock2Test.java | 17 ++ 20 files changed, 501 insertions(+), 353 deletions(-) create mode 100644 src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java create mode 100644 src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java create mode 100644 src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java create mode 100644 src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java 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 7ee3b2c9..db892989 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -127,7 +127,7 @@ public class WriteContextImpl implements WriteContext { // Workbook handler need to supplementary execution WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( - this, true); + this); WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); @@ -310,12 +310,11 @@ public class WriteContextImpl implements WriteContext { // Workbook and sheet handler need to supplementary execution WorkbookWriteHandlerContext workbookWriteHandlerContext = WriteHandlerUtils.createWorkbookWriteHandlerContext( - this, true); + this); WriteHandlerUtils.beforeWorkbookCreate(workbookWriteHandlerContext, true); WriteHandlerUtils.afterWorkbookCreate(workbookWriteHandlerContext, true); - SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this, - true); + SheetWriteHandlerContext sheetWriteHandlerContext = WriteHandlerUtils.createSheetWriteHandlerContext(this); WriteHandlerUtils.beforeSheetCreate(sheetWriteHandlerContext, true); WriteHandlerUtils.afterSheetCreate(sheetWriteHandlerContext, true); 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 dataFormatDataCache; public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook); @@ -41,27 +52,18 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { setExcelType(ExcelTypeEnum.XLSX); } - 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/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/WriteHandlerUtils.java b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java index cb04b956..062bfdc7 100644 --- a/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java +++ b/src/main/java/com/alibaba/excel/util/WriteHandlerUtils.java @@ -1,22 +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.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 lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.usermodel.Row; /** @@ -24,23 +21,14 @@ import org.apache.poi.ss.usermodel.Row; * * @author Jiaju Zhuang */ +@Slf4j public class WriteHandlerUtils { private WriteHandlerUtils() {} public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext) { - return createWorkbookWriteHandlerContext(writeContext, false); - } - - public static WorkbookWriteHandlerContext createWorkbookWriteHandlerContext(WriteContext writeContext, - boolean runOwn) { - List handlerList = getHandlerList(writeContext, WorkbookWriteHandler.class, runOwn); - WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, null, null, null); - if (runOwn) { - context.setOwnHandlerList(handlerList); - } else { - context.setHandlerList(handlerList); - } + WorkbookWriteHandlerContext context = new WorkbookWriteHandlerContext(writeContext, + writeContext.writeWorkbookHolder()); writeContext.writeWorkbookHolder().setWorkbookWriteHandlerContext(context); return context; } @@ -50,16 +38,9 @@ public class WriteHandlerUtils { } public static void beforeWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { - List handlerList; - if (runOwn) { - handlerList = context.getOwnHandlerList(); - } else { - handlerList = context.getHandlerList(); - } - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((WorkbookWriteHandler)writeHandler).beforeWorkbookCreate(context); - } + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.beforeWorkbookCreate(context); } } @@ -68,42 +49,32 @@ public class WriteHandlerUtils { } public static void afterWorkbookCreate(WorkbookWriteHandlerContext context, boolean runOwn) { - List handlerList; + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, runOwn); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.afterWorkbookCreate(context); + } + } + + private static WorkbookHandlerExecutionChain getWorkbookHandlerExecutionChain(WorkbookWriteHandlerContext context, + boolean runOwn) { + AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder(); if (runOwn) { - handlerList = context.getOwnHandlerList(); + return abstractWriteHolder.getOwnWorkbookHandlerExecutionChain(); } else { - handlerList = context.getHandlerList(); - } - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((WorkbookWriteHandler)writeHandler).afterWorkbookCreate(context); - } + return abstractWriteHolder.getWorkbookHandlerExecutionChain(); } } public static void afterWorkbookDispose(WorkbookWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((WorkbookWriteHandler)writeHandler).afterWorkbookDispose(context); - } + WorkbookHandlerExecutionChain workbookHandlerExecutionChain = getWorkbookHandlerExecutionChain(context, false); + if (workbookHandlerExecutionChain != null) { + workbookHandlerExecutionChain.afterWorkbookDispose(context); } } public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext) { - return createSheetWriteHandlerContext(writeContext, false); - } - - public static SheetWriteHandlerContext createSheetWriteHandlerContext(WriteContext writeContext, boolean runOwn) { - List handlerList = getHandlerList(writeContext, SheetWriteHandler.class, runOwn); - SheetWriteHandlerContext context = new SheetWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), null, null); - if (runOwn) { - context.setOwnHandlerList(handlerList); - } else { - context.setHandlerList(handlerList); - } - return context; + return new SheetWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder()); } public static void beforeSheetCreate(SheetWriteHandlerContext context) { @@ -111,16 +82,9 @@ public class WriteHandlerUtils { } public static void beforeSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { - List handlerList; - if (runOwn) { - handlerList = context.getOwnHandlerList(); - } else { - handlerList = context.getHandlerList(); - } - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((SheetWriteHandler)writeHandler).beforeSheetCreate(context); - } + SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn); + if (sheetHandlerExecutionChain != null) { + sheetHandlerExecutionChain.beforeSheetCreate(context); } } @@ -129,111 +93,90 @@ public class WriteHandlerUtils { } public static void afterSheetCreate(SheetWriteHandlerContext context, boolean runOwn) { - List handlerList; + SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, runOwn); + if (sheetHandlerExecutionChain != null) { + sheetHandlerExecutionChain.afterSheetCreate(context); + } + } + + private static SheetHandlerExecutionChain getSheetHandlerExecutionChain(SheetWriteHandlerContext context, + boolean runOwn) { + AbstractWriteHolder abstractWriteHolder = (AbstractWriteHolder)context.getWriteContext().currentWriteHolder(); if (runOwn) { - handlerList = context.getOwnHandlerList(); + return abstractWriteHolder.getOwnSheetHandlerExecutionChain(); } else { - handlerList = context.getHandlerList(); - } - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((SheetWriteHandler)writeHandler).afterSheetCreate(context); - } + return abstractWriteHolder.getSheetHandlerExecutionChain(); } } public static CellWriteHandlerContext createCellWriteHandlerContext(WriteContext writeContext, Row row, Integer rowIndex, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead, ExcelContentProperty excelContentProperty) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get( - CellWriteHandler.class); return new CellWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), writeContext.writeSheetHolder(), writeContext.writeTableHolder(), row, rowIndex, null, columnIndex, - relativeRowIndex, head, null, null, isHead, excelContentProperty, handlerList); + relativeRowIndex, head, null, null, isHead, excelContentProperty); } public static void beforeCellCreate(CellWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((CellWriteHandler)writeHandler).beforeCellCreate(context); - } + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.beforeCellCreate(context); } } public static void afterCellCreate(CellWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((CellWriteHandler)writeHandler).afterCellCreate(context); - } + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellCreate(context); } } public static void afterCellDataConverted(CellWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((CellWriteHandler)writeHandler).afterCellDataConverted(context); - } + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellDataConverted(context); } } public static void afterCellDispose(CellWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((CellWriteHandler)writeHandler).afterCellDispose(context); - } + CellHandlerExecutionChain cellHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getCellHandlerExecutionChain(); + if (cellHandlerExecutionChain != null) { + cellHandlerExecutionChain.afterCellDispose(context); } } public static RowWriteHandlerContext createRowWriteHandlerContext(WriteContext writeContext, Integer rowIndex, Integer relativeRowIndex, Boolean isHead) { - List handlerList = writeContext.currentWriteHolder().writeHandlerMap().get( - RowWriteHandler.class); - return new RowWriteHandlerContext(writeContext, - writeContext.writeWorkbookHolder(), - writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, - isHead, handlerList); + return new RowWriteHandlerContext(writeContext, writeContext.writeWorkbookHolder(), + writeContext.writeSheetHolder(), writeContext.writeTableHolder(), rowIndex, null, relativeRowIndex, isHead); } public static void beforeRowCreate(RowWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((RowWriteHandler)writeHandler).beforeRowCreate(context); - } + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.beforeRowCreate(context); } } public static void afterRowCreate(RowWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((RowWriteHandler)writeHandler).afterRowCreate(context); - } + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.afterRowCreate(context); } } public static void afterRowDispose(RowWriteHandlerContext context) { - List handlerList = context.getHandlerList(); - if (CollectionUtils.isNotEmpty(handlerList)) { - for (WriteHandler writeHandler : handlerList) { - ((RowWriteHandler)writeHandler).afterRowDispose(context); - } + RowHandlerExecutionChain rowHandlerExecutionChain = ((AbstractWriteHolder)context.getWriteContext() + .currentWriteHolder()).getRowHandlerExecutionChain(); + if (rowHandlerExecutionChain != null) { + rowHandlerExecutionChain.afterRowDispose(context); } } - private static List getHandlerList(WriteContext writeContext, Class clazz, - boolean runOwn) { - Map, List> writeHandlerMap; - if (runOwn) { - writeHandlerMap = writeContext.currentWriteHolder().ownWriteHandlerMap(); - } else { - writeHandlerMap = writeContext.currentWriteHolder().writeHandlerMap(); - } - return writeHandlerMap.get(clazz); - } } 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 ffb8844c..0bb4007c 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteAddExecutor.java @@ -142,6 +142,8 @@ public class ExcelWriteAddExecutor extends AbstractExcelWriteExecutor { Map sortedAllFiledMap) { WriteHolder currentWriteHolder = writeContext.currentWriteHolder(); BeanMap beanMap = BeanMapUtils.create(oneRowData); + // 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 @@ -151,7 +153,7 @@ 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; } @@ -188,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; 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 3eb49f2b..97b8928e 100644 --- a/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java +++ b/src/main/java/com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java @@ -185,12 +185,14 @@ 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); } + Set dataKeySet = new HashSet(dataMap.keySet()); + for (AnalysisCell analysisCell : analysisCellList) { CellWriteHandlerContext cellWriteHandlerContext = WriteHandlerUtils.createCellWriteHandlerContext( writeContext, null, analysisCell.getRowIndex(), null, analysisCell.getColumnIndex(), @@ -198,7 +200,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { if (analysisCell.getOnlyOneVariable()) { String variable = analysisCell.getVariableList().get(0); - if (!dataMap.containsKey(variable)) { + if (!dataKeySet.contains(variable)) { continue; } Object value = dataMap.get(variable); @@ -231,7 +233,7 @@ public class ExcelWriteFillExecutor extends AbstractExcelWriteExecutor { 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); 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..4826d6bc --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/CellHandlerExecutionChain.java @@ -0,0 +1,63 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.handler.context.CellWriteHandlerContext; + +import lombok.Data; + +/** + * Execute the cell handler chain + * + * @author Jiaju Zhuang + */ +@Data +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..872afa8a --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/RowHandlerExecutionChain.java @@ -0,0 +1,56 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.RowWriteHandler; +import com.alibaba.excel.write.handler.context.RowWriteHandlerContext; + +import lombok.Data; + +/** + * Execute the row handler chain + * + * @author Jiaju Zhuang + */ +@Data +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..56770496 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/SheetHandlerExecutionChain.java @@ -0,0 +1,48 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.SheetWriteHandler; +import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext; + +import lombok.Data; + +/** + * Execute the sheet handler chain + * + * @author Jiaju Zhuang + */ +@Data +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..c7dacbb4 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/handler/chain/WorkbookHandlerExecutionChain.java @@ -0,0 +1,57 @@ +package com.alibaba.excel.write.handler.chain; + +import com.alibaba.excel.write.handler.WorkbookWriteHandler; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; + +import lombok.Data; + +/** + * Execute the workbook handler chain + * + * @author Jiaju Zhuang + */ +@Data +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 d585db93..bd6b1930 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 @@ -7,7 +7,6 @@ import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; -import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.impl.FillStyleCellWriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; @@ -106,16 +105,11 @@ public class CellWriteHandlerContext { */ private Boolean ignoreFillStyle; - /** - * handler - */ - private List handlerList; - public CellWriteHandlerContext(WriteContext writeContext, WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer rowIndex, Cell cell, Integer columnIndex, Integer relativeRowIndex, Head headData, List> cellDataList, WriteCellData firstCellData, - Boolean head, ExcelContentProperty excelContentProperty, List handlerList) { + Boolean head, ExcelContentProperty excelContentProperty) { this.writeContext = writeContext; this.writeWorkbookHolder = writeWorkbookHolder; this.writeSheetHolder = writeSheetHolder; @@ -130,6 +124,5 @@ public class CellWriteHandlerContext { this.firstCellData = firstCellData; this.head = head; this.excelContentProperty = excelContentProperty; - this.handlerList = handlerList; } } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java index cf3f79e6..6578d3a2 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/RowWriteHandlerContext.java @@ -1,9 +1,6 @@ package com.alibaba.excel.write.handler.context; -import java.util.List; - import com.alibaba.excel.context.WriteContext; -import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteTableHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; @@ -52,9 +49,4 @@ public class RowWriteHandlerContext { * Nullable.It is null in the case of fill data. */ private Boolean head; - - /** - * handler - */ - private List handlerList; } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java index 17401bd8..c1164eb0 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/SheetWriteHandlerContext.java @@ -1,9 +1,6 @@ package com.alibaba.excel.write.handler.context; -import java.util.List; - import com.alibaba.excel.context.WriteContext; -import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; @@ -30,13 +27,4 @@ public class SheetWriteHandlerContext { * sheet */ private WriteSheetHolder writeSheetHolder; - - /** - * handler - */ - private List handlerList; - /** - * handler - */ - private List ownHandlerList; } diff --git a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java index 032f21c6..f955d4bf 100644 --- a/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java +++ b/src/main/java/com/alibaba/excel/write/handler/context/WorkbookWriteHandlerContext.java @@ -1,9 +1,6 @@ package com.alibaba.excel.write.handler.context; -import java.util.List; - import com.alibaba.excel.context.WriteContext; -import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import lombok.AllArgsConstructor; @@ -25,14 +22,4 @@ public class WorkbookWriteHandlerContext { * workbook */ private WriteWorkbookHolder writeWorkbookHolder; - - /** - * handler - */ - private List handlerList; - - /** - * handler - */ - private List ownHandlerList; } diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index 2961efc0..cc4b2f21 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; @@ -62,15 +65,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 +91,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 +200,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 +209,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) { @@ -229,7 +260,7 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ } //if (hasStyle) { - dealStyle(handlerList); + dealStyle(handlerList); //} dealRowHigh(handlerList); @@ -310,38 +341,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 +369,74 @@ 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); + } + this.writeHandlerList.add(writeHandler); } - if (writeHandler instanceof RowWriteHandler) { - result.get(RowWriteHandler.class).add(writeHandler); + return; + } + if (writeHandler instanceof RowWriteHandler) { + if (!runOwn) { + if (rowHandlerExecutionChain == null) { + rowHandlerExecutionChain = new RowHandlerExecutionChain((RowWriteHandler)writeHandler); + } else { + rowHandlerExecutionChain.addLast((RowWriteHandler)writeHandler); + } + this.writeHandlerList.add(writeHandler); } - if (writeHandler instanceof SheetWriteHandler) { - result.get(SheetWriteHandler.class).add(writeHandler); + return; + } + if (writeHandler instanceof SheetWriteHandler) { + if (!runOwn) { + if (sheetHandlerExecutionChain == null) { + sheetHandlerExecutionChain = new SheetHandlerExecutionChain((SheetWriteHandler)writeHandler); + } else { + sheetHandlerExecutionChain.addLast((SheetWriteHandler)writeHandler); + } + this.writeHandlerList.add(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); + return; + } + if (writeHandler instanceof WorkbookWriteHandler) { + if (!runOwn) { + if (workbookHandlerExecutionChain == null) { + workbookHandlerExecutionChain = new WorkbookHandlerExecutionChain( + (WorkbookWriteHandler)writeHandler); + } else { + workbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler); + } + this.writeHandlerList.add(writeHandler); + } else { + if (ownWorkbookHandlerExecutionChain == null) { + ownWorkbookHandlerExecutionChain = new WorkbookHandlerExecutionChain( + (WorkbookWriteHandler)writeHandler); + } else { + ownWorkbookHandlerExecutionChain.addLast((WorkbookWriteHandler)writeHandler); + } } - result.get(WriteHandler.class).add(writeHandler); } - return result; } @Override @@ -406,16 +465,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(); 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/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index ef619196..100e59a1 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 @@ -259,8 +259,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; @@ -277,14 +275,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/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")); + } + + }