From e2bb1999f5ea3b8dce5e5e56a16f47aca133efea Mon Sep 17 00:00:00 2001 From: zhuangjiaju Date: Tue, 23 Jul 2019 22:04:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E8=AF=BB=E5=86=99=E5=AE=9E?= =?UTF-8?q?=E4=BD=93=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alibaba/excel/EasyExcelFactory.java | 2 +- .../java/com/alibaba/excel/ExcelReader.java | 2 +- .../java/com/alibaba/excel/ExcelWriter.java | 6 +- .../alibaba/excel/analysis/ExcelAnalyser.java | 2 +- .../excel/analysis/ExcelAnalyserImpl.java | 4 +- .../alibaba/excel/analysis/ExcelExecutor.java | 2 +- .../excel/analysis/v03/XlsSaxAnalyser.java | 2 +- .../v03/handlers/BOFRecordHandler.java | 4 +- .../excel/analysis/v07/XlsxSaxAnalyser.java | 4 +- .../excel/context/AnalysisContext.java | 62 +- .../excel/context/AnalysisContextImpl.java | 10 +- .../alibaba/excel/context/WriteContext.java | 13 +- .../excel/context/WriteContextImpl.java | 24 +- .../com/alibaba/excel/enums/HolderEnum.java | 25 + .../excel/metadata/AbstractHolder.java | 86 +++ .../excel/metadata/BasicParameter.java | 82 +-- .../excel/metadata/GlobalConfiguration.java | 37 ++ .../com/alibaba/excel/metadata/Holder.java | 34 ++ .../com/alibaba/excel/metadata/Sheet.java | 197 +++---- .../com/alibaba/excel/metadata/Table.java | 55 +- .../property/ExcelContentProperty.java | 2 + .../metadata/property/ExcelHeadProperty.java | 134 +---- .../read/builder/ExcelReaderBuilder.java | 2 +- .../listener/ModelBuildEventListener.java | 2 +- .../read/metadata/ReadBasicParameter.java | 43 ++ .../excel/read/metadata/ReadSheet.java | 33 ++ .../excel/read/metadata/ReadWorkbook.java | 95 +++ .../holder/AbstractWriteConfiguration.java | 240 ++++++++ .../read/metadata/holder/ReadHolder.java | 40 ++ .../read/metadata/holder/ReadRowHolder.java | 79 +++ .../read/metadata/holder/SheetHolder.java | 239 ++++++++ .../metadata/holder}/TableHolder.java | 15 +- .../metadata/holder}/WorkbookHolder.java | 45 +- .../property/ExcelReadHeadProperty.java | 17 + .../com/alibaba/excel/util/WorkBookUtil.java | 4 +- .../com/alibaba/excel/write/ExcelBuilder.java | 4 +- .../alibaba/excel/write/ExcelBuilderImpl.java | 9 +- .../write/builder/ExcelWriterBuilder.java | 2 +- .../builder/ExcelWriterSheetBuilder.java | 2 +- .../builder/ExcelWriterTableBuilder.java | 2 +- .../excel/write/handler/CellWriteHandler.java | 4 +- .../excel/write/handler/RowWriteHandler.java | 4 +- .../write/handler/SheetWriteHandler.java | 4 +- .../write/handler/WorkbookWriteHandler.java | 2 +- .../write/merge/AbstractMergeStrategy.java | 4 +- .../write/metadata/WriteBasicParameter.java | 51 ++ .../excel/write/metadata/WriteSheet.java | 69 +++ .../excel/write/metadata/WriteTable.java | 38 ++ .../metadata/WriteWorkbook.java} | 91 +-- .../metadata/holder/AbstractWriteHolder.java} | 555 ++++++++---------- .../excel/write/metadata/holder/UTils.java | 86 +++ .../metadata/holder/WriteHolder.java} | 28 +- .../metadata/holder/WriteSheetHolder.java | 109 ++++ .../metadata/holder/WriteTableHolder.java | 61 ++ .../metadata/holder/WriteWorkbookHolder.java | 159 +++++ .../property/ExcelWriteHeadProperty.java | 143 +++++ .../style/AbstractCellStyleStrategy.java | 6 +- .../AbstractColumnWidthStyleStrategy.java | 4 +- .../row/AbstractRowHeightStyleStrategy.java | 4 +- .../com/alibaba/easyexcel/test/ReadTest.java | 2 +- .../com/alibaba/easyexcel/test/WriteTest.java | 4 +- .../alibaba/easyexcel/test/read/ReadTest.java | 2 +- .../test/read/large/LargeData07Test.java | 2 +- .../test/read/simple/SimpleData07Test.java | 2 +- .../test/wirte/nohead/NoHeadData07Test.java | 2 +- .../test/wirte/order/OrderData07Test.java | 2 +- .../test/wirte/simple/SimpleData07Test.java | 2 +- 67 files changed, 2233 insertions(+), 869 deletions(-) create mode 100644 src/main/java/com/alibaba/excel/enums/HolderEnum.java create mode 100644 src/main/java/com/alibaba/excel/metadata/AbstractHolder.java create mode 100644 src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java create mode 100644 src/main/java/com/alibaba/excel/metadata/Holder.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/ReadBasicParameter.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/AbstractWriteConfiguration.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/SheetHolder.java rename src/main/java/com/alibaba/excel/{metadata/holder/write => read/metadata/holder}/TableHolder.java (90%) rename src/main/java/com/alibaba/excel/{metadata/holder/write => read/metadata/holder}/WorkbookHolder.java (91%) create mode 100644 src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java create mode 100644 src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java create mode 100644 src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java create mode 100644 src/main/java/com/alibaba/excel/write/metadata/WriteTable.java rename src/main/java/com/alibaba/excel/{metadata/Workbook.java => write/metadata/WriteWorkbook.java} (53%) rename src/main/java/com/alibaba/excel/{metadata/holder/write/AbstractWriteConfiguration.java => write/metadata/holder/AbstractWriteHolder.java} (51%) create mode 100644 src/main/java/com/alibaba/excel/write/metadata/holder/UTils.java rename src/main/java/com/alibaba/excel/{metadata/holder/write/WriteConfiguration.java => write/metadata/holder/WriteHolder.java} (61%) create mode 100644 src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java create mode 100644 src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java create mode 100644 src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java create mode 100644 src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java diff --git a/src/main/java/com/alibaba/excel/EasyExcelFactory.java b/src/main/java/com/alibaba/excel/EasyExcelFactory.java index d6229bbf..5ece4ffb 100644 --- a/src/main/java/com/alibaba/excel/EasyExcelFactory.java +++ b/src/main/java/com/alibaba/excel/EasyExcelFactory.java @@ -8,7 +8,7 @@ import java.util.List; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.WriteHandler; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; diff --git a/src/main/java/com/alibaba/excel/ExcelReader.java b/src/main/java/com/alibaba/excel/ExcelReader.java index 6cfecbde..cbed7896 100644 --- a/src/main/java/com/alibaba/excel/ExcelReader.java +++ b/src/main/java/com/alibaba/excel/ExcelReader.java @@ -13,7 +13,7 @@ import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelAnalysisException; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; import com.alibaba.excel.parameter.AnalysisParam; import com.alibaba.excel.support.ExcelTypeEnum; diff --git a/src/main/java/com/alibaba/excel/ExcelWriter.java b/src/main/java/com/alibaba/excel/ExcelWriter.java index 603198d3..3612d6dc 100644 --- a/src/main/java/com/alibaba/excel/ExcelWriter.java +++ b/src/main/java/com/alibaba/excel/ExcelWriter.java @@ -7,9 +7,9 @@ import java.util.List; import java.util.Map; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.metadata.Sheet; -import com.alibaba.excel.metadata.Table; -import com.alibaba.excel.metadata.Workbook; +import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.write.metadata.Table; +import com.alibaba.excel.write.metadata.Workbook; import com.alibaba.excel.parameter.GenerateParam; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.ExcelBuilder; diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java index 26179347..72336cc0 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java @@ -1,7 +1,7 @@ package com.alibaba.excel.analysis; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; /** * Excel file analyser diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 23124775..71b9a9b4 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -5,8 +5,8 @@ import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContextImpl; import com.alibaba.excel.exception.ExcelAnalysisException; -import com.alibaba.excel.metadata.Sheet; -import com.alibaba.excel.metadata.Workbook; +import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.write.metadata.Workbook; import com.alibaba.excel.support.ExcelTypeEnum; /** diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java b/src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java index dc575baf..a06b013b 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java @@ -2,7 +2,7 @@ package com.alibaba.excel.analysis; import java.util.List; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; /** * Excel file Executor diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index d952a496..f0f63db4 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -29,7 +29,7 @@ import com.alibaba.excel.analysis.v03.handlers.SSTRecordHandler; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent; import com.alibaba.excel.exception.ExcelAnalysisException; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; import com.alibaba.excel.util.CollectionUtils; /** diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BOFRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BOFRecordHandler.java index fb41807f..acecdcfc 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BOFRecordHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BOFRecordHandler.java @@ -1,14 +1,12 @@ package com.alibaba.excel.analysis.v03.handlers; import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler; -import com.alibaba.excel.analysis.v03.XlsRecordHandler; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BoundSheetRecord; import org.apache.poi.hssf.record.Record; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import java.util.ArrayList; import java.util.List; 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 40964968..248723dc 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -26,8 +26,8 @@ import com.alibaba.excel.analysis.ExcelExecutor; import com.alibaba.excel.cache.Ehcache; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.exception.ExcelAnalysisException; -import com.alibaba.excel.metadata.Sheet; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; import com.alibaba.excel.util.FileUtils; /** diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContext.java b/src/main/java/com/alibaba/excel/context/AnalysisContext.java index 5ea5a318..19e22c3d 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContext.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContext.java @@ -1,8 +1,9 @@ package com.alibaba.excel.context; -import com.alibaba.excel.metadata.holder.read.ReadConfiguration; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.read.metadata.ReadSheet; +import com.alibaba.excel.read.metadata.holder.ReadRowHolder; +import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; +import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; /** * @@ -14,71 +15,28 @@ public interface AnalysisContext { /** * Select the current table * - * @param sheet + * @param readSheet */ - void currentSheet(com.alibaba.excel.metadata.Sheet sheet); + void currentSheet(ReadSheet readSheet); /** * All information about the workbook you are currently working on * * @return */ - WorkbookHolder currentWorkbookHolder(); + ReadWorkbookHolder currentWorkbookHolder(); /** * All information about the sheet you are currently working on * * @return */ - SheetHolder currentSheetHolder(); + ReadSheetHolder currentSheetHolder(); /** - * Configuration of currently operated cell + * Row of currently operated cell * * @return */ - ReadConfiguration currentConfiguration(); - - /** - * set current result - * - * @param result - */ - void setCurrentRowAnalysisResult(Object result); - - /** - * get current result - * - * @return get current result - */ - Object currentRowAnalysisResult(); - - /** - * get current row - * - * @return - */ - Integer currentRowNum(); - - /** - * set current row num - * - * @param row - */ - void setCurrentRowNum(Integer row); - - /** - * get total row , Data may be inaccurate - * - * @return - */ - @Deprecated - Integer getTotalCount(); - - /** - * get total row ,Data may be inaccurate - * - * @param totalCount - */ - void setTotalCount(Integer totalCount); + ReadRowHolder currentRowHolder(); } diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index 73ffd9d3..7542d7a6 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -3,8 +3,10 @@ package com.alibaba.excel.context; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; +import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.write.metadata.Workbook; /** * @@ -21,7 +23,7 @@ public class AnalysisContextImpl implements AnalysisContext { */ private SheetHolder currentSheetHolder; - public AnalysisContextImpl(com.alibaba.excel.metadata.Workbook workbook) { + public AnalysisContextImpl(Workbook workbook) { if (workbook == null) { throw new IllegalArgumentException("Workbook argument cannot be null"); } @@ -32,7 +34,7 @@ public class AnalysisContextImpl implements AnalysisContext { } @Override - public void currentSheet(com.alibaba.excel.metadata.Sheet sheet) { + public void currentSheet(Sheet sheet) { if (sheet == null) { throw new IllegalArgumentException("Sheet argument cannot be null"); } diff --git a/src/main/java/com/alibaba/excel/context/WriteContext.java b/src/main/java/com/alibaba/excel/context/WriteContext.java index 26b2e02c..be43ada2 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContext.java +++ b/src/main/java/com/alibaba/excel/context/WriteContext.java @@ -1,10 +1,11 @@ package com.alibaba.excel.context; -import com.alibaba.excel.metadata.Table; -import com.alibaba.excel.metadata.holder.write.WriteConfiguration; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.write.metadata.Table; +import com.alibaba.excel.write.metadata.holder.WriteConfiguration; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; +import com.alibaba.excel.write.metadata.Sheet; /** * Write context @@ -17,7 +18,7 @@ public interface WriteContext { * * @param sheet */ - void currentSheet(com.alibaba.excel.metadata.Sheet sheet); + void currentSheet(Sheet sheet); /** * If the current table already exists, select it; if not, create it diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 1d44b2c3..4baa1182 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -13,11 +13,6 @@ import org.slf4j.LoggerFactory; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.Table; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; -import com.alibaba.excel.metadata.holder.write.WriteConfiguration; import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.write.handler.CellWriteHandler; @@ -25,6 +20,12 @@ 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.metadata.Table; +import com.alibaba.excel.write.metadata.Workbook; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; +import com.alibaba.excel.write.metadata.holder.WriteConfiguration; /** * A context is the main anchorage point of a excel writer. @@ -52,7 +53,7 @@ public class WriteContextImpl implements WriteContext { */ private WriteConfiguration currentWriteConfiguration; - public WriteContextImpl(com.alibaba.excel.metadata.Workbook workbook) { + public WriteContextImpl(Workbook workbook) { if (workbook == null) { throw new IllegalArgumentException("Workbook argument cannot be null"); } @@ -96,7 +97,7 @@ public class WriteContextImpl implements WriteContext { } } - private void initCurrentWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { + private void initCurrentWorkbookHolder(Workbook workbook) { currentWorkbookHolder = new WorkbookHolder(workbook); currentWriteConfiguration = currentWorkbookHolder; if (LOGGER.isDebugEnabled()) { @@ -108,11 +109,12 @@ public class WriteContextImpl implements WriteContext { * @param sheet */ @Override - public void currentSheet(com.alibaba.excel.metadata.Sheet sheet) { + public void currentSheet(com.alibaba.excel.write.metadata.Sheet sheet) { if (sheet == null) { throw new IllegalArgumentException("Sheet argument cannot be null"); } if (sheet.getSheetNo() == null || sheet.getSheetNo() <= 0) { + LOGGER.info("Sheet number is null"); sheet.setSheetNo(0); } if (currentWorkbookHolder.getHasBeenInitializedSheet().containsKey(sheet.getSheetNo())) { @@ -163,7 +165,7 @@ public class WriteContextImpl implements WriteContext { } } - private void initCurrentSheetHolder(com.alibaba.excel.metadata.Sheet sheet) { + private void initCurrentSheetHolder(com.alibaba.excel.write.metadata.Sheet sheet) { currentSheetHolder = new SheetHolder(sheet, currentWorkbookHolder); currentWorkbookHolder.getHasBeenInitializedSheet().put(sheet.getSheetNo(), currentSheetHolder); currentTableHolder = null; @@ -173,7 +175,7 @@ public class WriteContextImpl implements WriteContext { } } - private void initSheet(com.alibaba.excel.metadata.Sheet sheet) { + private void initSheet(com.alibaba.excel.write.metadata.Sheet sheet) { Sheet currentSheet; try { currentSheet = currentWorkbookHolder.getWorkbook().getSheetAt(sheet.getSheetNo()); @@ -303,7 +305,7 @@ public class WriteContextImpl implements WriteContext { initHead(currentTableHolder.getExcelHeadProperty()); } - private void initCurrentTableHolder(com.alibaba.excel.metadata.Table table) { + private void initCurrentTableHolder(Table table) { currentTableHolder = new TableHolder(table, currentSheetHolder, currentWorkbookHolder); currentSheetHolder.getHasBeenInitializedTable().put(table.getTableNo(), currentTableHolder); currentWriteConfiguration = currentTableHolder; diff --git a/src/main/java/com/alibaba/excel/enums/HolderEnum.java b/src/main/java/com/alibaba/excel/enums/HolderEnum.java new file mode 100644 index 00000000..e4a28115 --- /dev/null +++ b/src/main/java/com/alibaba/excel/enums/HolderEnum.java @@ -0,0 +1,25 @@ +package com.alibaba.excel.enums; + +/** + * The types of holder + * + * @author zhuangjiaju + **/ +public enum HolderEnum { + /** + * workbook + */ + WORKBOOK, + /** + * sheet + */ + SHEET, + /** + * table + */ + TABLE, + /** + * row + */ + ROW; +} diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java new file mode 100644 index 00000000..15dc5553 --- /dev/null +++ b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java @@ -0,0 +1,86 @@ +package com.alibaba.excel.metadata; + +import java.util.List; + +/** + * Write/read holder + * + * @author zhuangjiaju + */ +public abstract class AbstractHolder implements Holder { + /** + * Record whether it's new or from cache + */ + private Boolean newInitialization; + /** + * You can only choose one of the {@link AbstractHolder#head} and {@link AbstractHolder#clazz} + */ + private List> head; + /** + * You can only choose one of the {@link AbstractHolder#head} and {@link AbstractHolder#clazz} + */ + private Class clazz; + /** + * Some global variables + */ + private GlobalConfiguration globalConfiguration; + + public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) { + this.newInitialization = Boolean.TRUE; + this.head = basicParameter.getHead(); + this.clazz = basicParameter.getClazz(); + this.globalConfiguration = new GlobalConfiguration(); + if (basicParameter.getAutoTrim() == null) { + if (prentAbstractHolder == null) { + globalConfiguration.setAutoTrim(Boolean.TRUE); + } else { + globalConfiguration.setAutoTrim(prentAbstractHolder.getGlobalConfiguration().getAutoTrim()); + } + } else { + globalConfiguration.setAutoTrim(basicParameter.getAutoTrim()); + } + } + + public Boolean getNewInitialization() { + return newInitialization; + } + + public void setNewInitialization(Boolean newInitialization) { + this.newInitialization = newInitialization; + } + + public List> getHead() { + return head; + } + + public void setHead(List> head) { + this.head = head; + } + + public Class getClazz() { + return clazz; + } + + public void setClazz(Class clazz) { + this.clazz = clazz; + } + + public GlobalConfiguration getGlobalConfiguration() { + return globalConfiguration; + } + + public void setGlobalConfiguration(GlobalConfiguration globalConfiguration) { + this.globalConfiguration = globalConfiguration; + } + + @Override + public GlobalConfiguration globalConfiguration() { + return getGlobalConfiguration(); + } + + @Override + public boolean isNew() { + return getNewInitialization(); + } + +} diff --git a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java index a4dd583c..0bb9f2b8 100644 --- a/src/main/java/com/alibaba/excel/metadata/BasicParameter.java +++ b/src/main/java/com/alibaba/excel/metadata/BasicParameter.java @@ -4,8 +4,6 @@ import java.util.ArrayList; import java.util.List; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.read.listener.ReadListener; -import com.alibaba.excel.write.handler.WriteHandler; /** * Basic parameter @@ -13,18 +11,6 @@ import com.alibaba.excel.write.handler.WriteHandler; * @author zhuangjiaju **/ public class BasicParameter { - /** - * Count the number of added heads when read sheet. - * - *
  • 0 - This Sheet has no head ,since the first row are the data - *
  • 1 - This Sheet has one row head , this is the default - *
  • 2 - This Sheet has two row head ,since the third row is the data - */ - private Integer readHeadRowNumber; - /** - * Writes the head relative to the existing contents of the sheet. Indexes are zero-based. - */ - private Integer writeRelativeHeadRowIndex; /** * You can only choose one of the {@link BasicParameter#head} and {@link BasicParameter#clazz} */ @@ -33,42 +19,22 @@ public class BasicParameter { * You can only choose one of the {@link BasicParameter#head} and {@link BasicParameter#clazz} */ private Class clazz; - /** - * Need Head - */ - private Boolean needHead; /** * Custom type conversions override the default */ private List customConverterList = new ArrayList(); - /** - * Custom type handler override the default - */ - private List customWriteHandlerList = new ArrayList(); - /** - * Custom type listener run after default - */ - private List customReadListenerList = new ArrayList(); /** * Automatic trim includes sheet name and content */ private Boolean autoTrim; - - public Integer getReadHeadRowNumber() { - return readHeadRowNumber; - } - - public void setReadHeadRowNumber(Integer readHeadRowNumber) { - this.readHeadRowNumber = readHeadRowNumber; - } - - public Integer getWriteRelativeHeadRowIndex() { - return writeRelativeHeadRowIndex; - } - - public void setWriteRelativeHeadRowIndex(Integer writeRelativeHeadRowIndex) { - this.writeRelativeHeadRowIndex = writeRelativeHeadRowIndex; - } + /** + * true if date uses 1904 windowing, or false if using 1900 date windowing. + * + * default is false + * + * @return + */ + private Boolean use1904windowing; public List> getHead() { return head; @@ -86,14 +52,6 @@ public class BasicParameter { this.clazz = clazz; } - public Boolean getNeedHead() { - return needHead; - } - - public void setNeedHead(Boolean needHead) { - this.needHead = needHead; - } - public List getCustomConverterList() { return customConverterList; } @@ -102,22 +60,6 @@ public class BasicParameter { this.customConverterList = customConverterList; } - public List getCustomWriteHandlerList() { - return customWriteHandlerList; - } - - public void setCustomWriteHandlerList(List customWriteHandlerList) { - this.customWriteHandlerList = customWriteHandlerList; - } - - public List getCustomReadListenerList() { - return customReadListenerList; - } - - public void setCustomReadListenerList(List customReadListenerList) { - this.customReadListenerList = customReadListenerList; - } - public Boolean getAutoTrim() { return autoTrim; } @@ -125,4 +67,12 @@ public class BasicParameter { public void setAutoTrim(Boolean autoTrim) { this.autoTrim = autoTrim; } + + public Boolean getUse1904windowing() { + return use1904windowing; + } + + public void setUse1904windowing(Boolean use1904windowing) { + this.use1904windowing = use1904windowing; + } } diff --git a/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java new file mode 100644 index 00000000..ab51b128 --- /dev/null +++ b/src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java @@ -0,0 +1,37 @@ +package com.alibaba.excel.metadata; + +/** + * Global configuration + * + * @author zhuangjiaju + */ +public class GlobalConfiguration { + /** + * Automatic trim includes sheet name and content + */ + private Boolean autoTrim; + /** + * true if date uses 1904 windowing, or false if using 1900 date windowing. + * + * default is false + * + * @return + */ + private Boolean use1904windowing; + + public Boolean getUse1904windowing() { + return use1904windowing; + } + + public void setUse1904windowing(Boolean use1904windowing) { + this.use1904windowing = use1904windowing; + } + + public Boolean getAutoTrim() { + return autoTrim; + } + + public void setAutoTrim(Boolean autoTrim) { + this.autoTrim = autoTrim; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/Holder.java b/src/main/java/com/alibaba/excel/metadata/Holder.java new file mode 100644 index 00000000..9aef31aa --- /dev/null +++ b/src/main/java/com/alibaba/excel/metadata/Holder.java @@ -0,0 +1,34 @@ +package com.alibaba.excel.metadata; + +import com.alibaba.excel.enums.HolderEnum; + +/** + * + * Get the corresponding holder + * + * @author zhuangjiaju + **/ +public interface Holder { + + /** + * What holder is the return + * + * @return + */ + HolderEnum holderType(); + + /** + * + * Record whether it's new or from cache + * + * @return + */ + boolean isNew(); + + /** + * Some global variables + * + * @return + */ + GlobalConfiguration globalConfiguration(); +} diff --git a/src/main/java/com/alibaba/excel/metadata/Sheet.java b/src/main/java/com/alibaba/excel/metadata/Sheet.java index 149ae54d..ef98b9cc 100644 --- a/src/main/java/com/alibaba/excel/metadata/Sheet.java +++ b/src/main/java/com/alibaba/excel/metadata/Sheet.java @@ -4,121 +4,110 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; -import com.alibaba.excel.write.style.RowCellStyleStrategy; -import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; - /** - * sheet - * + * * @author jipengfei */ -public class Sheet extends BasicParameter { +public class Sheet { + /** - * Starting from 0 */ - private Integer sheetNo; + private int headLineMun; + + /** + * Starting from 1 + */ + private int sheetNo; + /** - * sheet name */ private String sheetName; + /** - * column with - * - * @deprecated please use {@link SimpleColumnWidthStyleStrategy} */ - @Deprecated - private Map columnWidthMap = new HashMap(); + private Class clazz; + + /** + */ + private List> head; /** * - * @deprecated please use{@link RowCellStyleStrategy} */ - @Deprecated private TableStyle tableStyle; - public Sheet() { - super(); - } - /** - * Create sheet - * - * @param sheetNo - * @param sheetName + * column with */ - public Sheet(Integer sheetNo, String sheetName) { - super(); - this.sheetNo = sheetNo; - this.sheetName = sheetName; - } + private Map columnWidthMap = new HashMap(); /** - * It was 'sheetNo' starting from 1 and now it is starting from 0 - * - * @param sheetNo - * @param readHeadRowNumber - * @deprecated please use {@link ExcelWriterSheetBuilder#build()} + * */ - @Deprecated - public Sheet(int sheetNo, int readHeadRowNumber) { - super(); - this.sheetNo = sheetNo - 1; - setReadHeadRowNumber(readHeadRowNumber); - } + private Boolean autoWidth = Boolean.FALSE; /** - * It was 'sheetNo' starting from 1 and now it is starting from 0 * - * @param sheetNo - * @deprecated please use {@link ExcelWriterSheetBuilder#build()} */ - @Deprecated + private int startRow = 0; + + public Sheet(int sheetNo) { - super(); - this.sheetNo = sheetNo - 1; + this.sheetNo = sheetNo; } - /** - * It was 'sheetNo' starting from 1 and now it is starting from 0 - * - * @param sheetNo - * @param readHeadRowNumber - * @param clazz - * @deprecated please use {@link ExcelWriterSheetBuilder#build()} - */ - @Deprecated - public Sheet(int sheetNo, int readHeadRowNumber, Class clazz) { - super(); - this.sheetNo = sheetNo - 1; - setReadHeadRowNumber(readHeadRowNumber); - setClazz(clazz); + public Sheet(int sheetNo, int headLineMun) { + this.sheetNo = sheetNo; + this.headLineMun = headLineMun; } - /** - * It was 'sheetNo' starting from 1 and now it is starting from 0 - * - * @param sheetNo - * @param readHeadRowNumber - * @param clazz - * @deprecated please use {@link ExcelWriterSheetBuilder#build()} - */ - @Deprecated - public Sheet(int sheetNo, int readHeadRowNumber, Class clazz, String sheetName, List> head) { - super(); - this.sheetNo = sheetNo - 1; + public Sheet(int sheetNo, int headLineMun, Class clazz) { + this.sheetNo = sheetNo; + this.headLineMun = headLineMun; + this.clazz = clazz; + } + + public Sheet(int sheetNo, int headLineMun, Class clazz, String sheetName, + List> head) { + this.sheetNo = sheetNo; + this.clazz = clazz; + this.headLineMun = headLineMun; this.sheetName = sheetName; + this.head = head; + } - setReadHeadRowNumber(readHeadRowNumber); - setClazz(clazz); - setHead(head); + public List> getHead() { + return head; } - public Integer getSheetNo() { + public void setHead(List> head) { + this.head = head; + } + + public Class getClazz() { + return clazz; + } + + public void setClazz(Class clazz) { + this.clazz = clazz; + if (headLineMun == 0) { + this.headLineMun = 1; + } + } + + public int getHeadLineMun() { + return headLineMun; + } + + public void setHeadLineMun(int headLineMun) { + this.headLineMun = headLineMun; + } + + public int getSheetNo() { return sheetNo; } - public void setSheetNo(Integer sheetNo) { + public void setSheetNo(int sheetNo) { this.sheetNo = sheetNo; } @@ -130,6 +119,16 @@ public class Sheet extends BasicParameter { this.sheetName = sheetName; } + public TableStyle getTableStyle() { + return tableStyle; + } + + public void setTableStyle(TableStyle tableStyle) { + this.tableStyle = tableStyle; + } + + + public Map getColumnWidthMap() { return columnWidthMap; } @@ -138,36 +137,32 @@ public class Sheet extends BasicParameter { this.columnWidthMap = columnWidthMap; } - public TableStyle getTableStyle() { - return tableStyle; + @Override + public String toString() { + return "Sheet{" + + "headLineMun=" + headLineMun + + ", sheetNo=" + sheetNo + + ", sheetName='" + sheetName + '\'' + + ", clazz=" + clazz + + ", head=" + head + + ", tableStyle=" + tableStyle + + ", columnWidthMap=" + columnWidthMap + + '}'; } - public void setTableStyle(TableStyle tableStyle) { - this.tableStyle = tableStyle; + public Boolean getAutoWidth() { + return autoWidth; } - /** - * - * @param writeRelativeHeadRowIndex - * @deprecated please use {@link Sheet#setWriteRelativeHeadRowIndex(Integer)} - */ - @Deprecated - public void setStartRow(int writeRelativeHeadRowIndex) { - setWriteRelativeHeadRowIndex(writeRelativeHeadRowIndex); + public void setAutoWidth(Boolean autoWidth) { + this.autoWidth = autoWidth; } - /** - * - * @param readHeadRowNumber - * @deprecated please use {@link Sheet#setReadHeadRowNumber(Integer)} )} - */ - @Deprecated - public void setHeadLineMun(int readHeadRowNumber) { - setReadHeadRowNumber(readHeadRowNumber); + public int getStartRow() { + return startRow; } - @Override - public String toString() { - return "Sheet{" + "sheetNo=" + sheetNo + ", sheetName='" + sheetName + '\'' + '}'; + public void setStartRow(int startRow) { + this.startRow = startRow; } } diff --git a/src/main/java/com/alibaba/excel/metadata/Table.java b/src/main/java/com/alibaba/excel/metadata/Table.java index 0915b401..6643c463 100644 --- a/src/main/java/com/alibaba/excel/metadata/Table.java +++ b/src/main/java/com/alibaba/excel/metadata/Table.java @@ -1,51 +1,60 @@ package com.alibaba.excel.metadata; -import com.alibaba.excel.write.style.RowCellStyleStrategy; +import java.util.List; /** - * table - * * @author jipengfei */ -public class Table extends BasicParameter { +public class Table { /** - * Starting from 0 */ - private Integer tableNo; + private Class clazz; + + /** + */ + private List> head; + + /** + */ + private int tableNo; + /** - * - * @deprecated please use{@link RowCellStyleStrategy} */ - @Deprecated private TableStyle tableStyle; - public Table() { - super(); + public TableStyle getTableStyle() { + return tableStyle; + } + + public void setTableStyle(TableStyle tableStyle) { + this.tableStyle = tableStyle; } public Table(Integer tableNo) { - super(); this.tableNo = tableNo; } - public Integer getTableNo() { - return tableNo; + public Class getClazz() { + return clazz; } - public void setTableNo(Integer tableNo) { - this.tableNo = tableNo; + public void setClazz(Class clazz) { + this.clazz = clazz; } - public TableStyle getTableStyle() { - return tableStyle; + public List> getHead() { + return head; } - public void setTableStyle(TableStyle tableStyle) { - this.tableStyle = tableStyle; + public void setHead(List> head) { + this.head = head; } - @Override - public String toString() { - return "Table{" + "tableNo=" + tableNo + '}'; + public int getTableNo() { + return tableNo; + } + + public void setTableNo(int tableNo) { + this.tableNo = tableNo; } } 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 70d72c32..0bdabeb0 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java @@ -22,6 +22,8 @@ public class ExcelContentProperty { /** * true if date uses 1904 windowing, or false if using 1900 date windowing. * + * default is false + * * @return */ private Boolean use1904windowing; 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 04db6d58..d11ffdd0 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -14,16 +14,9 @@ import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.NumberFormat; -import com.alibaba.excel.annotation.write.style.ColumnWidth; -import com.alibaba.excel.annotation.write.style.ContentRowHeight; -import com.alibaba.excel.annotation.write.style.ContentStyle; -import com.alibaba.excel.annotation.write.style.HeadRowHeight; -import com.alibaba.excel.annotation.write.style.HeadStyle; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.exception.ExcelGenerateException; -import com.alibaba.excel.metadata.CellRange; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.util.StringUtils; /** @@ -55,10 +48,8 @@ public class ExcelHeadProperty { * Configuration column information */ private Map contentPropertyMap; - private RowHeightProperty headRowHeightProperty; - private RowHeightProperty contentRowHeightProperty; - public ExcelHeadProperty(Class headClazz, List> head, WorkbookHolder workbookHolder) { + public ExcelHeadProperty(Class headClazz, List> head, Boolean convertAllFiled) { this.headClazz = headClazz; headMap = new TreeMap(); contentPropertyMap = new TreeMap(); @@ -72,7 +63,7 @@ public class ExcelHeadProperty { headKind = HeadKindEnum.STRING; } else { // convert headClazz to head - initColumnProperties(workbookHolder); + initColumnProperties(convertAllFiled); } initHeadRowNumber(); if (LOGGER.isDebugEnabled()) { @@ -104,7 +95,7 @@ public class ExcelHeadProperty { } } - private void initColumnProperties(WorkbookHolder workbookHolder) { + private void initColumnProperties(Boolean convertAllFiled) { if (headClazz == null) { return; } @@ -127,7 +118,7 @@ public class ExcelHeadProperty { continue; } ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); - if (excelProperty == null && (workbookHolder == null || !workbookHolder.getConvertAllFiled())) { + if (excelProperty == null && convertAllFiled != null && !convertAllFiled) { continue; } if (excelProperty == null || excelProperty.index() < 0) { @@ -141,35 +132,24 @@ public class ExcelHeadProperty { customFiledMap.put(excelProperty.index(), field); } - HeadStyle headStyle = (HeadStyle)headClazz.getAnnotation(HeadStyle.class); - ContentStyle contentStyle = (ContentStyle)headClazz.getAnnotation(ContentStyle.class); - ColumnWidth columnWidth = (ColumnWidth)headClazz.getAnnotation(ColumnWidth.class); - this.headRowHeightProperty = - RowHeightProperty.build((HeadRowHeight)headClazz.getAnnotation(HeadRowHeight.class)); - this.contentRowHeightProperty = - RowHeightProperty.build((ContentRowHeight)headClazz.getAnnotation(ContentRowHeight.class)); - int index = 0; for (Field field : defaultFieldList) { while (customFiledMap.containsKey(index)) { - initOneColumnProperty(index, customFiledMap.get(index), headStyle, contentStyle, columnWidth, - workbookHolder); + initOneColumnProperty(index, customFiledMap.get(index)); customFiledMap.remove(index); index++; } - initOneColumnProperty(index, field, headStyle, contentStyle, columnWidth, workbookHolder); + initOneColumnProperty(index, field); index++; } for (Map.Entry entry : customFiledMap.entrySet()) { - initOneColumnProperty(index, entry.getValue(), headStyle, contentStyle, columnWidth, workbookHolder); + initOneColumnProperty(index, entry.getValue()); index++; } headKind = HeadKindEnum.CLASS; } - private void initOneColumnProperty(int index, Field field, HeadStyle parentHeadStyle, - ContentStyle parentContentStyle, ColumnWidth parentColumnWidth, WorkbookHolder workbookHolder) { - + private void initOneColumnProperty(int index, Field field) { ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); List tmpHeadList = new ArrayList(); if (excelProperty != null) { @@ -181,36 +161,13 @@ public class ExcelHeadProperty { tmpHeadList.add(field.getName()); } Head head = new Head(index, field.getName(), tmpHeadList); - HeadStyle headStyle = field.getAnnotation(HeadStyle.class); - if (headStyle == null) { - headStyle = parentHeadStyle; - } - head.setCellStyleProperty(CellStyleProperty.build(headStyle)); - - ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class); - if (columnWidth == null) { - columnWidth = parentColumnWidth; - } - head.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth)); - ExcelContentProperty excelContentProperty = new ExcelContentProperty(); excelContentProperty.setHead(head); excelContentProperty.setField(field); - ContentStyle contentStyle = field.getAnnotation(ContentStyle.class); - if (contentStyle == null) { - contentStyle = parentContentStyle; - } - excelContentProperty.setCellStyleProperty(CellStyleProperty.build(contentStyle)); - excelContentProperty .setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); - excelContentProperty .setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); - - if (workbookHolder != null && workbookHolder.getReadGlobalProperty() != null) { - excelContentProperty.setGlobalProperty(workbookHolder.getReadGlobalProperty()); - } headMap.put(index, head); contentPropertyMap.put(index, excelContentProperty); } @@ -259,79 +216,4 @@ public class ExcelHeadProperty { this.contentPropertyMap = contentPropertyMap; } - public RowHeightProperty getHeadRowHeightProperty() { - return headRowHeightProperty; - } - - public void setHeadRowHeightProperty(RowHeightProperty headRowHeightProperty) { - this.headRowHeightProperty = headRowHeightProperty; - } - - public RowHeightProperty getContentRowHeightProperty() { - return contentRowHeightProperty; - } - - public void setContentRowHeightProperty(RowHeightProperty contentRowHeightProperty) { - this.contentRowHeightProperty = contentRowHeightProperty; - } - - /** - * Calculate all cells that need to be merged - * - * @return cells that need to be merged - */ - public List headCellRangeList() { - List cellRangeList = new ArrayList(); - int i = 0; - for (Map.Entry entry : headMap.entrySet()) { - Head head = entry.getValue(); - List columnValues = head.getHeadNameList(); - for (int j = 0; j < columnValues.size(); j++) { - int lastRow = getLastRangNum(j, columnValues.get(j), columnValues); - int lastColumn = getLastRangNum(i, columnValues.get(j), head.getHeadNameList()); - if ((lastRow > j || lastColumn > i) && lastRow >= 0 && lastColumn >= 0) { - cellRangeList.add(new CellRange(j, lastRow, i, lastColumn)); - } - } - i++; - } - return cellRangeList; - } - - /** - * Get the last consecutive string position - * - * @param j - * current value position - * @param value - * value content - * @param values - * values - * @return the last consecutive string position - */ - private int getLastRangNum(int j, String value, List values) { - if (value == null) { - return -1; - } - if (j > 0) { - String preValue = values.get(j - 1); - if (value.equals(preValue)) { - return -1; - } - } - int last = j; - for (int i = last + 1; i < values.size(); i++) { - String current = values.get(i); - if (value.equals(current)) { - last = i; - } else { - // if i>j && !value.equals(current) Indicates that the continuous range is exceeded - if (i > j) { - break; - } - } - } - return last; - } - } diff --git a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java index 77519e28..2e44c42f 100644 --- a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java +++ b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java @@ -1,6 +1,6 @@ package com.alibaba.excel.read.builder; -import com.alibaba.excel.metadata.Workbook; +import com.alibaba.excel.write.metadata.Workbook; /** * Build ExcelWriter 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 65da952e..a3660deb 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -14,7 +14,7 @@ import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.read.ReadConfiguration; +import com.alibaba.excel.read.metadata.read.ReadConfiguration; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty; diff --git a/src/main/java/com/alibaba/excel/read/metadata/ReadBasicParameter.java b/src/main/java/com/alibaba/excel/read/metadata/ReadBasicParameter.java new file mode 100644 index 00000000..5f30e2ab --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/ReadBasicParameter.java @@ -0,0 +1,43 @@ +package com.alibaba.excel.read.metadata; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.excel.metadata.BasicParameter; +import com.alibaba.excel.read.listener.ReadListener; + +/** + * Read basic parameter + * + * @author zhuangjiaju + **/ +public class ReadBasicParameter extends BasicParameter { + /** + * Count the number of added heads when read sheet. + * + *
  • 0 - This Sheet has no head ,since the first row are the data + *
  • 1 - This Sheet has one row head , this is the default + *
  • 2 - This Sheet has two row head ,since the third row is the data + */ + private Integer headRowNumber; + /** + * Custom type listener run after default + */ + private List customReadListenerList = new ArrayList(); + + public Integer getHeadRowNumber() { + return headRowNumber; + } + + public void setHeadRowNumber(Integer headRowNumber) { + this.headRowNumber = headRowNumber; + } + + public List getCustomReadListenerList() { + return customReadListenerList; + } + + public void setCustomReadListenerList(List customReadListenerList) { + this.customReadListenerList = customReadListenerList; + } +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java b/src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java new file mode 100644 index 00000000..0dbbbf2e --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java @@ -0,0 +1,33 @@ +package com.alibaba.excel.read.metadata; + +/** + * Read sheet + * + * @author jipengfei + */ +public class ReadSheet extends ReadBasicParameter { + /** + * Starting from 0 + */ + private Integer sheetNo; + /** + * sheet name + */ + private String sheetName; + + public Integer getSheetNo() { + return sheetNo; + } + + public void setSheetNo(Integer sheetNo) { + this.sheetNo = sheetNo; + } + + public String getSheetName() { + return sheetName; + } + + public void setSheetName(String sheetName) { + this.sheetName = sheetName; + } +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java b/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java new file mode 100644 index 00000000..24d03336 --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java @@ -0,0 +1,95 @@ +package com.alibaba.excel.read.metadata; + +import java.io.File; +import java.io.InputStream; + +import com.alibaba.excel.support.ExcelTypeEnum; + +/** + * Workbook + * + * @author zhuangjiaju + **/ +public class ReadWorkbook extends ReadBasicParameter { + /** + * Excel type + */ + private ExcelTypeEnum excelType; + /** + * Read InputStream + *

    + * If 'inputStream' and 'file' all not empty,file first + */ + private InputStream inputStream; + /** + * Read file + *

    + * If 'inputStream' and 'file' all not empty,file first + */ + private File file; + /** + * Default true + */ + private Boolean autoCloseStream; + /** + * Mandatory use 'inputStream' .Default is false + */ + private Boolean mandatoryUseInputStream; + /** + * The default is all excel objects.Default is true. + *

  • if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a field. + *
  • if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed. + * + * @deprecated Just to be compatible with historical data, The default is always going to be convert all filed. + */ + @Deprecated + private Boolean convertAllFiled; + + public ExcelTypeEnum getExcelType() { + return excelType; + } + + public void setExcelType(ExcelTypeEnum excelType) { + this.excelType = excelType; + } + + public InputStream getInputStream() { + return inputStream; + } + + public void setInputStream(InputStream inputStream) { + this.inputStream = inputStream; + } + + public File getFile() { + return file; + } + + public void setFile(File file) { + this.file = file; + } + + public Boolean getAutoCloseStream() { + return autoCloseStream; + } + + public void setAutoCloseStream(Boolean autoCloseStream) { + this.autoCloseStream = autoCloseStream; + } + + public Boolean getMandatoryUseInputStream() { + return mandatoryUseInputStream; + } + + public void setMandatoryUseInputStream(Boolean mandatoryUseInputStream) { + this.mandatoryUseInputStream = mandatoryUseInputStream; + } + + public Boolean getConvertAllFiled() { + return convertAllFiled; + } + + public void setConvertAllFiled(Boolean convertAllFiled) { + this.convertAllFiled = convertAllFiled; + } +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractWriteConfiguration.java b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractWriteConfiguration.java new file mode 100644 index 00000000..48f258bd --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractWriteConfiguration.java @@ -0,0 +1,240 @@ +package com.alibaba.excel.read.metadata.holder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKey; +import com.alibaba.excel.enums.HeadKindEnum; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.exception.ExcelAnalysisException; +import com.alibaba.excel.exception.ExcelDataConvertException; +import com.alibaba.excel.metadata.AbstractHolder; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.read.listener.ReadListenerRegistryCenter; +import com.alibaba.excel.read.listener.event.AnalysisFinishEvent; +import com.alibaba.excel.read.metadata.ReadBasicParameter; +import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; +import com.alibaba.excel.util.StringUtils; + +/** + * Read Holder + * + * @author zhuangjiaju + */ +public abstract class AbstractReadHolder extends AbstractHolder implements ReadHolder, ReadListenerRegistryCenter { + /** + * Count the number of added heads when read sheet. + * + *
  • 0 - This Sheet has no head ,since the first row are the data + *
  • 1 - This Sheet has one row head , this is the default + *
  • 2 - This Sheet has two row head ,since the third row is the data + */ + private Integer headRowNumber; + /** + * Excel head property + */ + private ExcelReadHeadProperty excelReadHeadProperty; + /** + * Read listener + */ + private List readListenerList; + /** + * Converter for workbook + */ + private Map converterMap; + + public AbstractReadHolder(ReadBasicParameter readBasicParameter, AbstractReadHolder parentAbstractReadHolder, + Boolean convertAllFiled) { + super(readBasicParameter, parentAbstractReadHolder); + if (readBasicParameter.getUse1904windowing() == null && parentAbstractReadHolder != null) { + getGlobalConfiguration() + .setUse1904windowing(parentAbstractReadHolder.getGlobalConfiguration().getUse1904windowing()); + } else { + getGlobalConfiguration().setUse1904windowing(readBasicParameter.getUse1904windowing()); + } + + if (readBasicParameter.getHeadRowNumber() == null) { + if (parentAbstractReadHolder == null) { + this.headRowNumber = 1; + } else { + this.headRowNumber = parentAbstractReadHolder.getHeadRowNumber(); + } + } else { + this.headRowNumber = readBasicParameter.getHeadRowNumber(); + } + // Initialization property + this.excelReadHeadProperty = new ExcelReadHeadProperty(getClazz(), getHead(), convertAllFiled); + + if (parentAbstractReadHolder == null) { + this.readListenerList = new ArrayList(); + } else { + this.readListenerList = new ArrayList(parentAbstractReadHolder.getReadListenerList()); + } + if (readBasicParameter.getCustomReadListenerList() != null + && !readBasicParameter.getCustomReadListenerList().isEmpty()) { + this.readListenerList.addAll(readBasicParameter.getCustomReadListenerList()); + } + + if (parentAbstractReadHolder == null) { + this.converterMap = new HashMap(); + } else { + this.converterMap = new HashMap(parentAbstractReadHolder.getConverterMap()); + } + if (readBasicParameter.getCustomConverterList() != null + && !readBasicParameter.getCustomConverterList().isEmpty()) { + for (Converter converter : readBasicParameter.getCustomConverterList()) { + converterMap.put(ConverterKey.buildConverterKey(converter), converter); + } + } + } + + @Override + public void register(AnalysisEventListener listener) { + readListenerList.add(listener); + } + + @Override + public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) { + List cellDataList = (List)event.getAnalysisResult(); + if (analysisContext.currentRowHolder().getRow().getRowNum() > analysisContext.currentSheetHolder() + .getReadHeadRowNumber()) { + for (ReadListener readListener : readListenerList) { + try { + readListener.invoke(analysisContext.currentRowAnalysisResult(), analysisContext); + } catch (Exception e) { + for (ReadListener readListenerException : readListenerList) { + try { + readListenerException.onException(e, analysisContext); + } catch (Exception exception) { + throw new ExcelAnalysisException("Listen error!", exception); + } + } + } + } + } + // Now is header + if (analysisContext.currentRowNum().equals(analysisContext.currentSheetHolder().getReadHeadRowNumber())) { + buildHead(analysisContext, cellDataList); + } + } + + @Override + public void notifyAfterAllAnalysed(AnalysisContext analysisContext) { + for (ReadListener readListener : readListenerList) { + readListener.doAfterAllAnalysed(analysisContext); + } + } + + private void buildHead(AnalysisContext analysisContext, List cellDataList) { + if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelReadHeadProperty().getHeadKind())) { + return; + } + List dataList = (List)buildStringList(cellDataList, analysisContext.currentConfiguration()); + ExcelReadHeadProperty excelHeadPropertyData = analysisContext.currentConfiguration().excelReadHeadProperty(); + Map headMapData = excelHeadPropertyData.getHeadMap(); + Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); + Map tmpHeadMap = new HashMap(headMapData.size() * 4 / 3 + 1); + Map tmpContentPropertyMap = + new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); + for (Map.Entry entry : headMapData.entrySet()) { + Head headData = entry.getValue(); + if (headData.getForceIndex()) { + tmpHeadMap.put(entry.getKey(), headData); + tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); + continue; + } + String headName = headData.getHeadNameList().get(0); + for (int i = 0; i < dataList.size(); i++) { + String headString = dataList.get(i); + if (StringUtils.isEmpty(headString)) { + continue; + } + if (analysisContext.currentSheetHolder().getAutoTrim()) { + headString = headString.trim(); + } + if (headName.equals(headString)) { + headData.setColumnIndex(i); + tmpHeadMap.put(i, headData); + tmpContentPropertyMap.put(i, contentPropertyMapData.get(entry.getKey())); + break; + } + } + } + excelHeadPropertyData.setHeadMap(tmpHeadMap); + excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); + } + + private Object buildStringList(List data, + com.alibaba.excel.read.metadata.read.ReadConfiguration readConfiguration) { + List list = new ArrayList(); + for (CellData cellData : data) { + Converter converter = readConfiguration.readConverterMap() + .get(ConverterKey.buildConverterKey(String.class, cellData.getType())); + if (converter == null) { + throw new ExcelDataConvertException( + "Converter not found, convert " + cellData.getType() + " to String"); + } + try { + list.add((String)(converter.convertToJavaData(cellData, null))); + } catch (Exception e) { + throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e); + } + } + return list; + } + + public List getReadListenerList() { + return readListenerList; + } + + public void setReadListenerList(List readListenerList) { + this.readListenerList = readListenerList; + } + + public Map getConverterMap() { + return converterMap; + } + + public void setConverterMap(Map converterMap) { + this.converterMap = converterMap; + } + + public ExcelReadHeadProperty getExcelReadHeadProperty() { + return excelReadHeadProperty; + } + + public void setExcelReadHeadProperty(ExcelReadHeadProperty excelReadHeadProperty) { + this.excelReadHeadProperty = excelReadHeadProperty; + } + + public Integer getHeadRowNumber() { + return headRowNumber; + } + + public void setHeadRowNumber(Integer headRowNumber) { + this.headRowNumber = headRowNumber; + } + + @Override + public List readListenerList() { + return getReadListenerList(); + } + + @Override + public Map converterMap() { + return getConverterMap(); + } + + @Override + public ExcelReadHeadProperty excelReadHeadProperty() { + return getExcelReadHeadProperty(); + } + +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java new file mode 100644 index 00000000..02bf22d3 --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java @@ -0,0 +1,40 @@ +package com.alibaba.excel.read.metadata.holder; + +import java.util.List; +import java.util.Map; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKey; +import com.alibaba.excel.metadata.Holder; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; + +/** + * + * Get the corresponding Holder + * + * @author zhuangjiaju + **/ +public interface ReadHolder extends Holder { + /** + * What handler does the currently operated cell need to execute + * + * @return + */ + List readListenerList(); + + /** + * What converter does the currently operated cell need to execute + * + * @return + */ + Map converterMap(); + + /** + * What 'ExcelReadHeadProperty' does the currently operated cell need to execute + * + * @return + */ + ExcelReadHeadProperty excelReadHeadProperty(); + +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java new file mode 100644 index 00000000..97d92d07 --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java @@ -0,0 +1,79 @@ +package com.alibaba.excel.read.metadata.holder; + +import org.apache.poi.ss.usermodel.Row; + +import com.alibaba.excel.enums.HolderEnum; +import com.alibaba.excel.metadata.GlobalConfiguration; +import com.alibaba.excel.metadata.Holder; + +/** + * sheet holder + * + * @author zhuangjiaju + */ +public class ReadRowHolder implements Holder { + + /*** + * poi row + */ + private Row row; + /** + * Some global variables + */ + private GlobalConfiguration globalConfiguration; + /** + * The result of the previous listener + */ + private Object currentRowAnalysisResult; + /** + * Data starting from the first row after the head is removed.Start form 1 + */ + private int relativeRowIndex; + + public Row getRow() { + return row; + } + + public void setRow(Row row) { + this.row = row; + } + + public GlobalConfiguration getGlobalConfiguration() { + return globalConfiguration; + } + + public void setGlobalConfiguration(GlobalConfiguration globalConfiguration) { + this.globalConfiguration = globalConfiguration; + } + + public Object getCurrentRowAnalysisResult() { + return currentRowAnalysisResult; + } + + public void setCurrentRowAnalysisResult(Object currentRowAnalysisResult) { + this.currentRowAnalysisResult = currentRowAnalysisResult; + } + + public int getRelativeRowIndex() { + return relativeRowIndex; + } + + public void setRelativeRowIndex(int relativeRowIndex) { + this.relativeRowIndex = relativeRowIndex; + } + + @Override + public HolderEnum holderType() { + return HolderEnum.ROW; + } + + @Override + public boolean isNew() { + return true; + } + + @Override + public GlobalConfiguration globalConfiguration() { + return getGlobalConfiguration(); + } +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/SheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/SheetHolder.java new file mode 100644 index 00000000..4cce405b --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/SheetHolder.java @@ -0,0 +1,239 @@ +package com.alibaba.excel.read.metadata.holder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.poi.ss.usermodel.Sheet; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKey; +import com.alibaba.excel.metadata.CellStyle; +import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.TableStyle; +import com.alibaba.excel.metadata.property.ExcelHeadProperty; +import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.write.handler.WriteHandler; +import com.alibaba.excel.write.style.RowCellStyleStrategy; +import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy; + +/** + * sheet holder + * + * @author zhuangjiaju + */ +public class ReadSheetHolder extends AbstractReadHolder { + private static final Logger LOGGER = LoggerFactory.getLogger(SheetHolder.class); + /*** + * poi sheet + */ + private Sheet sheet; + /*** + * sheetNo + */ + private Integer sheetNo; + /*** + * sheetName + */ + private String sheetName; + /** + * get total row , Data may be inaccurate + */ + @Deprecated + private Integer total; + /*** + * poi sheet + */ + private WorkbookHolder parentWorkBook; + /*** + * has been initialized table + */ + private Map hasBeenInitializedTable; + /** + * current param + */ + private com.alibaba.excel.write.metadata.Sheet sheetParam; + + public static SheetHolder buildWriteWorkSheetHolder(com.alibaba.excel.write.metadata.Sheet sheet, + WorkbookHolder workbookHolder) { + SheetHolder sheetHolder = buildBaseSheetHolder(sheet, workbookHolder); + + sheetHolder.setNewInitialization(Boolean.TRUE); + if (sheet.getNeedHead() == null) { + sheetHolder.setNeedHead(workbookHolder.needHead()); + } else { + sheetHolder.setNeedHead(sheet.getNeedHead()); + } + if (sheet.getWriteRelativeHeadRowIndex() == null) { + sheetHolder.setWriteRelativeHeadRowIndex(workbookHolder.writeRelativeHeadRowIndex()); + } else { + sheetHolder.setWriteRelativeHeadRowIndex(sheet.getWriteRelativeHeadRowIndex()); + } + // Compatible with old code + compatibleOldCode(sheet); + List handlerList = new ArrayList(); + if (sheet.getCustomWriteHandlerList() != null && !sheet.getCustomWriteHandlerList().isEmpty()) { + handlerList.addAll(sheet.getCustomWriteHandlerList()); + } + // Initialization Annotation + sheetHolder.initAnnotationConfig(handlerList); + + sheetHolder + .setWriteHandlerMap(sheetHolder.sortAndClearUpHandler(handlerList, workbookHolder.getWriteHandlerMap())); + Map converterMap = new HashMap(workbookHolder.getWriteConverterMap()); + if (sheet.getCustomConverterList() != null && !sheet.getCustomConverterList().isEmpty()) { + for (Converter converter : sheet.getCustomConverterList()) { + converterMap.put(converter.getClass(), converter); + } + } + sheetHolder.setWriteConverterMap(converterMap); + sheetHolder.setHasBeenInitializedTable(new HashMap()); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Sheet writeHandlerMap:{}", sheetHolder.getWriteHandlerMap()); + } + return sheetHolder; + } + + public static SheetHolder buildReadWorkSheetHolder(com.alibaba.excel.write.metadata.Sheet sheet, + WorkbookHolder workbookHolder) { + SheetHolder sheetHolder = buildBaseSheetHolder(sheet, workbookHolder); + if (sheet.getReadHeadRowNumber() == null) { + if (workbookHolder.getReadHeadRowNumber() == null) { + sheetHolder.setReadHeadRowNumber(sheetHolder.getExcelHeadProperty().getHeadRowNumber()); + } else { + sheetHolder.setReadHeadRowNumber(workbookHolder.getReadHeadRowNumber()); + } + } else { + sheetHolder.setReadHeadRowNumber(sheet.getReadHeadRowNumber()); + } + + Map converterMap = + new HashMap(workbookHolder.getReadConverterMap()); + if (sheet.getCustomConverterList() != null && !sheet.getCustomConverterList().isEmpty()) { + for (Converter converter : sheet.getCustomConverterList()) { + converterMap.put(ConverterKey.buildConverterKey(converter), converter); + } + } + sheetHolder.setReadConverterMap(converterMap); + + List readListenerList = new ArrayList(); + if (sheet.getCustomReadListenerList() != null && !sheet.getCustomReadListenerList().isEmpty()) { + readListenerList.addAll(sheet.getCustomReadListenerList()); + } + sheetHolder.setReadListenerList(readListenerList); + return sheetHolder; + } + + private static SheetHolder buildBaseSheetHolder(com.alibaba.excel.write.metadata.Sheet sheet, + WorkbookHolder workbookHolder) { + SheetHolder sheetHolder = new SheetHolder(); + sheetHolder.setSheetParam(sheet); + sheetHolder.setParentWorkBook(workbookHolder); + + boolean noHead = (sheet.getHead() == null || sheet.getHead().isEmpty()) && sheet.getClazz() == null; + if (noHead) { + // Use parent + sheetHolder.setHead(workbookHolder.getHead()); + sheetHolder.setClazz(workbookHolder.getClazz()); + } else { + sheetHolder.setHead(sheet.getHead()); + sheetHolder.setClazz(sheet.getClazz()); + } + + if (sheet.getAutoTrim() == null) { + workbookHolder.setAutoTrim(workbookHolder.getAutoTrim()); + } else { + workbookHolder.setAutoTrim(sheet.getNeedHead()); + } + // Initialization property + sheetHolder + .setExcelHeadProperty(new ExcelHeadProperty(sheetHolder.getClazz(), sheetHolder.getHead(), workbookHolder)); + return sheetHolder; + } + + /** + * Compatible with old code + */ + @Deprecated + private static void compatibleOldCode(com.alibaba.excel.write.metadata.Sheet sheet) { + if (sheet.getColumnWidthMap() != null && !sheet.getColumnWidthMap().isEmpty()) { + final Map columnWidthMap = sheet.getColumnWidthMap(); + if (sheet.getCustomWriteHandlerList() == null) { + sheet.setCustomWriteHandlerList(new ArrayList()); + } + sheet.getCustomWriteHandlerList().add(new AbstractHeadColumnWidthStyleStrategy() { + @Override + protected Integer columnWidth(Head head) { + if (columnWidthMap.containsKey(head.getColumnIndex())) { + columnWidthMap.get(head.getColumnIndex()); + } + return 20; + } + }); + } + if (sheet.getTableStyle() != null) { + final TableStyle tableStyle = sheet.getTableStyle(); + if (sheet.getCustomWriteHandlerList() == null) { + sheet.setCustomWriteHandlerList(new ArrayList()); + } + CellStyle headCellStyle = new CellStyle(); + headCellStyle.setFont(tableStyle.getTableHeadFont()); + headCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); + CellStyle contentCellStyle = new CellStyle(); + contentCellStyle.setFont(tableStyle.getTableContentFont()); + contentCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); + sheet.getCustomWriteHandlerList().add(new RowCellStyleStrategy(headCellStyle, contentCellStyle)); + } + } + + public Sheet getSheet() { + return sheet; + } + + public void setSheet(Sheet sheet) { + this.sheet = sheet; + } + + public Integer getSheetNo() { + return sheetNo; + } + + public void setSheetNo(Integer sheetNo) { + this.sheetNo = sheetNo; + } + + public String getSheetName() { + return sheetName; + } + + public void setSheetName(String sheetName) { + this.sheetName = sheetName; + } + + public WorkbookHolder getParentWorkBook() { + return parentWorkBook; + } + + public void setParentWorkBook(WorkbookHolder parentWorkBook) { + this.parentWorkBook = parentWorkBook; + } + + public Map getHasBeenInitializedTable() { + return hasBeenInitializedTable; + } + + public void setHasBeenInitializedTable(Map hasBeenInitializedTable) { + this.hasBeenInitializedTable = hasBeenInitializedTable; + } + + public com.alibaba.excel.write.metadata.Sheet getSheetParam() { + return sheetParam; + } + + public void setSheetParam(com.alibaba.excel.write.metadata.Sheet sheetParam) { + this.sheetParam = sheetParam; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/holder/write/TableHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/TableHolder.java similarity index 90% rename from src/main/java/com/alibaba/excel/metadata/holder/write/TableHolder.java rename to src/main/java/com/alibaba/excel/read/metadata/holder/TableHolder.java index bebc30f9..1116ff3b 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/write/TableHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/TableHolder.java @@ -1,4 +1,4 @@ -package com.alibaba.excel.metadata.holder.write; +package com.alibaba.excel.read.metadata.holder; import java.util.ArrayList; import java.util.HashMap; @@ -10,8 +10,9 @@ import org.slf4j.LoggerFactory; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.metadata.CellStyle; -import com.alibaba.excel.metadata.Table; +import com.alibaba.excel.write.metadata.Table; import com.alibaba.excel.metadata.TableStyle; +import com.alibaba.excel.write.metadata.holder.AbstractWriteConfiguration; import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.style.RowCellStyleStrategy; @@ -21,7 +22,7 @@ import com.alibaba.excel.write.style.RowCellStyleStrategy; * * @author zhuangjiaju */ -public class TableHolder extends AbstractWriteConfiguration { +public class ReadTableHolder extends AbstractReadHolder { private static final Logger LOGGER = LoggerFactory.getLogger(TableHolder.class); /*** @@ -35,10 +36,10 @@ public class TableHolder extends AbstractWriteConfiguration { /** * current table param */ - private com.alibaba.excel.metadata.Table tableParam; + private Table tableParam; - public static TableHolder buildWriteWorkTableHolder(com.alibaba.excel.metadata.Table table, SheetHolder sheetHolder, - WorkbookHolder workbookHolder) { + public static TableHolder buildWriteWorkTableHolder(Table table, SheetHolder sheetHolder, + WorkbookHolder workbookHolder) { TableHolder tableHolder = new TableHolder(); tableHolder.setTableParam(table); tableHolder.setParentSheet(sheetHolder); @@ -95,7 +96,7 @@ public class TableHolder extends AbstractWriteConfiguration { * Compatible with old code */ @Deprecated - private static void compatibleOldCode(com.alibaba.excel.metadata.Table table) { + private static void compatibleOldCode(Table table) { if (table.getTableStyle() != null) { final TableStyle tableStyle = table.getTableStyle(); if (table.getCustomWriteHandlerList() == null) { diff --git a/src/main/java/com/alibaba/excel/metadata/holder/write/WorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/WorkbookHolder.java similarity index 91% rename from src/main/java/com/alibaba/excel/metadata/holder/write/WorkbookHolder.java rename to src/main/java/com/alibaba/excel/read/metadata/holder/WorkbookHolder.java index c9a011f0..433bfc30 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/write/WorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/WorkbookHolder.java @@ -1,4 +1,4 @@ -package com.alibaba.excel.metadata.holder.write; +package com.alibaba.excel.read.metadata.holder; import java.io.File; import java.io.InputStream; @@ -17,10 +17,12 @@ import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.ConverterKey; import com.alibaba.excel.converters.DefaultConverterLoader; +import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.read.listener.ModelBuildEventListener; import com.alibaba.excel.read.listener.ReadListener; +import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.handler.DefaultWriteHandlerLoader; import com.alibaba.excel.write.handler.WriteHandler; @@ -30,35 +32,25 @@ import com.alibaba.excel.write.handler.WriteHandler; * * @author zhuangjiaju */ -public class WorkbookHolder extends AbstractWriteConfiguration { - private static final Logger LOGGER = LoggerFactory.getLogger(WorkbookHolder.class); +public class ReadWorkbookHolder extends AbstractReadHolder { + private static final Logger LOGGER = LoggerFactory.getLogger(ReadWorkbookHolder.class); /*** * poi Workbook */ private Workbook workbook; - /** - * prevent duplicate creation of sheet objects - */ - private Map hasBeenInitializedSheet; /** * current param */ - private com.alibaba.excel.metadata.Workbook workbookParam; + private ReadWorkbook readWorkbook; /** - * Final output stream - */ - private OutputStream outputStream; - /** - *
  • write: Template input stream - *
  • read: Read InputStream + * Read InputStream *

    * If 'inputStream' and 'file' all not empty,file first */ private InputStream inputStream; /** - *

  • write: Template file - *
  • read: Read file + * Read file *

    * If 'inputStream' and 'file' all not empty,file first */ @@ -81,12 +73,6 @@ public class WorkbookHolder extends AbstractWriteConfiguration { * A cache that stores temp data to save memory.Default use {@link com.alibaba.excel.cache.Ehcache} */ private ReadCache readCache; - /** - * true if date uses 1904 windowing, or false if using 1900 date windowing. - * - * @return - */ - private Boolean use1904windowing; /** * Mmandatory use 'inputStream' @@ -115,7 +101,7 @@ public class WorkbookHolder extends AbstractWriteConfiguration { @Deprecated private com.alibaba.excel.event.WriteHandler writeHandler; - public static WorkbookHolder buildWriteWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { + public static WorkbookHolder buildWriteWorkbookHolder(com.alibaba.excel.write.metadata.Workbook workbook) { WorkbookHolder workbookHolder = buildBaseWorkbookHolder(workbook); workbookHolder.setNewInitialization(Boolean.TRUE); if (workbook.getNeedHead() == null) { @@ -149,7 +135,7 @@ public class WorkbookHolder extends AbstractWriteConfiguration { return workbookHolder; } - public static WorkbookHolder buildReadWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { + public static WorkbookHolder buildReadWorkbookHolder(com.alibaba.excel.write.metadata.Workbook workbook) { WorkbookHolder workbookHolder = buildBaseWorkbookHolder(workbook); if (workbook.getFile() == null && workbookHolder.getInputStream() == null) { throw new ExcelAnalysisException("Read excel 'file' and 'inputStream' cannot be empty at the same time!"); @@ -175,7 +161,7 @@ public class WorkbookHolder extends AbstractWriteConfiguration { return workbookHolder; } - private static WorkbookHolder buildBaseWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { + private static WorkbookHolder buildBaseWorkbookHolder(com.alibaba.excel.write.metadata.Workbook workbook) { WorkbookHolder workbookHolder = new WorkbookHolder(); workbookHolder.setUse1904windowing(workbook.getUse1904windowing()); workbookHolder.setWorkbookParam(workbook); @@ -218,11 +204,11 @@ public class WorkbookHolder extends AbstractWriteConfiguration { this.hasBeenInitializedSheet = hasBeenInitializedSheet; } - public com.alibaba.excel.metadata.Workbook getWorkbookParam() { + public com.alibaba.excel.write.metadata.Workbook getWorkbookParam() { return workbookParam; } - public void setWorkbookParam(com.alibaba.excel.metadata.Workbook workbookParam) { + public void setWorkbookParam(com.alibaba.excel.write.metadata.Workbook workbookParam) { this.workbookParam = workbookParam; } @@ -321,4 +307,9 @@ public class WorkbookHolder extends AbstractWriteConfiguration { public void setReadTempFile(File readTempFile) { this.readTempFile = readTempFile; } + + @Override + public HolderEnum holderType() { + return HolderEnum.WORKBOOK; + } } diff --git a/src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java b/src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java new file mode 100644 index 00000000..fdc5562d --- /dev/null +++ b/src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java @@ -0,0 +1,17 @@ +package com.alibaba.excel.read.metadata.property; + +import java.util.List; + +import com.alibaba.excel.metadata.property.ExcelHeadProperty; + +/** + * Define the header attribute of excel + * + * @author jipengfei + */ +public class ExcelReadHeadProperty extends ExcelHeadProperty { + + public ExcelReadHeadProperty(Class headClazz, List> head, Boolean convertAllFiled) { + super(headClazz, head, convertAllFiled); + } +} diff --git a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java index 650c2a78..a1da95ab 100644 --- a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java +++ b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java @@ -13,7 +13,7 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; import com.alibaba.excel.support.ExcelTypeEnum; /** @@ -42,7 +42,7 @@ public class WorkBookUtil { return new HSSFWorkbook(); } - public static Sheet createSheet(Workbook workbook, com.alibaba.excel.metadata.Sheet sheet) { + public static Sheet createSheet(Workbook workbook, com.alibaba.excel.write.metadata.Sheet sheet) { return workbook.createSheet(sheet.getSheetName() != null ? sheet.getSheetName() : sheet.getSheetNo() + ""); } diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilder.java b/src/main/java/com/alibaba/excel/write/ExcelBuilder.java index 28e09731..9b247281 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilder.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilder.java @@ -2,8 +2,8 @@ package com.alibaba.excel.write; import java.util.List; -import com.alibaba.excel.metadata.Sheet; -import com.alibaba.excel.metadata.Table; +import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.write.metadata.Table; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; /** diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 4f1c82ea..1f83d3ab 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -15,9 +15,9 @@ import com.alibaba.excel.converters.Converter; import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.Sheet; -import com.alibaba.excel.metadata.Table; -import com.alibaba.excel.metadata.holder.write.WriteConfiguration; +import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.write.metadata.Table; +import com.alibaba.excel.write.metadata.holder.WriteConfiguration; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.POITempFile; @@ -25,6 +25,7 @@ import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.WriteHandler; +import com.alibaba.excel.write.metadata.Workbook; import net.sf.cglib.beans.BeanMap; @@ -35,7 +36,7 @@ public class ExcelBuilderImpl implements ExcelBuilder { private WriteContext context; - public ExcelBuilderImpl(com.alibaba.excel.metadata.Workbook workbook) { + public ExcelBuilderImpl(Workbook workbook) { // 初始化时候创建临时缓存目录,用于规避POI在并发写bug POITempFile.createPOIFilesDirectory(); context = new WriteContextImpl(workbook); diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java index df8c84bb..b3aaa51e 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java @@ -13,7 +13,7 @@ import java.util.List; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.exception.ExcelGenerateException; -import com.alibaba.excel.metadata.Workbook; +import com.alibaba.excel.write.metadata.Workbook; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.handler.WriteHandler; diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java index 9ac3e62c..c75466d9 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java @@ -5,7 +5,7 @@ import java.util.HashMap; import java.util.List; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; import com.alibaba.excel.write.handler.WriteHandler; /** diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java index faf13928..640ef50a 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java @@ -5,7 +5,7 @@ import java.util.HashMap; import java.util.List; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.metadata.Table; +import com.alibaba.excel.write.metadata.Table; import com.alibaba.excel.write.handler.WriteHandler; /** diff --git a/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java index b62c6e68..534a45b0 100644 --- a/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java @@ -4,8 +4,8 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; import com.sun.istack.internal.Nullable; /** diff --git a/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java index 75f086c5..50e79c35 100644 --- a/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java @@ -2,8 +2,8 @@ package com.alibaba.excel.write.handler; import org.apache.poi.ss.usermodel.Row; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; import com.sun.istack.internal.Nullable; /** diff --git a/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java index f69b909f..9405e849 100644 --- a/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java @@ -1,7 +1,7 @@ package com.alibaba.excel.write.handler; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; /** * intercepts handle sheet creation diff --git a/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java index 846a8995..2db28872 100644 --- a/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java @@ -1,6 +1,6 @@ package com.alibaba.excel.write.handler; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; /** * intercepts handle Workbook creation diff --git a/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java b/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java index ecc45659..05a1522b 100644 --- a/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java +++ b/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java @@ -5,8 +5,8 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; import com.alibaba.excel.write.handler.CellWriteHandler; /** diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java b/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java new file mode 100644 index 00000000..de11991b --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java @@ -0,0 +1,51 @@ +package com.alibaba.excel.write.metadata; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.excel.metadata.BasicParameter; +import com.alibaba.excel.write.handler.WriteHandler; + +/** + * Write basic parameter + * + * @author zhuangjiaju + **/ +public class WriteBasicParameter extends BasicParameter { + /** + * Writes the head relative to the existing contents of the sheet. Indexes are zero-based. + */ + private Integer relativeHeadRowIndex; + /** + * Need Head + */ + private Boolean needHead; + /** + * Custom type handler override the default + */ + private List customWriteHandlerList = new ArrayList(); + + public Integer getRelativeHeadRowIndex() { + return relativeHeadRowIndex; + } + + public void setRelativeHeadRowIndex(Integer relativeHeadRowIndex) { + this.relativeHeadRowIndex = relativeHeadRowIndex; + } + + public Boolean getNeedHead() { + return needHead; + } + + public void setNeedHead(Boolean needHead) { + this.needHead = needHead; + } + + public List getCustomWriteHandlerList() { + return customWriteHandlerList; + } + + public void setCustomWriteHandlerList(List customWriteHandlerList) { + this.customWriteHandlerList = customWriteHandlerList; + } +} diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java new file mode 100644 index 00000000..8a7dcc31 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java @@ -0,0 +1,69 @@ +package com.alibaba.excel.write.metadata; + +import java.util.HashMap; +import java.util.Map; + +import com.alibaba.excel.metadata.TableStyle; +import com.alibaba.excel.write.style.RowCellStyleStrategy; +import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy; + +/** + * Write sheet + * + * @author jipengfei + */ +public class WriteSheet extends WriteBasicParameter { + /** + * Starting from 0 + */ + private Integer sheetNo; + /** + * sheet name + */ + private String sheetName; + /** + * column with + * + * @deprecated please use {@link SimpleColumnWidthStyleStrategy} + */ + @Deprecated + private Map columnWidthMap = new HashMap(); + /** + * + * @deprecated please use{@link RowCellStyleStrategy} + */ + @Deprecated + private TableStyle tableStyle; + + public Integer getSheetNo() { + return sheetNo; + } + + public void setSheetNo(Integer sheetNo) { + this.sheetNo = sheetNo; + } + + public String getSheetName() { + return sheetName; + } + + public void setSheetName(String sheetName) { + this.sheetName = sheetName; + } + + public Map getColumnWidthMap() { + return columnWidthMap; + } + + public void setColumnWidthMap(Map columnWidthMap) { + this.columnWidthMap = columnWidthMap; + } + + public TableStyle getTableStyle() { + return tableStyle; + } + + public void setTableStyle(TableStyle tableStyle) { + this.tableStyle = tableStyle; + } +} diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java b/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java new file mode 100644 index 00000000..12217123 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteTable.java @@ -0,0 +1,38 @@ +package com.alibaba.excel.write.metadata; + +import com.alibaba.excel.metadata.TableStyle; +import com.alibaba.excel.write.style.RowCellStyleStrategy; + +/** + * table + * + * @author jipengfei + */ +public class WriteTable extends WriteBasicParameter { + /** + * Starting from 0 + */ + private Integer tableNo; + /** + * + * @deprecated please use{@link RowCellStyleStrategy} + */ + @Deprecated + private TableStyle tableStyle; + + public Integer getTableNo() { + return tableNo; + } + + public void setTableNo(Integer tableNo) { + this.tableNo = tableNo; + } + + public TableStyle getTableStyle() { + return tableStyle; + } + + public void setTableStyle(TableStyle tableStyle) { + this.tableStyle = tableStyle; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/Workbook.java b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java similarity index 53% rename from src/main/java/com/alibaba/excel/metadata/Workbook.java rename to src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java index 3dd7f339..3a587270 100644 --- a/src/main/java/com/alibaba/excel/metadata/Workbook.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java @@ -1,12 +1,9 @@ -package com.alibaba.excel.metadata; +package com.alibaba.excel.write.metadata; import java.io.File; import java.io.InputStream; import java.io.OutputStream; -import com.alibaba.excel.cache.ReadCache; -import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.handler.WriteHandler; @@ -15,14 +12,13 @@ import com.alibaba.excel.write.handler.WriteHandler; * * @author zhuangjiaju **/ -public class Workbook extends BasicParameter { +public class WriteWorkbook extends WriteBasicParameter { /** * Excel type */ private ExcelTypeEnum excelType; - /** - * Read final output stream + * Final output stream */ private OutputStream outputStream; /** @@ -31,7 +27,7 @@ public class Workbook extends BasicParameter { *

    * If 'inputStream' and 'file' all not empty,file first */ - private InputStream inputStream; + private InputStream templateInputStream; /** *

  • write: Template file @@ -39,30 +35,19 @@ public class Workbook extends BasicParameter { *

    * If 'inputStream' and 'file' all not empty,file first */ - private File file; + private File templateFile; /** * Default true */ private Boolean autoCloseStream; /** - * This object can be read in the Listener {@link AnalysisEventListener#invoke(Object, AnalysisContext)} - * {@link AnalysisContext#getCustom()} - * - */ - private Object readCustomObject; - /** - * A cache that stores temp data to save memory.Default use {@link com.alibaba.excel.cache.Ehcache} + * Mandatory use 'inputStream' .Default is false */ - private ReadCache readCache; + private Boolean mandatoryUseInputStream; /** - * true if date uses 1904 windowing, or false if using 1900 date windowing. - * - * @return - */ - private Boolean use1904windowing; - /** - * The default is all excel objects.if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a - * field. if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed. + * The default is all excel objects.Default is true. + *

  • if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a field. + *
  • if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed. * * @deprecated Just to be compatible with historical data, The default is always going to be convert all filed. */ @@ -92,28 +77,20 @@ public class Workbook extends BasicParameter { this.outputStream = outputStream; } - public InputStream getInputStream() { - return inputStream; + public InputStream getTemplateInputStream() { + return templateInputStream; } - public void setInputStream(InputStream inputStream) { - this.inputStream = inputStream; + public void setTemplateInputStream(InputStream templateInputStream) { + this.templateInputStream = templateInputStream; } - public File getFile() { - return file; + public File getTemplateFile() { + return templateFile; } - public void setFile(File file) { - this.file = file; - } - - public com.alibaba.excel.event.WriteHandler getWriteHandler() { - return writeHandler; - } - - public void setWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) { - this.writeHandler = writeHandler; + public void setTemplateFile(File templateFile) { + this.templateFile = templateFile; } public Boolean getAutoCloseStream() { @@ -124,35 +101,27 @@ public class Workbook extends BasicParameter { this.autoCloseStream = autoCloseStream; } - public Boolean getConvertAllFiled() { - return convertAllFiled; - } - - public void setConvertAllFiled(Boolean convertAllFiled) { - this.convertAllFiled = convertAllFiled; - } - - public Object getReadCustomObject() { - return readCustomObject; + public Boolean getMandatoryUseInputStream() { + return mandatoryUseInputStream; } - public void setReadCustomObject(Object readCustomObject) { - this.readCustomObject = readCustomObject; + public void setMandatoryUseInputStream(Boolean mandatoryUseInputStream) { + this.mandatoryUseInputStream = mandatoryUseInputStream; } - public ReadCache getReadCache() { - return readCache; + public Boolean getConvertAllFiled() { + return convertAllFiled; } - public void setReadCache(ReadCache readCache) { - this.readCache = readCache; + public void setConvertAllFiled(Boolean convertAllFiled) { + this.convertAllFiled = convertAllFiled; } - public Boolean getUse1904windowing() { - return use1904windowing; + public com.alibaba.excel.event.WriteHandler getWriteHandler() { + return writeHandler; } - public void setUse1904windowing(Boolean use1904windowing) { - this.use1904windowing = use1904windowing; + public void setWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) { + this.writeHandler = writeHandler; } } diff --git a/src/main/java/com/alibaba/excel/metadata/holder/write/AbstractWriteConfiguration.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java similarity index 51% rename from src/main/java/com/alibaba/excel/metadata/holder/write/AbstractWriteConfiguration.java rename to src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index b04c5e3b..9c739ddd 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/write/AbstractWriteConfiguration.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -1,4 +1,4 @@ -package com.alibaba.excel.metadata.holder.write; +package com.alibaba.excel.write.metadata.holder; import java.util.ArrayList; import java.util.HashMap; @@ -11,360 +11,193 @@ import java.util.TreeMap; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; -import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.converters.ConverterKey; +import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.enums.HeadKindEnum; -import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.excel.metadata.holder.read.ReadConfiguration; -import com.alibaba.excel.read.listener.event.AnalysisFinishEvent; import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.event.Order; -import com.alibaba.excel.exception.ExcelAnalysisException; -import com.alibaba.excel.exception.ExcelDataConvertException; -import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.AbstractHolder; import com.alibaba.excel.metadata.CellStyle; import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.TableStyle; import com.alibaba.excel.metadata.property.CellStyleProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty; -import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.RowHeightProperty; -import com.alibaba.excel.read.listener.ReadListener; -import com.alibaba.excel.read.listener.ReadListenerRegistryCenter; -import com.alibaba.excel.util.StringUtils; 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.metadata.WriteBasicParameter; +import com.alibaba.excel.write.metadata.WriteSheet; +import com.alibaba.excel.write.metadata.WriteTable; +import com.alibaba.excel.write.property.ExcelWriteHeadProperty; import com.alibaba.excel.write.style.AbstractColumnCellStyleStrategy; +import com.alibaba.excel.write.style.RowCellStyleStrategy; import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy; +import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; /** - * sheet holder + * Write holder configuration * * @author zhuangjiaju */ -public abstract class AbstractWriteConfiguration - implements WriteConfiguration, ReadConfiguration, ReadListenerRegistryCenter { +public abstract class AbstractWriteHolder extends AbstractHolder implements WriteHolder { /** * Need Head */ private Boolean needHead; - /** - * Write handler for workbook - */ - private Map, List> writeHandlerMap; - /** - * Read listener - */ - private List readListenerList; - /** - * Converter for workbook - */ - private Map readConverterMap; - /** - * Converter for workbook - */ - private Map writeConverterMap; - /** - * Excel head property - */ - private ExcelHeadProperty excelHeadProperty; /** * Writes the head relative to the existing contents of the sheet. Indexes are zero-based. */ - private Integer writeRelativeHeadRowIndex; - /** - * Record whether it's new or from cache - */ - private Boolean newInitialization; + private Integer relativeHeadRowIndex; /** - * Count the number of added heads when read sheet. - * - *
  • 0 - This Sheet has no head ,since the first row are the data - *
  • 1 - This Sheet has one row head , this is the default - *
  • 2 - This Sheet has two row head ,since the third row is the data - */ - private Integer readHeadRowNumber; - /** - * You can only choose one of the {@link AbstractWriteConfiguration#head} and - * {@link AbstractWriteConfiguration#clazz} + * Excel head property */ - private List> head; + private ExcelWriteHeadProperty excelWriteHeadProperty; /** - * You can only choose one of the {@link AbstractWriteConfiguration#head} and - * {@link AbstractWriteConfiguration#clazz} + * Write handler for workbook */ - private Class clazz; + private Map, List> writeHandlerMap; /** - * Automatic trim includes sheet name and content + * Converter for workbook */ - private Boolean autoTrim; - - public Boolean getNeedHead() { - return needHead; - } - - public void setNeedHead(Boolean needHead) { - this.needHead = needHead; - } - - public Map, List> getWriteHandlerMap() { - return writeHandlerMap; - } - - public void setWriteHandlerMap(Map, List> writeHandlerMap) { - this.writeHandlerMap = writeHandlerMap; - } - - public ExcelHeadProperty getExcelHeadProperty() { - return excelHeadProperty; - } - - public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { - this.excelHeadProperty = excelHeadProperty; - } - - public Integer getWriteRelativeHeadRowIndex() { - return writeRelativeHeadRowIndex; - } - - public void setWriteRelativeHeadRowIndex(Integer writeRelativeHeadRowIndex) { - this.writeRelativeHeadRowIndex = writeRelativeHeadRowIndex; - } - - public Boolean getNewInitialization() { - return newInitialization; - } - - public void setNewInitialization(Boolean newInitialization) { - this.newInitialization = newInitialization; - } - - public List> getHead() { - return head; - } - - public void setHead(List> head) { - this.head = head; - } - - public Class getClazz() { - return clazz; - } - - public void setClazz(Class clazz) { - this.clazz = clazz; - } - - public Boolean getAutoTrim() { - return autoTrim; - } - - public void setAutoTrim(Boolean autoTrim) { - this.autoTrim = autoTrim; - } - - public List getReadListenerList() { - return readListenerList; - } - - public void setReadListenerList(List readListenerList) { - this.readListenerList = readListenerList; - } - - public Map getReadConverterMap() { - return readConverterMap; - } - - public void setReadConverterMap(Map readConverterMap) { - this.readConverterMap = readConverterMap; - } - - public Map getWriteConverterMap() { - return writeConverterMap; - } + private Map converterMap; + + public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder, + Boolean convertAllFiled) { + super(writeBasicParameter, parentAbstractWriteHolder); + if (writeBasicParameter.getUse1904windowing() == null) { + if (parentAbstractWriteHolder == null) { + getGlobalConfiguration().setUse1904windowing(Boolean.FALSE); + } else { + getGlobalConfiguration() + .setUse1904windowing(parentAbstractWriteHolder.getGlobalConfiguration().getUse1904windowing()); + } + } else { + getGlobalConfiguration().setUse1904windowing(writeBasicParameter.getUse1904windowing()); + } - public void setWriteConverterMap(Map writeConverterMap) { - this.writeConverterMap = writeConverterMap; - } + if (writeBasicParameter.getNeedHead() == null) { + if (parentAbstractWriteHolder == null) { + this.needHead = Boolean.TRUE; + } else { + this.needHead = parentAbstractWriteHolder.getNeedHead(); + } + } else { + this.needHead = writeBasicParameter.getNeedHead(); + } - public Integer getReadHeadRowNumber() { - return readHeadRowNumber; - } + if (writeBasicParameter.getRelativeHeadRowIndex() == null) { + if (parentAbstractWriteHolder == null) { + this.relativeHeadRowIndex = 0; + } else { + this.relativeHeadRowIndex = parentAbstractWriteHolder.getRelativeHeadRowIndex(); + } + } else { + this.relativeHeadRowIndex = writeBasicParameter.getRelativeHeadRowIndex(); + } - public void setReadHeadRowNumber(Integer readHeadRowNumber) { - this.readHeadRowNumber = readHeadRowNumber; - } + // Initialization property + this.excelWriteHeadProperty = new ExcelWriteHeadProperty(getClazz(), getHead(), convertAllFiled); - @Override - public void register(AnalysisEventListener listener) { - readListenerList.add(listener); - } + // Compatible with old code + compatibleOldCode(writeBasicParameter); - @Override - public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) { - List cellDataList = (List)event.getAnalysisResult(); - if (analysisContext.currentRowNum() > analysisContext.currentSheetHolder().getReadHeadRowNumber()) { - for (ReadListener readListener : readListenerList) { - try { - readListener.invoke(analysisContext.currentRowAnalysisResult(), analysisContext); - } catch (Exception e) { - for (ReadListener readListenerException : readListenerList) { - try { - readListenerException.onException(e, analysisContext); - } catch (Exception exception) { - throw new ExcelAnalysisException("Listen error!", exception); - } - } - } - } - } - // Now is header - if (analysisContext.currentRowNum().equals(analysisContext.currentSheetHolder().getReadHeadRowNumber())) { - buildHead(analysisContext, cellDataList); + // Set writeHandlerMap + List handlerList = new ArrayList(); + if (writeBasicParameter.getCustomWriteHandlerList() != null + && !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) { + handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList()); } - } + // Initialization Annotation + initAnnotationConfig(handlerList); - @Override - public void notifyAfterAllAnalysed(AnalysisContext analysisContext) { - for (ReadListener readListener : readListenerList) { - readListener.doAfterAllAnalysed(analysisContext); + Map, List> parentWriteHandlerMap = null; + if (parentAbstractWriteHolder != null) { + parentWriteHandlerMap = parentAbstractWriteHolder.getWriteHandlerMap(); } - } + this.writeHandlerMap = sortAndClearUpHandler(handlerList, parentWriteHandlerMap); - private void buildHead(AnalysisContext analysisContext, List cellDataList) { - if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelHeadProperty().getHeadKind())) { - return; + // Set converterMap + if (parentAbstractWriteHolder == null) { + this.converterMap = DefaultConverterLoader.loadDefaultWriteConverter(); + } else { + this.converterMap = new HashMap(parentAbstractWriteHolder.getConverterMap()); } - List dataList = (List)buildStringList(cellDataList, analysisContext.currentConfiguration()); - ExcelHeadProperty excelHeadPropertyData = analysisContext.currentConfiguration().excelHeadProperty(); - Map headMapData = excelHeadPropertyData.getHeadMap(); - Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); - Map tmpHeadMap = new HashMap(headMapData.size() * 4 / 3 + 1); - Map tmpContentPropertyMap = - new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); - for (Map.Entry entry : headMapData.entrySet()) { - Head headData = entry.getValue(); - if (headData.getForceIndex()) { - tmpHeadMap.put(entry.getKey(), headData); - tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); - continue; - } - String headName = headData.getHeadNameList().get(0); - for (int i = 0; i < dataList.size(); i++) { - String headString = dataList.get(i); - if (StringUtils.isEmpty(headString)) { - continue; - } - if (analysisContext.currentSheetHolder().getAutoTrim()) { - headString = headString.trim(); - } - if (headName.equals(headString)) { - headData.setColumnIndex(i); - tmpHeadMap.put(i, headData); - tmpContentPropertyMap.put(i, contentPropertyMapData.get(entry.getKey())); - break; - } + if (writeBasicParameter.getCustomConverterList() != null + && !writeBasicParameter.getCustomConverterList().isEmpty()) { + for (Converter converter : writeBasicParameter.getCustomConverterList()) { + converterMap.put(converter.getClass(), converter); } } - excelHeadPropertyData.setHeadMap(tmpHeadMap); - excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); } - private Object buildStringList(List data, ReadConfiguration readConfiguration) { - List list = new ArrayList(); - for (CellData cellData : data) { - Converter converter = readConfiguration.readConverterMap() - .get(ConverterKey.buildConverterKey(String.class, cellData.getType())); - if (converter == null) { - throw new ExcelDataConvertException( - "Converter not found, convert " + cellData.getType() + " to String"); - } - try { - list.add((String)(converter.convertToJavaData(cellData, null))); - } catch (Exception e) { - throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e); - } + /** + * Compatible with old code + */ + @Deprecated + private void compatibleOldCode(WriteBasicParameter writeBasicParameter) { + switch (holderType()) { + case SHEET: + compatibleOldCodeCreateRowCellStyleStrategy(writeBasicParameter, + ((WriteSheet)writeBasicParameter).getTableStyle()); + compatibleOldCodeCreateHeadColumnWidthStyleStrategy(writeBasicParameter, + ((WriteSheet)writeBasicParameter).getColumnWidthMap()); + return; + case TABLE: + compatibleOldCodeCreateRowCellStyleStrategy(writeBasicParameter, + ((WriteTable)writeBasicParameter).getTableStyle()); + return; + default: } - return list; } - protected Map, List> sortAndClearUpHandler( - List handlerList, Map, List> parentHandlerMap) { - // add - if (parentHandlerMap != null) { - for (List parentHandlerList : parentHandlerMap.values()) { - handlerList.addAll(parentHandlerList); - } + @Deprecated + private void compatibleOldCodeCreateRowCellStyleStrategy(WriteBasicParameter writeBasicParameter, + TableStyle tableStyle) { + if (tableStyle == null) { + return; } - // sort - Map> orderExcelWriteHandlerMap = new TreeMap>(); - for (WriteHandler handler : handlerList) { - int order = Integer.MIN_VALUE; - if (handler instanceof Order) { - order = ((Order)handler).order(); - } - if (orderExcelWriteHandlerMap.containsKey(order)) { - orderExcelWriteHandlerMap.get(order).add(handler); - } else { - List tempHandlerList = new ArrayList(); - tempHandlerList.add(handler); - orderExcelWriteHandlerMap.put(order, tempHandlerList); - } + if (writeBasicParameter.getCustomWriteHandlerList() == null) { + writeBasicParameter.setCustomWriteHandlerList(new ArrayList()); } - // clean up - Set alreadyExistedHandlerSet = new HashSet(); - List cleanUpHandlerList = new ArrayList(); - for (Map.Entry> entry : orderExcelWriteHandlerMap.entrySet()) { - for (WriteHandler handler : entry.getValue()) { - if (handler instanceof NotRepeatExecutor) { - String uniqueValue = ((NotRepeatExecutor)handler).uniqueValue(); - if (alreadyExistedHandlerSet.contains(uniqueValue)) { - continue; - } - alreadyExistedHandlerSet.add(uniqueValue); - } - cleanUpHandlerList.add(handler); - } + CellStyle headCellStyle = new CellStyle(); + headCellStyle.setFont(tableStyle.getTableHeadFont()); + headCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); + CellStyle contentCellStyle = new CellStyle(); + contentCellStyle.setFont(tableStyle.getTableContentFont()); + contentCellStyle.setIndexedColors(tableStyle.getTableContentBackGroundColor()); + writeBasicParameter.getCustomWriteHandlerList().add(new RowCellStyleStrategy(headCellStyle, contentCellStyle)); + } + + @Deprecated + private void compatibleOldCodeCreateHeadColumnWidthStyleStrategy(WriteBasicParameter writeBasicParameter, + final Map columnWidthMap) { + if (columnWidthMap == null || columnWidthMap.isEmpty()) { + return; } - // classify - Map, List> result = - new HashMap, List>(); - 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()); - for (WriteHandler writeHandler : cleanUpHandlerList) { - if (writeHandler instanceof CellWriteHandler) { - result.get(CellWriteHandler.class).add(writeHandler); - } - if (writeHandler instanceof RowWriteHandler) { - result.get(RowWriteHandler.class).add(writeHandler); - } - if (writeHandler instanceof SheetWriteHandler) { - result.get(SheetWriteHandler.class).add(writeHandler); - } - if (writeHandler instanceof SheetWriteHandler) { - result.get(SheetWriteHandler.class).add(writeHandler); - } - if (writeHandler instanceof WorkbookWriteHandler) { - result.get(WorkbookWriteHandler.class).add(writeHandler); - } - result.get(WriteHandler.class).add(writeHandler); + if (writeBasicParameter.getCustomWriteHandlerList() == null) { + writeBasicParameter.setCustomWriteHandlerList(new ArrayList()); } - return result; + writeBasicParameter.getCustomWriteHandlerList().add(new AbstractHeadColumnWidthStyleStrategy() { + @Override + protected Integer columnWidth(Head head) { + if (columnWidthMap.containsKey(head.getColumnIndex())) { + columnWidthMap.get(head.getColumnIndex()); + } + return 20; + } + }); } protected void initAnnotationConfig(List handlerList) { - if (!HeadKindEnum.CLASS.equals(getExcelHeadProperty().getHeadKind())) { + if (!HeadKindEnum.CLASS.equals(getExcelWriteHeadProperty().getHeadKind())) { return; } - Map headMap = getExcelHeadProperty().getHeadMap(); - Map contentPropertyMap = getExcelHeadProperty().getContentPropertyMap(); + Map headMap = getExcelWriteHeadProperty().getHeadMap(); + Map contentPropertyMap = getExcelWriteHeadProperty().getContentPropertyMap(); boolean hasCellStyle = false; boolean hasColumnWidth = false; @@ -391,8 +224,8 @@ public abstract class AbstractWriteConfiguration } private void dealRowHigh(List handlerList, Map contentPropertyMap) { - RowHeightProperty headRowHeightProperty = excelHeadProperty.getHeadRowHeightProperty(); - RowHeightProperty contentRowHeightProperty = excelHeadProperty.getContentRowHeightProperty(); + RowHeightProperty headRowHeightProperty = getExcelWriteHeadProperty().getHeadRowHeightProperty(); + RowHeightProperty contentRowHeightProperty = getExcelWriteHeadProperty().getContentRowHeightProperty(); if (headRowHeightProperty == null && contentRowHeightProperty == null) { return; } @@ -452,43 +285,135 @@ public abstract class AbstractWriteConfiguration handlerList.add(columnCellStyleStrategy); } - @Override - public Map, List> writeHandlerMap() { - return getWriteHandlerMap(); + protected Map, List> sortAndClearUpHandler( + List handlerList, Map, List> parentHandlerMap) { + // add + if (parentHandlerMap != null) { + for (List parentHandlerList : parentHandlerMap.values()) { + handlerList.addAll(parentHandlerList); + } + } + // sort + Map> orderExcelWriteHandlerMap = new TreeMap>(); + for (WriteHandler handler : handlerList) { + int order = Integer.MIN_VALUE; + if (handler instanceof Order) { + order = ((Order)handler).order(); + } + if (orderExcelWriteHandlerMap.containsKey(order)) { + orderExcelWriteHandlerMap.get(order).add(handler); + } else { + List tempHandlerList = new ArrayList(); + tempHandlerList.add(handler); + orderExcelWriteHandlerMap.put(order, tempHandlerList); + } + } + // clean up + Set alreadyExistedHandlerSet = new HashSet(); + List cleanUpHandlerList = new ArrayList(); + for (Map.Entry> entry : orderExcelWriteHandlerMap.entrySet()) { + for (WriteHandler handler : entry.getValue()) { + if (handler instanceof NotRepeatExecutor) { + String uniqueValue = ((NotRepeatExecutor)handler).uniqueValue(); + if (alreadyExistedHandlerSet.contains(uniqueValue)) { + continue; + } + alreadyExistedHandlerSet.add(uniqueValue); + } + cleanUpHandlerList.add(handler); + } + } + // classify + Map, List> result = + new HashMap, List>(); + 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()); + for (WriteHandler writeHandler : cleanUpHandlerList) { + if (writeHandler instanceof CellWriteHandler) { + result.get(CellWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof RowWriteHandler) { + result.get(RowWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof SheetWriteHandler) { + result.get(SheetWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof SheetWriteHandler) { + result.get(SheetWriteHandler.class).add(writeHandler); + } + if (writeHandler instanceof WorkbookWriteHandler) { + result.get(WorkbookWriteHandler.class).add(writeHandler); + } + result.get(WriteHandler.class).add(writeHandler); + } + return result; } - @Override - public boolean needHead() { - return getNeedHead(); + public Boolean getNeedHead() { + return needHead; } - @Override - public int writeRelativeHeadRowIndex() { - return getWriteRelativeHeadRowIndex(); + public void setNeedHead(Boolean needHead) { + this.needHead = needHead; + } + + public Map, List> getWriteHandlerMap() { + return writeHandlerMap; + } + + public void setWriteHandlerMap(Map, List> writeHandlerMap) { + this.writeHandlerMap = writeHandlerMap; + } + + public Map getConverterMap() { + return converterMap; + } + + public void setConverterMap(Map converterMap) { + this.converterMap = converterMap; + } + + public ExcelWriteHeadProperty getExcelWriteHeadProperty() { + return excelWriteHeadProperty; + } + + public void setExcelWriteHeadProperty(ExcelWriteHeadProperty excelWriteHeadProperty) { + this.excelWriteHeadProperty = excelWriteHeadProperty; + } + + public Integer getRelativeHeadRowIndex() { + return relativeHeadRowIndex; + } + + public void setRelativeHeadRowIndex(Integer relativeHeadRowIndex) { + this.relativeHeadRowIndex = relativeHeadRowIndex; } @Override - public ExcelHeadProperty excelHeadProperty() { - return getExcelHeadProperty(); + public ExcelWriteHeadProperty excelWriteHeadProperty() { + return getExcelWriteHeadProperty(); } @Override - public boolean isNew() { - return getNewInitialization(); + public Map, List> writeHandlerMap() { + return getWriteHandlerMap(); } @Override - public List readListenerList() { - return getReadListenerList(); + public Map converterMap() { + return getConverterMap(); } @Override - public Map readConverterMap() { - return getReadConverterMap(); + public boolean needHead() { + return getNeedHead(); } @Override - public Map writeConverterMap() { - return getWriteConverterMap(); + public int relativeHeadRowIndex() { + return getRelativeHeadRowIndex(); } } diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/UTils.java b/src/main/java/com/alibaba/excel/write/metadata/holder/UTils.java new file mode 100644 index 00000000..3838168c --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/UTils.java @@ -0,0 +1,86 @@ +package com.alibaba.excel.write.metadata.holder; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKey; +import com.alibaba.excel.enums.HeadKindEnum; +import com.alibaba.excel.exception.ExcelDataConvertException; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.read.metadata.read.ReadConfiguration; +import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.metadata.property.ExcelHeadProperty; +import com.alibaba.excel.util.StringUtils; + +/** + * TODO + * + * @author 罗成 + **/ +public class UTils { + + + private void buildHead(AnalysisContext analysisContext, List cellDataList) { + if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelHeadProperty().getHeadKind())) { + return; + } + List dataList = (List)buildStringList(cellDataList, analysisContext.currentConfiguration()); + ExcelHeadProperty excelHeadPropertyData = analysisContext.currentConfiguration().excelHeadProperty(); + Map headMapData = excelHeadPropertyData.getHeadMap(); + Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); + Map tmpHeadMap = new HashMap(headMapData.size() * 4 / 3 + 1); + Map tmpContentPropertyMap = + new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); + for (Map.Entry entry : headMapData.entrySet()) { + Head headData = entry.getValue(); + if (headData.getForceIndex()) { + tmpHeadMap.put(entry.getKey(), headData); + tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); + continue; + } + String headName = headData.getHeadNameList().get(0); + for (int i = 0; i < dataList.size(); i++) { + String headString = dataList.get(i); + if (StringUtils.isEmpty(headString)) { + continue; + } + if (analysisContext.currentSheetHolder().getAutoTrim()) { + headString = headString.trim(); + } + if (headName.equals(headString)) { + headData.setColumnIndex(i); + tmpHeadMap.put(i, headData); + tmpContentPropertyMap.put(i, contentPropertyMapData.get(entry.getKey())); + break; + } + } + } + excelHeadPropertyData.setHeadMap(tmpHeadMap); + excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); + } + + private Object buildStringList(List data, ReadConfiguration readConfiguration) { + List list = new ArrayList(); + for (CellData cellData : data) { + Converter converter = readConfiguration.readConverterMap() + .get(ConverterKey.buildConverterKey(String.class, cellData.getType())); + if (converter == null) { + throw new ExcelDataConvertException( + "Converter not found, convert " + cellData.getType() + " to String"); + } + try { + list.add((String)(converter.convertToJavaData(cellData, null))); + } catch (Exception e) { + throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e); + } + } + return list; + } + + +} diff --git a/src/main/java/com/alibaba/excel/metadata/holder/write/WriteConfiguration.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java similarity index 61% rename from src/main/java/com/alibaba/excel/metadata/holder/write/WriteConfiguration.java rename to src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java index 68401b97..1cf3c074 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/write/WriteConfiguration.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java @@ -1,19 +1,24 @@ -package com.alibaba.excel.metadata.holder.write; +package com.alibaba.excel.write.metadata.holder; import java.util.List; import java.util.Map; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.metadata.property.ExcelHeadProperty; +import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.write.handler.WriteHandler; +import com.alibaba.excel.write.property.ExcelWriteHeadProperty; /** * - * Get the corresponding configuration + * Get the corresponding Holder * * @author zhuangjiaju **/ -public interface WriteConfiguration { +public interface WriteHolder extends Holder { + /** + * What 'ExcelWriteHeadProperty' does the currently operated cell need to execute + */ + ExcelWriteHeadProperty excelWriteHeadProperty(); /** * What handler does the currently operated cell need to execute @@ -27,7 +32,7 @@ public interface WriteConfiguration { * * @return */ - Map writeConverterMap(); + Map converterMap(); /** * Whether a header is required for the currently operated cell @@ -38,19 +43,8 @@ public interface WriteConfiguration { /** * Writes the head relative to the existing contents of the sheet. Indexes are zero-based. - */ - int writeRelativeHeadRowIndex(); - - /** - * What 'ExcelHeadProperty' does the currently operated cell need to execute - */ - ExcelHeadProperty excelHeadProperty(); - - /** - * - * Record whether it's new or from cache * * @return */ - boolean isNew(); + int relativeHeadRowIndex(); } 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 new file mode 100644 index 00000000..92a83a16 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java @@ -0,0 +1,109 @@ +package com.alibaba.excel.write.metadata.holder; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.poi.ss.usermodel.Sheet; + +import com.alibaba.excel.enums.HolderEnum; +import com.alibaba.excel.read.metadata.holder.TableHolder; +import com.alibaba.excel.write.metadata.WriteSheet; + +/** + * sheet holder + * + * @author zhuangjiaju + */ +public class WriteSheetHolder extends AbstractWriteHolder { + /** + * current param + */ + private WriteSheet writeSheet; + /*** + * poi sheet + */ + private Sheet sheet; + /*** + * sheetNo + */ + private Integer sheetNo; + /*** + * sheetName + */ + private String sheetName; + /*** + * poi sheet + */ + private WriteWorkbookHolder parentWorkBook; + /*** + * has been initialized table + */ + private Map hasBeenInitializedTable; + + public WriteSheetHolder(WriteSheet writeSheet, WriteWorkbookHolder writeWorkbookHolder) { + super(writeSheet, writeWorkbookHolder, writeWorkbookHolder.getWriteWorkbook().getConvertAllFiled()); + this.writeSheet = writeSheet; + this.sheetNo = writeSheet.getSheetNo(); + if (writeSheet.getSheetName() == null) { + this.sheetName = writeSheet.getSheetNo().toString(); + } else { + this.sheetName = writeSheet.getSheetName(); + } + this.parentWorkBook = writeWorkbookHolder; + this.hasBeenInitializedTable = new HashMap(); + } + + public WriteSheet getWriteSheet() { + return writeSheet; + } + + public void setWriteSheet(WriteSheet writeSheet) { + this.writeSheet = writeSheet; + } + + public Sheet getSheet() { + return sheet; + } + + public void setSheet(Sheet sheet) { + this.sheet = sheet; + } + + public Integer getSheetNo() { + return sheetNo; + } + + public void setSheetNo(Integer sheetNo) { + this.sheetNo = sheetNo; + } + + public String getSheetName() { + return sheetName; + } + + public void setSheetName(String sheetName) { + this.sheetName = sheetName; + } + + public WriteWorkbookHolder getParentWorkBook() { + return parentWorkBook; + } + + public void setParentWorkBook(WriteWorkbookHolder parentWorkBook) { + this.parentWorkBook = parentWorkBook; + } + + public Map getHasBeenInitializedTable() { + return hasBeenInitializedTable; + } + + public void setHasBeenInitializedTable( + Map hasBeenInitializedTable) { + this.hasBeenInitializedTable = hasBeenInitializedTable; + } + + @Override + public HolderEnum holderType() { + return HolderEnum.SHEET; + } +} 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 new file mode 100644 index 00000000..db150d67 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java @@ -0,0 +1,61 @@ +package com.alibaba.excel.write.metadata.holder; + +import com.alibaba.excel.enums.HolderEnum; +import com.alibaba.excel.write.metadata.WriteTable; + +/** + * sheet holder + * + * @author zhuangjiaju + */ +public class WriteTableHolder extends AbstractWriteHolder { + /*** + * poi sheet + */ + private WriteSheetHolder parentSheet; + /*** + * tableNo + */ + private Integer tableNo; + /** + * current table param + */ + private WriteTable writeTable; + + public WriteTableHolder(WriteTable writeTable, WriteSheetHolder writeSheetHolder, + WriteWorkbookHolder writeWorkbookHolder) { + super(writeTable, writeSheetHolder, writeWorkbookHolder.getWriteWorkbook().getConvertAllFiled()); + this.parentSheet = writeSheetHolder; + this.tableNo = writeTable.getTableNo(); + this.writeTable = writeTable; + } + + public WriteSheetHolder getParentSheet() { + return parentSheet; + } + + public void setParentSheet(WriteSheetHolder parentSheet) { + this.parentSheet = parentSheet; + } + + public Integer getTableNo() { + return tableNo; + } + + public void setTableNo(Integer tableNo) { + this.tableNo = tableNo; + } + + public WriteTable getWriteTable() { + return writeTable; + } + + public void setWriteTable(WriteTable writeTable) { + this.writeTable = writeTable; + } + + @Override + public HolderEnum holderType() { + return HolderEnum.TABLE; + } +} 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 new file mode 100644 index 00000000..b793b37f --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -0,0 +1,159 @@ +package com.alibaba.excel.write.metadata.holder; + +import java.io.File; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; + +import org.apache.poi.ss.usermodel.Workbook; + +import com.alibaba.excel.enums.HolderEnum; +import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.excel.write.metadata.WriteWorkbook; + +/** + * Workbook holder + * + * @author zhuangjiaju + */ +public class WriteWorkbookHolder extends AbstractWriteHolder { + /*** + * poi Workbook + */ + private Workbook workbook; + + /** + * current param + */ + private WriteWorkbook writeWorkbook; + /** + * Final output stream + */ + private OutputStream outputStream; + /** + * Template input stream + *

    + * If 'inputStream' and 'file' all not empty,file first + */ + private InputStream templateInputStream; + /** + * Template file + *

    + * If 'inputStream' and 'file' all not empty,file first + */ + private File templateFile; + /** + * Default true + */ + private Boolean autoCloseStream; + /** + * Excel type + */ + private ExcelTypeEnum excelType; + /** + * Mandatory use 'inputStream' + */ + private Boolean mandatoryUseInputStream; + /** + * prevent duplicate creation of sheet objects + */ + private Map hasBeenInitializedSheet; + + public WriteWorkbookHolder(WriteWorkbook writeWorkbook) { + super(writeWorkbook, null, writeWorkbook.getConvertAllFiled()); + this.writeWorkbook = writeWorkbook; + this.outputStream = writeWorkbook.getOutputStream(); + this.templateInputStream = writeWorkbook.getTemplateInputStream(); + this.templateFile = writeWorkbook.getTemplateFile(); + if (writeWorkbook.getAutoCloseStream() == null) { + this.autoCloseStream = Boolean.TRUE; + } else { + this.autoCloseStream = writeWorkbook.getAutoCloseStream(); + } + this.excelType = writeWorkbook.getExcelType(); + if (writeWorkbook.getMandatoryUseInputStream() == null) { + this.mandatoryUseInputStream = Boolean.FALSE; + } else { + this.mandatoryUseInputStream = writeWorkbook.getMandatoryUseInputStream(); + } + this.hasBeenInitializedSheet = new HashMap(); + } + + public Workbook getWorkbook() { + return workbook; + } + + public void setWorkbook(Workbook workbook) { + this.workbook = workbook; + } + + public Map getHasBeenInitializedSheet() { + return hasBeenInitializedSheet; + } + + public void setHasBeenInitializedSheet(Map hasBeenInitializedSheet) { + this.hasBeenInitializedSheet = hasBeenInitializedSheet; + } + + public WriteWorkbook getWriteWorkbook() { + return writeWorkbook; + } + + public void setWriteWorkbook(WriteWorkbook writeWorkbook) { + this.writeWorkbook = writeWorkbook; + } + + public OutputStream getOutputStream() { + return outputStream; + } + + public void setOutputStream(OutputStream outputStream) { + this.outputStream = outputStream; + } + + public InputStream getTemplateInputStream() { + return templateInputStream; + } + + public void setTemplateInputStream(InputStream templateInputStream) { + this.templateInputStream = templateInputStream; + } + + public File getTemplateFile() { + return templateFile; + } + + public void setTemplateFile(File templateFile) { + this.templateFile = templateFile; + } + + public Boolean getAutoCloseStream() { + return autoCloseStream; + } + + public void setAutoCloseStream(Boolean autoCloseStream) { + this.autoCloseStream = autoCloseStream; + } + + public ExcelTypeEnum getExcelType() { + return excelType; + } + + public void setExcelType(ExcelTypeEnum excelType) { + this.excelType = excelType; + } + + public Boolean getMandatoryUseInputStream() { + return mandatoryUseInputStream; + } + + public void setMandatoryUseInputStream(Boolean mandatoryUseInputStream) { + this.mandatoryUseInputStream = mandatoryUseInputStream; + } + + @Override + public HolderEnum holderType() { + return HolderEnum.WORKBOOK; + } +} diff --git a/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java new file mode 100644 index 00000000..cfe974d4 --- /dev/null +++ b/src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java @@ -0,0 +1,143 @@ +package com.alibaba.excel.write.property; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import com.alibaba.excel.annotation.write.style.ColumnWidth; +import com.alibaba.excel.annotation.write.style.ContentRowHeight; +import com.alibaba.excel.annotation.write.style.ContentStyle; +import com.alibaba.excel.annotation.write.style.HeadRowHeight; +import com.alibaba.excel.annotation.write.style.HeadStyle; +import com.alibaba.excel.enums.HeadKindEnum; +import com.alibaba.excel.metadata.CellRange; +import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.metadata.property.CellStyleProperty; +import com.alibaba.excel.metadata.property.ColumnWidthProperty; +import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.metadata.property.ExcelHeadProperty; +import com.alibaba.excel.metadata.property.RowHeightProperty; + +/** + * Define the header attribute of excel + * + * @author jipengfei + */ +public class ExcelWriteHeadProperty extends ExcelHeadProperty { + private RowHeightProperty headRowHeightProperty; + private RowHeightProperty contentRowHeightProperty; + + public ExcelWriteHeadProperty(Class headClazz, List> head, Boolean convertAllFiled) { + super(headClazz, head, convertAllFiled); + if (getHeadKind() != HeadKindEnum.CLASS) { + return; + } + this.headRowHeightProperty = + RowHeightProperty.build((HeadRowHeight)headClazz.getAnnotation(HeadRowHeight.class)); + this.contentRowHeightProperty = + RowHeightProperty.build((ContentRowHeight)headClazz.getAnnotation(ContentRowHeight.class)); + + HeadStyle parentHeadStyle = (HeadStyle)headClazz.getAnnotation(HeadStyle.class); + ContentStyle parentContentStyle = (ContentStyle)headClazz.getAnnotation(ContentStyle.class); + ColumnWidth parentColumnWidth = (ColumnWidth)headClazz.getAnnotation(ColumnWidth.class); + for (Map.Entry entry : getContentPropertyMap().entrySet()) { + Integer index = entry.getKey(); + ExcelContentProperty excelContentPropertyData = entry.getValue(); + Field field = excelContentPropertyData.getField(); + Head headData = getHeadMap().get(index); + HeadStyle headStyle = field.getAnnotation(HeadStyle.class); + if (headStyle == null) { + headStyle = parentHeadStyle; + } + headData.setCellStyleProperty(CellStyleProperty.build(headStyle)); + ColumnWidth columnWidth = field.getAnnotation(ColumnWidth.class); + if (columnWidth == null) { + columnWidth = parentColumnWidth; + } + headData.setColumnWidthProperty(ColumnWidthProperty.build(columnWidth)); + ContentStyle contentStyle = field.getAnnotation(ContentStyle.class); + if (contentStyle == null) { + contentStyle = parentContentStyle; + } + excelContentPropertyData.setCellStyleProperty(CellStyleProperty.build(contentStyle)); + } + + } + + public RowHeightProperty getHeadRowHeightProperty() { + return headRowHeightProperty; + } + + public void setHeadRowHeightProperty(RowHeightProperty headRowHeightProperty) { + this.headRowHeightProperty = headRowHeightProperty; + } + + public RowHeightProperty getContentRowHeightProperty() { + return contentRowHeightProperty; + } + + public void setContentRowHeightProperty(RowHeightProperty contentRowHeightProperty) { + this.contentRowHeightProperty = contentRowHeightProperty; + } + + /** + * Calculate all cells that need to be merged + * + * @return cells that need to be merged + */ + public List headCellRangeList() { + List cellRangeList = new ArrayList(); + int i = 0; + for (Map.Entry entry : getHeadMap().entrySet()) { + Head head = entry.getValue(); + List columnValues = head.getHeadNameList(); + for (int j = 0; j < columnValues.size(); j++) { + int lastRow = getLastRangNum(j, columnValues.get(j), columnValues); + int lastColumn = getLastRangNum(i, columnValues.get(j), head.getHeadNameList()); + if ((lastRow > j || lastColumn > i) && lastRow >= 0 && lastColumn >= 0) { + cellRangeList.add(new CellRange(j, lastRow, i, lastColumn)); + } + } + i++; + } + return cellRangeList; + } + + /** + * Get the last consecutive string position + * + * @param j + * current value position + * @param value + * value content + * @param values + * values + * @return the last consecutive string position + */ + private int getLastRangNum(int j, String value, List values) { + if (value == null) { + return -1; + } + if (j > 0) { + String preValue = values.get(j - 1); + if (value.equals(preValue)) { + return -1; + } + } + int last = j; + for (int i = last + 1; i < values.size(); i++) { + String current = values.get(i); + if (value.equals(current)) { + last = i; + } else { + // if i>j && !value.equals(current) Indicates that the continuous range is exceeded + if (i > j) { + break; + } + } + } + return last; + } + +} diff --git a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java index 9725ca51..9109680e 100644 --- a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java @@ -6,9 +6,9 @@ import org.apache.poi.ss.usermodel.Workbook; import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; -import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; +import com.alibaba.excel.write.metadata.holder.WorkbookHolder; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler; diff --git a/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java index 5d158e04..87787ef4 100644 --- a/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java @@ -6,8 +6,8 @@ import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; import com.alibaba.excel.write.handler.CellWriteHandler; import com.sun.istack.internal.Nullable; diff --git a/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java index cd335c24..008c1052 100644 --- a/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java @@ -3,8 +3,8 @@ package com.alibaba.excel.write.style.row; import org.apache.poi.ss.usermodel.Row; import com.alibaba.excel.event.NotRepeatExecutor; -import com.alibaba.excel.metadata.holder.write.SheetHolder; -import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.write.metadata.holder.SheetHolder; +import com.alibaba.excel.write.metadata.holder.TableHolder; import com.alibaba.excel.write.handler.RowWriteHandler; /** diff --git a/src/test/java/com/alibaba/easyexcel/test/ReadTest.java b/src/test/java/com/alibaba/easyexcel/test/ReadTest.java index f92fedeb..06a2c2f8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/ReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/ReadTest.java @@ -6,7 +6,7 @@ import com.alibaba.easyexcel.test.model.ReadModel2; import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelReader; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; import org.junit.Test; import java.io.IOException; diff --git a/src/test/java/com/alibaba/easyexcel/test/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/WriteTest.java index 86d7214c..ca9d42e1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/WriteTest.java @@ -5,8 +5,8 @@ import com.alibaba.easyexcel.test.model.WriteModel; import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.metadata.Sheet; -import com.alibaba.excel.metadata.Table; +import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.write.metadata.Table; import com.alibaba.excel.support.ExcelTypeEnum; import org.junit.Test; diff --git a/src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java b/src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java index d6ea8670..812b1950 100644 --- a/src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java @@ -12,7 +12,7 @@ import com.alibaba.easyexcel.test.model.ReadModel2; import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelReader; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; public class ReadTest { diff --git a/src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java b/src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java index edbb0472..29018975 100644 --- a/src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java @@ -6,7 +6,7 @@ import org.slf4j.LoggerFactory; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelReader; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; /** * Simple data test diff --git a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java b/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java index 3b2fc50f..47365cda 100644 --- a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java @@ -7,7 +7,7 @@ import org.junit.Test; import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelReader; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; /** * Simple data test diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java index 8d17226f..c5da2829 100644 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java @@ -9,7 +9,7 @@ import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.easyexcel.test.wirte.order.OrderData; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; /** * Order data test diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java index 6f9dad2c..105e0c6c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java @@ -8,7 +8,7 @@ import org.junit.Test; import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; /** * Order data test diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java index a905728c..98171db1 100644 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java @@ -8,7 +8,7 @@ import org.junit.Test; import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.write.metadata.Sheet; /** * Simple data test