Browse Source

拆分读写实体对象

bugfix
zhuangjiaju 5 years ago
parent
commit
e2bb1999f5
  1. 2
      src/main/java/com/alibaba/excel/EasyExcelFactory.java
  2. 2
      src/main/java/com/alibaba/excel/ExcelReader.java
  3. 6
      src/main/java/com/alibaba/excel/ExcelWriter.java
  4. 2
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyser.java
  5. 4
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  6. 2
      src/main/java/com/alibaba/excel/analysis/ExcelExecutor.java
  7. 2
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  8. 4
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BOFRecordHandler.java
  9. 4
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  10. 62
      src/main/java/com/alibaba/excel/context/AnalysisContext.java
  11. 10
      src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  12. 13
      src/main/java/com/alibaba/excel/context/WriteContext.java
  13. 24
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  14. 25
      src/main/java/com/alibaba/excel/enums/HolderEnum.java
  15. 86
      src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
  16. 82
      src/main/java/com/alibaba/excel/metadata/BasicParameter.java
  17. 37
      src/main/java/com/alibaba/excel/metadata/GlobalConfiguration.java
  18. 34
      src/main/java/com/alibaba/excel/metadata/Holder.java
  19. 193
      src/main/java/com/alibaba/excel/metadata/Sheet.java
  20. 55
      src/main/java/com/alibaba/excel/metadata/Table.java
  21. 2
      src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java
  22. 134
      src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
  23. 2
      src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
  24. 2
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  25. 43
      src/main/java/com/alibaba/excel/read/metadata/ReadBasicParameter.java
  26. 33
      src/main/java/com/alibaba/excel/read/metadata/ReadSheet.java
  27. 95
      src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java
  28. 240
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractWriteConfiguration.java
  29. 40
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java
  30. 79
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java
  31. 239
      src/main/java/com/alibaba/excel/read/metadata/holder/SheetHolder.java
  32. 13
      src/main/java/com/alibaba/excel/read/metadata/holder/TableHolder.java
  33. 45
      src/main/java/com/alibaba/excel/read/metadata/holder/WorkbookHolder.java
  34. 17
      src/main/java/com/alibaba/excel/read/metadata/property/ExcelReadHeadProperty.java
  35. 4
      src/main/java/com/alibaba/excel/util/WorkBookUtil.java
  36. 4
      src/main/java/com/alibaba/excel/write/ExcelBuilder.java
  37. 9
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  38. 2
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  39. 2
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  40. 2
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java
  41. 4
      src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java
  42. 4
      src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java
  43. 4
      src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java
  44. 2
      src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java
  45. 4
      src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java
  46. 51
      src/main/java/com/alibaba/excel/write/metadata/WriteBasicParameter.java
  47. 69
      src/main/java/com/alibaba/excel/write/metadata/WriteSheet.java
  48. 38
      src/main/java/com/alibaba/excel/write/metadata/WriteTable.java
  49. 91
      src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java
  50. 497
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  51. 86
      src/main/java/com/alibaba/excel/write/metadata/holder/UTils.java
  52. 28
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java
  53. 109
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteSheetHolder.java
  54. 61
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteTableHolder.java
  55. 159
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java
  56. 143
      src/main/java/com/alibaba/excel/write/property/ExcelWriteHeadProperty.java
  57. 6
      src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java
  58. 4
      src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java
  59. 4
      src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java
  60. 2
      src/test/java/com/alibaba/easyexcel/test/ReadTest.java
  61. 4
      src/test/java/com/alibaba/easyexcel/test/WriteTest.java
  62. 2
      src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java
  63. 2
      src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java
  64. 2
      src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java
  65. 2
      src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java
  66. 2
      src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java
  67. 2
      src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java

2
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;

2
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;

6
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;

2
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

4
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;
/**

2
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

2
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;
/**

4
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;

4
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;
/**

62
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();
}

10
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");
}

13
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

24
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;

25
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;
}

86
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<List<String>> 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<List<String>> getHead() {
return head;
}
public void setHead(List<List<String>> 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();
}
}

82
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.
*
* <li>0 - This Sheet has no head ,since the first row are the data
* <li>1 - This Sheet has one row head , this is the default
* <li>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<Converter> customConverterList = new ArrayList<Converter>();
/**
* Custom type handler override the default
*/
private List<WriteHandler> customWriteHandlerList = new ArrayList<WriteHandler>();
/**
* Custom type listener run after default
*/
private List<ReadListener> customReadListenerList = new ArrayList<ReadListener>();
/**
* 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<List<String>> 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<Converter> getCustomConverterList() {
return customConverterList;
}
@ -102,22 +60,6 @@ public class BasicParameter {
this.customConverterList = customConverterList;
}
public List<WriteHandler> getCustomWriteHandlerList() {
return customWriteHandlerList;
}
public void setCustomWriteHandlerList(List<WriteHandler> customWriteHandlerList) {
this.customWriteHandlerList = customWriteHandlerList;
}
public List<ReadListener> getCustomReadListenerList() {
return customReadListenerList;
}
public void setCustomReadListenerList(List<ReadListener> 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;
}
}

37
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;
}
}

34
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();
}

193
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<Integer, Integer> columnWidthMap = new HashMap<Integer, Integer>();
private Class<? extends BaseRowModel> clazz;
/**
*/
private List<List<String>> 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<Integer,Integer> columnWidthMap = new HashMap<Integer, Integer>();
/**
* 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<List<String>> head) {
super();
this.sheetNo = sheetNo - 1;
public Sheet(int sheetNo, int headLineMun, Class<? extends BaseRowModel> clazz) {
this.sheetNo = sheetNo;
this.headLineMun = headLineMun;
this.clazz = clazz;
}
public Sheet(int sheetNo, int headLineMun, Class<? extends BaseRowModel> clazz, String sheetName,
List<List<String>> head) {
this.sheetNo = sheetNo;
this.clazz = clazz;
this.headLineMun = headLineMun;
this.sheetName = sheetName;
this.head = head;
}
public List<List<String>> getHead() {
return head;
}
public void setHead(List<List<String>> head) {
this.head = head;
}
public Class<? extends BaseRowModel> getClazz() {
return clazz;
}
public void setClazz(Class<? extends BaseRowModel> clazz) {
this.clazz = clazz;
if (headLineMun == 0) {
this.headLineMun = 1;
}
}
public int getHeadLineMun() {
return headLineMun;
}
setReadHeadRowNumber(readHeadRowNumber);
setClazz(clazz);
setHead(head);
public void setHeadLineMun(int headLineMun) {
this.headLineMun = headLineMun;
}
public Integer getSheetNo() {
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<Integer, Integer> 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;
}
}

55
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<? extends BaseRowModel> clazz;
/**
*/
private List<List<String>> 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<? extends BaseRowModel> getClazz() {
return clazz;
}
public void setTableNo(Integer tableNo) {
this.tableNo = tableNo;
public void setClazz(Class<? extends BaseRowModel> clazz) {
this.clazz = clazz;
}
public TableStyle getTableStyle() {
return tableStyle;
public List<List<String>> getHead() {
return head;
}
public void setTableStyle(TableStyle tableStyle) {
this.tableStyle = tableStyle;
public void setHead(List<List<String>> 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;
}
}

2
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;

134
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<Integer, ExcelContentProperty> contentPropertyMap;
private RowHeightProperty headRowHeightProperty;
private RowHeightProperty contentRowHeightProperty;
public ExcelHeadProperty(Class headClazz, List<List<String>> head, WorkbookHolder workbookHolder) {
public ExcelHeadProperty(Class headClazz, List<List<String>> head, Boolean convertAllFiled) {
this.headClazz = headClazz;
headMap = new TreeMap<Integer, Head>();
contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>();
@ -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<Integer, Field> 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<String> tmpHeadList = new ArrayList<String>();
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<CellRange> headCellRangeList() {
List<CellRange> cellRangeList = new ArrayList<CellRange>();
int i = 0;
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
Head head = entry.getValue();
List<String> 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<String> 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;
}
}

2
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

2
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;

43
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.
*
* <li>0 - This Sheet has no head ,since the first row are the data
* <li>1 - This Sheet has one row head , this is the default
* <li>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<ReadListener> customReadListenerList = new ArrayList<ReadListener>();
public Integer getHeadRowNumber() {
return headRowNumber;
}
public void setHeadRowNumber(Integer headRowNumber) {
this.headRowNumber = headRowNumber;
}
public List<ReadListener> getCustomReadListenerList() {
return customReadListenerList;
}
public void setCustomReadListenerList(List<ReadListener> customReadListenerList) {
this.customReadListenerList = customReadListenerList;
}
}

33
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;
}
}

95
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
* <p>
* If 'inputStream' and 'file' all not empty,file first
*/
private InputStream inputStream;
/**
* Read file
* <p>
* 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.
* <li>if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a field.
* <li>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;
}
}

240
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.
*
* <li>0 - This Sheet has no head ,since the first row are the data
* <li>1 - This Sheet has one row head , this is the default
* <li>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<ReadListener> readListenerList;
/**
* Converter for workbook
*/
private Map<ConverterKey, Converter> 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<ReadListener>();
} else {
this.readListenerList = new ArrayList<ReadListener>(parentAbstractReadHolder.getReadListenerList());
}
if (readBasicParameter.getCustomReadListenerList() != null
&& !readBasicParameter.getCustomReadListenerList().isEmpty()) {
this.readListenerList.addAll(readBasicParameter.getCustomReadListenerList());
}
if (parentAbstractReadHolder == null) {
this.converterMap = new HashMap<ConverterKey, Converter>();
} else {
this.converterMap = new HashMap<ConverterKey, Converter>(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<CellData> cellDataList = (List<CellData>)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<CellData> cellDataList) {
if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelReadHeadProperty().getHeadKind())) {
return;
}
List<String> dataList = (List<String>)buildStringList(cellDataList, analysisContext.currentConfiguration());
ExcelReadHeadProperty excelHeadPropertyData = analysisContext.currentConfiguration().excelReadHeadProperty();
Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap();
Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1);
Map<Integer, ExcelContentProperty> tmpContentPropertyMap =
new HashMap<Integer, ExcelContentProperty>(contentPropertyMapData.size() * 4 / 3 + 1);
for (Map.Entry<Integer, Head> 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<CellData> data,
com.alibaba.excel.read.metadata.read.ReadConfiguration readConfiguration) {
List<String> list = new ArrayList<String>();
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<ReadListener> getReadListenerList() {
return readListenerList;
}
public void setReadListenerList(List<ReadListener> readListenerList) {
this.readListenerList = readListenerList;
}
public Map<ConverterKey, Converter> getConverterMap() {
return converterMap;
}
public void setConverterMap(Map<ConverterKey, Converter> 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<ReadListener> readListenerList() {
return getReadListenerList();
}
@Override
public Map<ConverterKey, Converter> converterMap() {
return getConverterMap();
}
@Override
public ExcelReadHeadProperty excelReadHeadProperty() {
return getExcelReadHeadProperty();
}
}

40
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<ReadListener> readListenerList();
/**
* What converter does the currently operated cell need to execute
*
* @return
*/
Map<ConverterKey, Converter> converterMap();
/**
* What 'ExcelReadHeadProperty' does the currently operated cell need to execute
*
* @return
*/
ExcelReadHeadProperty excelReadHeadProperty();
}

79
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();
}
}

239
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<Integer, TableHolder> 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<WriteHandler> handlerList = new ArrayList<WriteHandler>();
if (sheet.getCustomWriteHandlerList() != null && !sheet.getCustomWriteHandlerList().isEmpty()) {
handlerList.addAll(sheet.getCustomWriteHandlerList());
}
// Initialization Annotation
sheetHolder.initAnnotationConfig(handlerList);
sheetHolder
.setWriteHandlerMap(sheetHolder.sortAndClearUpHandler(handlerList, workbookHolder.getWriteHandlerMap()));
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(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<Integer, TableHolder>());
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<ConverterKey, Converter> converterMap =
new HashMap<ConverterKey, Converter>(workbookHolder.getReadConverterMap());
if (sheet.getCustomConverterList() != null && !sheet.getCustomConverterList().isEmpty()) {
for (Converter converter : sheet.getCustomConverterList()) {
converterMap.put(ConverterKey.buildConverterKey(converter), converter);
}
}
sheetHolder.setReadConverterMap(converterMap);
List<ReadListener> readListenerList = new ArrayList<ReadListener>();
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<Integer, Integer> columnWidthMap = sheet.getColumnWidthMap();
if (sheet.getCustomWriteHandlerList() == null) {
sheet.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
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<WriteHandler>());
}
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<Integer, TableHolder> getHasBeenInitializedTable() {
return hasBeenInitializedTable;
}
public void setHasBeenInitializedTable(Map<Integer, TableHolder> 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;
}
}

13
src/main/java/com/alibaba/excel/metadata/holder/write/TableHolder.java → 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,9 +36,9 @@ 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,
public static TableHolder buildWriteWorkTableHolder(Table table, SheetHolder sheetHolder,
WorkbookHolder workbookHolder) {
TableHolder tableHolder = new TableHolder();
tableHolder.setTableParam(table);
@ -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) {

45
src/main/java/com/alibaba/excel/metadata/holder/write/WorkbookHolder.java → 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<Integer, SheetHolder> hasBeenInitializedSheet;
/**
* current param
*/
private com.alibaba.excel.metadata.Workbook workbookParam;
private ReadWorkbook readWorkbook;
/**
* Final output stream
*/
private OutputStream outputStream;
/**
* <li>write: Template input stream
* <li>read: Read InputStream
* Read InputStream
* <p>
* If 'inputStream' and 'file' all not empty,file first
*/
private InputStream inputStream;
/**
* <li>write: Template file
* <li>read: Read file
* Read file
* <p>
* 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;
}
}

17
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<List<String>> head, Boolean convertAllFiled) {
super(headClazz, head, convertAllFiled);
}
}

4
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() + "");
}

4
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;
/**

9
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);

2
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;

2
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;
/**

2
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;
/**

4
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;
/**

4
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;
/**

4
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

2
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

4
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;
/**

51
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<WriteHandler> customWriteHandlerList = new ArrayList<WriteHandler>();
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<WriteHandler> getCustomWriteHandlerList() {
return customWriteHandlerList;
}
public void setCustomWriteHandlerList(List<WriteHandler> customWriteHandlerList) {
this.customWriteHandlerList = customWriteHandlerList;
}
}

69
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<Integer, Integer> columnWidthMap = new HashMap<Integer, Integer>();
/**
*
* @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<Integer, Integer> getColumnWidthMap() {
return columnWidthMap;
}
public void setColumnWidthMap(Map<Integer, Integer> columnWidthMap) {
this.columnWidthMap = columnWidthMap;
}
public TableStyle getTableStyle() {
return tableStyle;
}
public void setTableStyle(TableStyle tableStyle) {
this.tableStyle = tableStyle;
}
}

38
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;
}
}

91
src/main/java/com/alibaba/excel/metadata/Workbook.java → 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 {
* <p>
* If 'inputStream' and 'file' all not empty,file first
*/
private InputStream inputStream;
private InputStream templateInputStream;
/**
* <li>write: Template file
@ -39,30 +35,19 @@ public class Workbook extends BasicParameter {
* <p>
* 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.
* <li>if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a field.
* <li>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 void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public File getFile() {
return file;
public InputStream getTemplateInputStream() {
return templateInputStream;
}
public void setFile(File file) {
this.file = file;
public void setTemplateInputStream(InputStream templateInputStream) {
this.templateInputStream = templateInputStream;
}
public com.alibaba.excel.event.WriteHandler getWriteHandler() {
return writeHandler;
public File getTemplateFile() {
return templateFile;
}
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;
}
}

497
src/main/java/com/alibaba/excel/metadata/holder/write/AbstractWriteConfiguration.java → 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,285 +11,278 @@ 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<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap;
/**
* Read listener
*/
private List<ReadListener> readListenerList;
/**
* Converter for workbook
*/
private Map<ConverterKey, Converter> readConverterMap;
/**
* Converter for workbook
*/
private Map<Class, Converter> 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.
*
* <li>0 - This Sheet has no head ,since the first row are the data
* <li>1 - This Sheet has one row head , this is the default
* <li>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<List<String>> 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<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap;
/**
* Automatic trim includes sheet name and content
* Converter for workbook
*/
private Boolean autoTrim;
public Boolean getNeedHead() {
return needHead;
private Map<Class, Converter> 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());
}
public void setNeedHead(Boolean needHead) {
this.needHead = needHead;
} else {
getGlobalConfiguration().setUse1904windowing(writeBasicParameter.getUse1904windowing());
}
public Map<Class<? extends WriteHandler>, List<WriteHandler>> getWriteHandlerMap() {
return writeHandlerMap;
if (writeBasicParameter.getNeedHead() == null) {
if (parentAbstractWriteHolder == null) {
this.needHead = Boolean.TRUE;
} else {
this.needHead = parentAbstractWriteHolder.getNeedHead();
}
public void setWriteHandlerMap(Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap) {
this.writeHandlerMap = writeHandlerMap;
} else {
this.needHead = writeBasicParameter.getNeedHead();
}
public ExcelHeadProperty getExcelHeadProperty() {
return excelHeadProperty;
if (writeBasicParameter.getRelativeHeadRowIndex() == null) {
if (parentAbstractWriteHolder == null) {
this.relativeHeadRowIndex = 0;
} else {
this.relativeHeadRowIndex = parentAbstractWriteHolder.getRelativeHeadRowIndex();
}
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) {
this.excelHeadProperty = excelHeadProperty;
} else {
this.relativeHeadRowIndex = writeBasicParameter.getRelativeHeadRowIndex();
}
public Integer getWriteRelativeHeadRowIndex() {
return writeRelativeHeadRowIndex;
}
// Initialization property
this.excelWriteHeadProperty = new ExcelWriteHeadProperty(getClazz(), getHead(), convertAllFiled);
public void setWriteRelativeHeadRowIndex(Integer writeRelativeHeadRowIndex) {
this.writeRelativeHeadRowIndex = writeRelativeHeadRowIndex;
}
// Compatible with old code
compatibleOldCode(writeBasicParameter);
public Boolean getNewInitialization() {
return newInitialization;
// Set writeHandlerMap
List<WriteHandler> handlerList = new ArrayList<WriteHandler>();
if (writeBasicParameter.getCustomWriteHandlerList() != null
&& !writeBasicParameter.getCustomWriteHandlerList().isEmpty()) {
handlerList.addAll(writeBasicParameter.getCustomWriteHandlerList());
}
// Initialization Annotation
initAnnotationConfig(handlerList);
public void setNewInitialization(Boolean newInitialization) {
this.newInitialization = newInitialization;
Map<Class<? extends WriteHandler>, List<WriteHandler>> parentWriteHandlerMap = null;
if (parentAbstractWriteHolder != null) {
parentWriteHandlerMap = parentAbstractWriteHolder.getWriteHandlerMap();
}
this.writeHandlerMap = sortAndClearUpHandler(handlerList, parentWriteHandlerMap);
public List<List<String>> getHead() {
return head;
// Set converterMap
if (parentAbstractWriteHolder == null) {
this.converterMap = DefaultConverterLoader.loadDefaultWriteConverter();
} else {
this.converterMap = new HashMap<Class, Converter>(parentAbstractWriteHolder.getConverterMap());
}
public void setHead(List<List<String>> head) {
this.head = head;
if (writeBasicParameter.getCustomConverterList() != null
&& !writeBasicParameter.getCustomConverterList().isEmpty()) {
for (Converter converter : writeBasicParameter.getCustomConverterList()) {
converterMap.put(converter.getClass(), converter);
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
public Boolean getAutoTrim() {
return autoTrim;
/**
* 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:
}
public void setAutoTrim(Boolean autoTrim) {
this.autoTrim = autoTrim;
}
public List<ReadListener> getReadListenerList() {
return readListenerList;
@Deprecated
private void compatibleOldCodeCreateRowCellStyleStrategy(WriteBasicParameter writeBasicParameter,
TableStyle tableStyle) {
if (tableStyle == null) {
return;
}
public void setReadListenerList(List<ReadListener> readListenerList) {
this.readListenerList = readListenerList;
if (writeBasicParameter.getCustomWriteHandlerList() == null) {
writeBasicParameter.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
public Map<ConverterKey, Converter> getReadConverterMap() {
return readConverterMap;
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));
}
public void setReadConverterMap(Map<ConverterKey, Converter> readConverterMap) {
this.readConverterMap = readConverterMap;
@Deprecated
private void compatibleOldCodeCreateHeadColumnWidthStyleStrategy(WriteBasicParameter writeBasicParameter,
final Map<Integer, Integer> columnWidthMap) {
if (columnWidthMap == null || columnWidthMap.isEmpty()) {
return;
}
public Map<Class, Converter> getWriteConverterMap() {
return writeConverterMap;
if (writeBasicParameter.getCustomWriteHandlerList() == null) {
writeBasicParameter.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
}
public void setWriteConverterMap(Map<Class, Converter> writeConverterMap) {
this.writeConverterMap = writeConverterMap;
writeBasicParameter.getCustomWriteHandlerList().add(new AbstractHeadColumnWidthStyleStrategy() {
@Override
protected Integer columnWidth(Head head) {
if (columnWidthMap.containsKey(head.getColumnIndex())) {
columnWidthMap.get(head.getColumnIndex());
}
public Integer getReadHeadRowNumber() {
return readHeadRowNumber;
return 20;
}
public void setReadHeadRowNumber(Integer readHeadRowNumber) {
this.readHeadRowNumber = readHeadRowNumber;
});
}
@Override
public void register(AnalysisEventListener listener) {
readListenerList.add(listener);
protected void initAnnotationConfig(List<WriteHandler> handlerList) {
if (!HeadKindEnum.CLASS.equals(getExcelWriteHeadProperty().getHeadKind())) {
return;
}
Map<Integer, Head> headMap = getExcelWriteHeadProperty().getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMap = getExcelWriteHeadProperty().getContentPropertyMap();
@Override
public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) {
List<CellData> cellDataList = (List<CellData>)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);
boolean hasCellStyle = false;
boolean hasColumnWidth = false;
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
if (entry.getValue().getCellStyleProperty() != null) {
hasCellStyle = true;
}
if (entry.getValue().getColumnWidthProperty() != null) {
hasColumnWidth = true;
}
ExcelContentProperty excelContentProperty = contentPropertyMap.get(entry.getKey());
if (excelContentProperty.getCellStyleProperty() != null) {
hasCellStyle = true;
}
}
if (hasCellStyle) {
dealCellStyle(handlerList, contentPropertyMap);
}
// Now is header
if (analysisContext.currentRowNum().equals(analysisContext.currentSheetHolder().getReadHeadRowNumber())) {
buildHead(analysisContext, cellDataList);
if (hasColumnWidth) {
dealColumnWidth(handlerList);
}
dealRowHigh(handlerList, contentPropertyMap);
}
@Override
public void notifyAfterAllAnalysed(AnalysisContext analysisContext) {
for (ReadListener readListener : readListenerList) {
readListener.doAfterAllAnalysed(analysisContext);
private void dealRowHigh(List<WriteHandler> handlerList, Map<Integer, ExcelContentProperty> contentPropertyMap) {
RowHeightProperty headRowHeightProperty = getExcelWriteHeadProperty().getHeadRowHeightProperty();
RowHeightProperty contentRowHeightProperty = getExcelWriteHeadProperty().getContentRowHeightProperty();
if (headRowHeightProperty == null && contentRowHeightProperty == null) {
return;
}
Short headRowHeight = null;
if (headRowHeightProperty != null) {
headRowHeight = headRowHeightProperty.getHeight();
}
private void buildHead(AnalysisContext analysisContext, List<CellData> cellDataList) {
if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelHeadProperty().getHeadKind())) {
return;
Short contentRowHeight = null;
if (contentRowHeightProperty != null) {
contentRowHeight = contentRowHeightProperty.getHeight();
}
List<String> dataList = (List<String>)buildStringList(cellDataList, analysisContext.currentConfiguration());
ExcelHeadProperty excelHeadPropertyData = analysisContext.currentConfiguration().excelHeadProperty();
Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap();
Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1);
Map<Integer, ExcelContentProperty> tmpContentPropertyMap =
new HashMap<Integer, ExcelContentProperty>(contentPropertyMapData.size() * 4 / 3 + 1);
for (Map.Entry<Integer, Head> entry : headMapData.entrySet()) {
Head headData = entry.getValue();
if (headData.getForceIndex()) {
tmpHeadMap.put(entry.getKey(), headData);
tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey()));
continue;
handlerList.add(new SimpleRowHeightStyleStrategy(headRowHeight, contentRowHeight));
}
String headName = headData.getHeadNameList().get(0);
for (int i = 0; i < dataList.size(); i++) {
String headString = dataList.get(i);
if (StringUtils.isEmpty(headString)) {
continue;
private void dealColumnWidth(List<WriteHandler> handlerList) {
WriteHandler columnWidthStyleStrategy = new AbstractColumnWidthStyleStrategy() {
@Override
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) {
if (head == null) {
return;
}
if (analysisContext.currentSheetHolder().getAutoTrim()) {
headString = headString.trim();
if (head.getColumnWidthProperty() != null) {
sheet.setColumnWidth(head.getColumnIndex(), head.getColumnWidthProperty().getWidth());
}
if (headName.equals(headString)) {
headData.setColumnIndex(i);
tmpHeadMap.put(i, headData);
tmpContentPropertyMap.put(i, contentPropertyMapData.get(entry.getKey()));
break;
}
};
handlerList.add(columnWidthStyleStrategy);
}
private void dealCellStyle(List<WriteHandler> handlerList,
final Map<Integer, ExcelContentProperty> contentPropertyMap) {
WriteHandler columnCellStyleStrategy = new AbstractColumnCellStyleStrategy() {
@Override
protected CellStyle headCellStyle(Head head) {
if (head == null || head.getCellStyleProperty() == null) {
return null;
}
excelHeadPropertyData.setHeadMap(tmpHeadMap);
excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap);
CellStyleProperty cellStyleProperty = head.getCellStyleProperty();
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(),
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors());
}
private Object buildStringList(List<CellData> data, ReadConfiguration readConfiguration) {
List<String> list = new ArrayList<String>();
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");
@Override
protected CellStyle contentCellStyle(Head head) {
if (head == null) {
return null;
}
try {
list.add((String)(converter.convertToJavaData(cellData, null)));
} catch (Exception e) {
throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e);
ExcelContentProperty excelContentProperty = contentPropertyMap.get(head.getColumnIndex());
if (excelContentProperty == null || excelContentProperty.getCellStyleProperty() == null) {
return null;
}
CellStyleProperty cellStyleProperty = excelContentProperty.getCellStyleProperty();
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(),
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors());
}
return list;
};
handlerList.add(columnCellStyleStrategy);
}
protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpHandler(
@ -359,136 +352,68 @@ public abstract class AbstractWriteConfiguration
return result;
}
protected void initAnnotationConfig(List<WriteHandler> handlerList) {
if (!HeadKindEnum.CLASS.equals(getExcelHeadProperty().getHeadKind())) {
return;
public Boolean getNeedHead() {
return needHead;
}
Map<Integer, Head> headMap = getExcelHeadProperty().getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMap = getExcelHeadProperty().getContentPropertyMap();
boolean hasCellStyle = false;
boolean hasColumnWidth = false;
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
if (entry.getValue().getCellStyleProperty() != null) {
hasCellStyle = true;
}
if (entry.getValue().getColumnWidthProperty() != null) {
hasColumnWidth = true;
}
ExcelContentProperty excelContentProperty = contentPropertyMap.get(entry.getKey());
if (excelContentProperty.getCellStyleProperty() != null) {
hasCellStyle = true;
}
public void setNeedHead(Boolean needHead) {
this.needHead = needHead;
}
if (hasCellStyle) {
dealCellStyle(handlerList, contentPropertyMap);
}
if (hasColumnWidth) {
dealColumnWidth(handlerList);
}
dealRowHigh(handlerList, contentPropertyMap);
public Map<Class<? extends WriteHandler>, List<WriteHandler>> getWriteHandlerMap() {
return writeHandlerMap;
}
private void dealRowHigh(List<WriteHandler> handlerList, Map<Integer, ExcelContentProperty> contentPropertyMap) {
RowHeightProperty headRowHeightProperty = excelHeadProperty.getHeadRowHeightProperty();
RowHeightProperty contentRowHeightProperty = excelHeadProperty.getContentRowHeightProperty();
if (headRowHeightProperty == null && contentRowHeightProperty == null) {
return;
}
Short headRowHeight = null;
if (headRowHeightProperty != null) {
headRowHeight = headRowHeightProperty.getHeight();
}
Short contentRowHeight = null;
if (contentRowHeightProperty != null) {
contentRowHeight = contentRowHeightProperty.getHeight();
}
handlerList.add(new SimpleRowHeightStyleStrategy(headRowHeight, contentRowHeight));
public void setWriteHandlerMap(Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap) {
this.writeHandlerMap = writeHandlerMap;
}
private void dealColumnWidth(List<WriteHandler> handlerList) {
WriteHandler columnWidthStyleStrategy = new AbstractColumnWidthStyleStrategy() {
@Override
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) {
if (head == null) {
return;
}
if (head.getColumnWidthProperty() != null) {
sheet.setColumnWidth(head.getColumnIndex(), head.getColumnWidthProperty().getWidth());
}
}
};
handlerList.add(columnWidthStyleStrategy);
public Map<Class, Converter> getConverterMap() {
return converterMap;
}
private void dealCellStyle(List<WriteHandler> handlerList,
final Map<Integer, ExcelContentProperty> contentPropertyMap) {
WriteHandler columnCellStyleStrategy = new AbstractColumnCellStyleStrategy() {
@Override
protected CellStyle headCellStyle(Head head) {
if (head == null || head.getCellStyleProperty() == null) {
return null;
}
CellStyleProperty cellStyleProperty = head.getCellStyleProperty();
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(),
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors());
public void setConverterMap(Map<Class, Converter> converterMap) {
this.converterMap = converterMap;
}
@Override
protected CellStyle contentCellStyle(Head head) {
if (head == null) {
return null;
}
ExcelContentProperty excelContentProperty = contentPropertyMap.get(head.getColumnIndex());
if (excelContentProperty == null || excelContentProperty.getCellStyleProperty() == null) {
return null;
}
CellStyleProperty cellStyleProperty = excelContentProperty.getCellStyleProperty();
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(),
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors());
}
};
handlerList.add(columnCellStyleStrategy);
public ExcelWriteHeadProperty getExcelWriteHeadProperty() {
return excelWriteHeadProperty;
}
@Override
public Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap() {
return getWriteHandlerMap();
public void setExcelWriteHeadProperty(ExcelWriteHeadProperty excelWriteHeadProperty) {
this.excelWriteHeadProperty = excelWriteHeadProperty;
}
@Override
public boolean needHead() {
return getNeedHead();
public Integer getRelativeHeadRowIndex() {
return relativeHeadRowIndex;
}
@Override
public int writeRelativeHeadRowIndex() {
return getWriteRelativeHeadRowIndex();
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<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap() {
return getWriteHandlerMap();
}
@Override
public List<ReadListener> readListenerList() {
return getReadListenerList();
public Map<Class, Converter> converterMap() {
return getConverterMap();
}
@Override
public Map<ConverterKey, Converter> readConverterMap() {
return getReadConverterMap();
public boolean needHead() {
return getNeedHead();
}
@Override
public Map<Class, Converter> writeConverterMap() {
return getWriteConverterMap();
public int relativeHeadRowIndex() {
return getRelativeHeadRowIndex();
}
}

86
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<CellData> cellDataList) {
if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelHeadProperty().getHeadKind())) {
return;
}
List<String> dataList = (List<String>)buildStringList(cellDataList, analysisContext.currentConfiguration());
ExcelHeadProperty excelHeadPropertyData = analysisContext.currentConfiguration().excelHeadProperty();
Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap();
Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1);
Map<Integer, ExcelContentProperty> tmpContentPropertyMap =
new HashMap<Integer, ExcelContentProperty>(contentPropertyMapData.size() * 4 / 3 + 1);
for (Map.Entry<Integer, Head> 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<CellData> data, ReadConfiguration readConfiguration) {
List<String> list = new ArrayList<String>();
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;
}
}

28
src/main/java/com/alibaba/excel/metadata/holder/write/WriteConfiguration.java → 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<Class, Converter> writeConverterMap();
Map<Class, Converter> 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();
}

109
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<Integer, TableHolder> 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<Integer, TableHolder>();
}
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<Integer, TableHolder> getHasBeenInitializedTable() {
return hasBeenInitializedTable;
}
public void setHasBeenInitializedTable(
Map<Integer, TableHolder> hasBeenInitializedTable) {
this.hasBeenInitializedTable = hasBeenInitializedTable;
}
@Override
public HolderEnum holderType() {
return HolderEnum.SHEET;
}
}

61
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;
}
}

159
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
* <p>
* If 'inputStream' and 'file' all not empty,file first
*/
private InputStream templateInputStream;
/**
* Template file
* <p>
* 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<Integer, WriteSheetHolder> 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<Integer, WriteSheetHolder>();
}
public Workbook getWorkbook() {
return workbook;
}
public void setWorkbook(Workbook workbook) {
this.workbook = workbook;
}
public Map<Integer, WriteSheetHolder> getHasBeenInitializedSheet() {
return hasBeenInitializedSheet;
}
public void setHasBeenInitializedSheet(Map<Integer, WriteSheetHolder> 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;
}
}

143
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<List<String>> 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<Integer, ExcelContentProperty> 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<CellRange> headCellRangeList() {
List<CellRange> cellRangeList = new ArrayList<CellRange>();
int i = 0;
for (Map.Entry<Integer, Head> entry : getHeadMap().entrySet()) {
Head head = entry.getValue();
List<String> 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<String> 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;
}
}

6
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;

4
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;

4
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;
/**

2
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;

4
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;

2
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 {

2
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

2
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

2
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

2
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

2
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

Loading…
Cancel
Save