Browse Source

拆分读写实体对象

pull/507/head
zhuangjiaju 6 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.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.event.WriteHandler; 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.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; 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.converters.Converter;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException; 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.parameter.AnalysisParam;
import com.alibaba.excel.support.ExcelTypeEnum; 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 java.util.Map;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.write.metadata.Table;
import com.alibaba.excel.metadata.Workbook; import com.alibaba.excel.write.metadata.Workbook;
import com.alibaba.excel.parameter.GenerateParam; import com.alibaba.excel.parameter.GenerateParam;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.ExcelBuilder; 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; package com.alibaba.excel.analysis;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Excel file analyser * 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.AnalysisContext;
import com.alibaba.excel.context.AnalysisContextImpl; import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
import com.alibaba.excel.metadata.Workbook; import com.alibaba.excel.write.metadata.Workbook;
import com.alibaba.excel.support.ExcelTypeEnum; 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 java.util.List;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Excel file Executor * 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.context.AnalysisContext;
import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent; import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent;
import com.alibaba.excel.exception.ExcelAnalysisException; 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; 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; package com.alibaba.excel.analysis.v03.handlers;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler; import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.XlsRecordHandler;
import com.alibaba.excel.context.AnalysisContext; 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.eventusermodel.EventWorkbookBuilder;
import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BoundSheetRecord; import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.cache.Ehcache;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.write.metadata.holder.WorkbookHolder;
import com.alibaba.excel.util.FileUtils; 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; package com.alibaba.excel.context;
import com.alibaba.excel.metadata.holder.read.ReadConfiguration; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.metadata.holder.write.WorkbookHolder; 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 * 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 * All information about the workbook you are currently working on
* *
* @return * @return
*/ */
WorkbookHolder currentWorkbookHolder(); ReadWorkbookHolder currentWorkbookHolder();
/** /**
* All information about the sheet you are currently working on * All information about the sheet you are currently working on
* *
* @return * @return
*/ */
SheetHolder currentSheetHolder(); ReadSheetHolder currentSheetHolder();
/** /**
* Configuration of currently operated cell * Row of currently operated cell
* *
* @return * @return
*/ */
ReadConfiguration currentConfiguration(); ReadRowHolder currentRowHolder();
/**
* 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);
} }

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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.WorkbookHolder; 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; private SheetHolder currentSheetHolder;
public AnalysisContextImpl(com.alibaba.excel.metadata.Workbook workbook) { public AnalysisContextImpl(Workbook workbook) {
if (workbook == null) { if (workbook == null) {
throw new IllegalArgumentException("Workbook argument cannot be null"); throw new IllegalArgumentException("Workbook argument cannot be null");
} }
@ -32,7 +34,7 @@ public class AnalysisContextImpl implements AnalysisContext {
} }
@Override @Override
public void currentSheet(com.alibaba.excel.metadata.Sheet sheet) { public void currentSheet(Sheet sheet) {
if (sheet == null) { if (sheet == null) {
throw new IllegalArgumentException("Sheet argument cannot be 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; package com.alibaba.excel.context;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.write.metadata.Table;
import com.alibaba.excel.metadata.holder.write.WriteConfiguration; import com.alibaba.excel.write.metadata.holder.WriteConfiguration;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.metadata.holder.TableHolder;
import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.write.metadata.holder.WorkbookHolder;
import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Write context * Write context
@ -17,7 +18,7 @@ public interface WriteContext {
* *
* @param sheet * @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 * 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.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.Head; 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.metadata.property.ExcelHeadProperty;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
import com.alibaba.excel.write.handler.CellWriteHandler; 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.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler; 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. * A context is the main anchorage point of a excel writer.
@ -52,7 +53,7 @@ public class WriteContextImpl implements WriteContext {
*/ */
private WriteConfiguration currentWriteConfiguration; private WriteConfiguration currentWriteConfiguration;
public WriteContextImpl(com.alibaba.excel.metadata.Workbook workbook) { public WriteContextImpl(Workbook workbook) {
if (workbook == null) { if (workbook == null) {
throw new IllegalArgumentException("Workbook argument cannot be 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); currentWorkbookHolder = new WorkbookHolder(workbook);
currentWriteConfiguration = currentWorkbookHolder; currentWriteConfiguration = currentWorkbookHolder;
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -108,11 +109,12 @@ public class WriteContextImpl implements WriteContext {
* @param sheet * @param sheet
*/ */
@Override @Override
public void currentSheet(com.alibaba.excel.metadata.Sheet sheet) { public void currentSheet(com.alibaba.excel.write.metadata.Sheet sheet) {
if (sheet == null) { if (sheet == null) {
throw new IllegalArgumentException("Sheet argument cannot be null"); throw new IllegalArgumentException("Sheet argument cannot be null");
} }
if (sheet.getSheetNo() == null || sheet.getSheetNo() <= 0) { if (sheet.getSheetNo() == null || sheet.getSheetNo() <= 0) {
LOGGER.info("Sheet number is null");
sheet.setSheetNo(0); sheet.setSheetNo(0);
} }
if (currentWorkbookHolder.getHasBeenInitializedSheet().containsKey(sheet.getSheetNo())) { 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); currentSheetHolder = new SheetHolder(sheet, currentWorkbookHolder);
currentWorkbookHolder.getHasBeenInitializedSheet().put(sheet.getSheetNo(), currentSheetHolder); currentWorkbookHolder.getHasBeenInitializedSheet().put(sheet.getSheetNo(), currentSheetHolder);
currentTableHolder = null; 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; Sheet currentSheet;
try { try {
currentSheet = currentWorkbookHolder.getWorkbook().getSheetAt(sheet.getSheetNo()); currentSheet = currentWorkbookHolder.getWorkbook().getSheetAt(sheet.getSheetNo());
@ -303,7 +305,7 @@ public class WriteContextImpl implements WriteContext {
initHead(currentTableHolder.getExcelHeadProperty()); initHead(currentTableHolder.getExcelHeadProperty());
} }
private void initCurrentTableHolder(com.alibaba.excel.metadata.Table table) { private void initCurrentTableHolder(Table table) {
currentTableHolder = new TableHolder(table, currentSheetHolder, currentWorkbookHolder); currentTableHolder = new TableHolder(table, currentSheetHolder, currentWorkbookHolder);
currentSheetHolder.getHasBeenInitializedTable().put(table.getTableNo(), currentTableHolder); currentSheetHolder.getHasBeenInitializedTable().put(table.getTableNo(), currentTableHolder);
currentWriteConfiguration = 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 java.util.List;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.handler.WriteHandler;
/** /**
* Basic parameter * Basic parameter
@ -13,18 +11,6 @@ import com.alibaba.excel.write.handler.WriteHandler;
* @author zhuangjiaju * @author zhuangjiaju
**/ **/
public class BasicParameter { 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} * 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} * You can only choose one of the {@link BasicParameter#head} and {@link BasicParameter#clazz}
*/ */
private Class clazz; private Class clazz;
/**
* Need Head
*/
private Boolean needHead;
/** /**
* Custom type conversions override the default * Custom type conversions override the default
*/ */
private List<Converter> customConverterList = new ArrayList<Converter>(); 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 * Automatic trim includes sheet name and content
*/ */
private Boolean autoTrim; private Boolean autoTrim;
/**
public Integer getReadHeadRowNumber() { * true if date uses 1904 windowing, or false if using 1900 date windowing.
return readHeadRowNumber; *
} * default is false
*
public void setReadHeadRowNumber(Integer readHeadRowNumber) { * @return
this.readHeadRowNumber = readHeadRowNumber; */
} private Boolean use1904windowing;
public Integer getWriteRelativeHeadRowIndex() {
return writeRelativeHeadRowIndex;
}
public void setWriteRelativeHeadRowIndex(Integer writeRelativeHeadRowIndex) {
this.writeRelativeHeadRowIndex = writeRelativeHeadRowIndex;
}
public List<List<String>> getHead() { public List<List<String>> getHead() {
return head; return head;
@ -86,14 +52,6 @@ public class BasicParameter {
this.clazz = clazz; this.clazz = clazz;
} }
public Boolean getNeedHead() {
return needHead;
}
public void setNeedHead(Boolean needHead) {
this.needHead = needHead;
}
public List<Converter> getCustomConverterList() { public List<Converter> getCustomConverterList() {
return customConverterList; return customConverterList;
} }
@ -102,22 +60,6 @@ public class BasicParameter {
this.customConverterList = customConverterList; 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() { public Boolean getAutoTrim() {
return autoTrim; return autoTrim;
} }
@ -125,4 +67,12 @@ public class BasicParameter {
public void setAutoTrim(Boolean autoTrim) { public void setAutoTrim(Boolean autoTrim) {
this.autoTrim = 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.List;
import java.util.Map; 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 * @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; private String sheetName;
/** /**
* column with
*
* @deprecated please use {@link SimpleColumnWidthStyleStrategy}
*/ */
@Deprecated private Class<? extends BaseRowModel> clazz;
private Map<Integer, Integer> columnWidthMap = new HashMap<Integer, Integer>();
/**
*/
private List<List<String>> head;
/** /**
* *
* @deprecated please use{@link RowCellStyleStrategy}
*/ */
@Deprecated
private TableStyle tableStyle; private TableStyle tableStyle;
public Sheet() {
super();
}
/** /**
* Create sheet * column with
*
* @param sheetNo
* @param sheetName
*/ */
public Sheet(Integer sheetNo, String sheetName) { private Map<Integer,Integer> columnWidthMap = new HashMap<Integer, Integer>();
super();
this.sheetNo = sheetNo;
this.sheetName = sheetName;
}
/** /**
* It was 'sheetNo' starting from 1 and now it is starting from 0
* *
* @param sheetNo
* @param readHeadRowNumber
* @deprecated please use {@link ExcelWriterSheetBuilder#build()}
*/ */
@Deprecated private Boolean autoWidth = Boolean.FALSE;
public Sheet(int sheetNo, int readHeadRowNumber) {
super();
this.sheetNo = sheetNo - 1;
setReadHeadRowNumber(readHeadRowNumber);
}
/** /**
* 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) { public Sheet(int sheetNo) {
super(); this.sheetNo = sheetNo;
this.sheetNo = sheetNo - 1;
} }
/** public Sheet(int sheetNo, int headLineMun) {
* It was 'sheetNo' starting from 1 and now it is starting from 0 this.sheetNo = sheetNo;
* this.headLineMun = headLineMun;
* @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, Class<? extends BaseRowModel> clazz) {
* It was 'sheetNo' starting from 1 and now it is starting from 0 this.sheetNo = sheetNo;
* this.headLineMun = headLineMun;
* @param sheetNo this.clazz = clazz;
* @param readHeadRowNumber }
* @param clazz
* @deprecated please use {@link ExcelWriterSheetBuilder#build()} public Sheet(int sheetNo, int headLineMun, Class<? extends BaseRowModel> clazz, String sheetName,
*/ List<List<String>> head) {
@Deprecated this.sheetNo = sheetNo;
public Sheet(int sheetNo, int readHeadRowNumber, Class clazz, String sheetName, List<List<String>> head) { this.clazz = clazz;
super(); this.headLineMun = headLineMun;
this.sheetNo = sheetNo - 1;
this.sheetName = sheetName; this.sheetName = sheetName;
this.head = head;
}
setReadHeadRowNumber(readHeadRowNumber); public List<List<String>> getHead() {
setClazz(clazz); return head;
setHead(head);
} }
public Integer getSheetNo() { 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;
}
public void setHeadLineMun(int headLineMun) {
this.headLineMun = headLineMun;
}
public int getSheetNo() {
return sheetNo; return sheetNo;
} }
public void setSheetNo(Integer sheetNo) { public void setSheetNo(int sheetNo) {
this.sheetNo = sheetNo; this.sheetNo = sheetNo;
} }
@ -130,6 +119,16 @@ public class Sheet extends BasicParameter {
this.sheetName = sheetName; this.sheetName = sheetName;
} }
public TableStyle getTableStyle() {
return tableStyle;
}
public void setTableStyle(TableStyle tableStyle) {
this.tableStyle = tableStyle;
}
public Map<Integer, Integer> getColumnWidthMap() { public Map<Integer, Integer> getColumnWidthMap() {
return columnWidthMap; return columnWidthMap;
} }
@ -138,36 +137,32 @@ public class Sheet extends BasicParameter {
this.columnWidthMap = columnWidthMap; this.columnWidthMap = columnWidthMap;
} }
public TableStyle getTableStyle() { @Override
return tableStyle; public String toString() {
return "Sheet{" +
"headLineMun=" + headLineMun +
", sheetNo=" + sheetNo +
", sheetName='" + sheetName + '\'' +
", clazz=" + clazz +
", head=" + head +
", tableStyle=" + tableStyle +
", columnWidthMap=" + columnWidthMap +
'}';
} }
public void setTableStyle(TableStyle tableStyle) { public Boolean getAutoWidth() {
this.tableStyle = tableStyle; return autoWidth;
} }
/** public void setAutoWidth(Boolean autoWidth) {
* this.autoWidth = autoWidth;
* @param writeRelativeHeadRowIndex
* @deprecated please use {@link Sheet#setWriteRelativeHeadRowIndex(Integer)}
*/
@Deprecated
public void setStartRow(int writeRelativeHeadRowIndex) {
setWriteRelativeHeadRowIndex(writeRelativeHeadRowIndex);
} }
/** public int getStartRow() {
* return startRow;
* @param readHeadRowNumber
* @deprecated please use {@link Sheet#setReadHeadRowNumber(Integer)} )}
*/
@Deprecated
public void setHeadLineMun(int readHeadRowNumber) {
setReadHeadRowNumber(readHeadRowNumber);
} }
@Override public void setStartRow(int startRow) {
public String toString() { this.startRow = startRow;
return "Sheet{" + "sheetNo=" + sheetNo + ", sheetName='" + sheetName + '\'' + '}';
} }
} }

55
src/main/java/com/alibaba/excel/metadata/Table.java

@ -1,51 +1,60 @@
package com.alibaba.excel.metadata; package com.alibaba.excel.metadata;
import com.alibaba.excel.write.style.RowCellStyleStrategy; import java.util.List;
/** /**
* table
*
* @author jipengfei * @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; private TableStyle tableStyle;
public Table() { public TableStyle getTableStyle() {
super(); return tableStyle;
}
public void setTableStyle(TableStyle tableStyle) {
this.tableStyle = tableStyle;
} }
public Table(Integer tableNo) { public Table(Integer tableNo) {
super();
this.tableNo = tableNo; this.tableNo = tableNo;
} }
public Integer getTableNo() { public Class<? extends BaseRowModel> getClazz() {
return tableNo; return clazz;
} }
public void setTableNo(Integer tableNo) { public void setClazz(Class<? extends BaseRowModel> clazz) {
this.tableNo = tableNo; this.clazz = clazz;
} }
public TableStyle getTableStyle() { public List<List<String>> getHead() {
return tableStyle; return head;
} }
public void setTableStyle(TableStyle tableStyle) { public void setHead(List<List<String>> head) {
this.tableStyle = tableStyle; this.head = head;
} }
@Override public int getTableNo() {
public String toString() { return tableNo;
return "Table{" + "tableNo=" + 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. * true if date uses 1904 windowing, or false if using 1900 date windowing.
* *
* default is false
*
* @return * @return
*/ */
private Boolean use1904windowing; 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.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat; 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.enums.HeadKindEnum;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.CellRange;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.holder.write.WorkbookHolder;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
/** /**
@ -55,10 +48,8 @@ public class ExcelHeadProperty {
* Configuration column information * Configuration column information
*/ */
private Map<Integer, ExcelContentProperty> contentPropertyMap; 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; this.headClazz = headClazz;
headMap = new TreeMap<Integer, Head>(); headMap = new TreeMap<Integer, Head>();
contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>(); contentPropertyMap = new TreeMap<Integer, ExcelContentProperty>();
@ -72,7 +63,7 @@ public class ExcelHeadProperty {
headKind = HeadKindEnum.STRING; headKind = HeadKindEnum.STRING;
} else { } else {
// convert headClazz to head // convert headClazz to head
initColumnProperties(workbookHolder); initColumnProperties(convertAllFiled);
} }
initHeadRowNumber(); initHeadRowNumber();
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -104,7 +95,7 @@ public class ExcelHeadProperty {
} }
} }
private void initColumnProperties(WorkbookHolder workbookHolder) { private void initColumnProperties(Boolean convertAllFiled) {
if (headClazz == null) { if (headClazz == null) {
return; return;
} }
@ -127,7 +118,7 @@ public class ExcelHeadProperty {
continue; continue;
} }
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
if (excelProperty == null && (workbookHolder == null || !workbookHolder.getConvertAllFiled())) { if (excelProperty == null && convertAllFiled != null && !convertAllFiled) {
continue; continue;
} }
if (excelProperty == null || excelProperty.index() < 0) { if (excelProperty == null || excelProperty.index() < 0) {
@ -141,35 +132,24 @@ public class ExcelHeadProperty {
customFiledMap.put(excelProperty.index(), field); 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; int index = 0;
for (Field field : defaultFieldList) { for (Field field : defaultFieldList) {
while (customFiledMap.containsKey(index)) { while (customFiledMap.containsKey(index)) {
initOneColumnProperty(index, customFiledMap.get(index), headStyle, contentStyle, columnWidth, initOneColumnProperty(index, customFiledMap.get(index));
workbookHolder);
customFiledMap.remove(index); customFiledMap.remove(index);
index++; index++;
} }
initOneColumnProperty(index, field, headStyle, contentStyle, columnWidth, workbookHolder); initOneColumnProperty(index, field);
index++; index++;
} }
for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) { for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) {
initOneColumnProperty(index, entry.getValue(), headStyle, contentStyle, columnWidth, workbookHolder); initOneColumnProperty(index, entry.getValue());
index++; index++;
} }
headKind = HeadKindEnum.CLASS; headKind = HeadKindEnum.CLASS;
} }
private void initOneColumnProperty(int index, Field field, HeadStyle parentHeadStyle, private void initOneColumnProperty(int index, Field field) {
ContentStyle parentContentStyle, ColumnWidth parentColumnWidth, WorkbookHolder workbookHolder) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
List<String> tmpHeadList = new ArrayList<String>(); List<String> tmpHeadList = new ArrayList<String>();
if (excelProperty != null) { if (excelProperty != null) {
@ -181,36 +161,13 @@ public class ExcelHeadProperty {
tmpHeadList.add(field.getName()); tmpHeadList.add(field.getName());
} }
Head head = new Head(index, field.getName(), tmpHeadList); 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 excelContentProperty = new ExcelContentProperty();
excelContentProperty.setHead(head); excelContentProperty.setHead(head);
excelContentProperty.setField(field); excelContentProperty.setField(field);
ContentStyle contentStyle = field.getAnnotation(ContentStyle.class);
if (contentStyle == null) {
contentStyle = parentContentStyle;
}
excelContentProperty.setCellStyleProperty(CellStyleProperty.build(contentStyle));
excelContentProperty excelContentProperty
.setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class))); .setDateTimeFormatProperty(DateTimeFormatProperty.build(field.getAnnotation(DateTimeFormat.class)));
excelContentProperty excelContentProperty
.setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class))); .setNumberFormatProperty(NumberFormatProperty.build(field.getAnnotation(NumberFormat.class)));
if (workbookHolder != null && workbookHolder.getReadGlobalProperty() != null) {
excelContentProperty.setGlobalProperty(workbookHolder.getReadGlobalProperty());
}
headMap.put(index, head); headMap.put(index, head);
contentPropertyMap.put(index, excelContentProperty); contentPropertyMap.put(index, excelContentProperty);
} }
@ -259,79 +216,4 @@ public class ExcelHeadProperty {
this.contentPropertyMap = contentPropertyMap; 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; package com.alibaba.excel.read.builder;
import com.alibaba.excel.metadata.Workbook; import com.alibaba.excel.write.metadata.Workbook;
/** /**
* Build ExcelWriter * 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.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head; 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.ExcelContentProperty;
import com.alibaba.excel.metadata.property.ExcelHeadProperty; 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -10,8 +10,9 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.CellStyle; 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.metadata.TableStyle;
import com.alibaba.excel.write.metadata.holder.AbstractWriteConfiguration;
import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.style.RowCellStyleStrategy; import com.alibaba.excel.write.style.RowCellStyleStrategy;
@ -21,7 +22,7 @@ import com.alibaba.excel.write.style.RowCellStyleStrategy;
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class TableHolder extends AbstractWriteConfiguration { public class ReadTableHolder extends AbstractReadHolder {
private static final Logger LOGGER = LoggerFactory.getLogger(TableHolder.class); private static final Logger LOGGER = LoggerFactory.getLogger(TableHolder.class);
/*** /***
@ -35,9 +36,9 @@ public class TableHolder extends AbstractWriteConfiguration {
/** /**
* current table param * 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) { WorkbookHolder workbookHolder) {
TableHolder tableHolder = new TableHolder(); TableHolder tableHolder = new TableHolder();
tableHolder.setTableParam(table); tableHolder.setTableParam(table);
@ -95,7 +96,7 @@ public class TableHolder extends AbstractWriteConfiguration {
* Compatible with old code * Compatible with old code
*/ */
@Deprecated @Deprecated
private static void compatibleOldCode(com.alibaba.excel.metadata.Table table) { private static void compatibleOldCode(Table table) {
if (table.getTableStyle() != null) { if (table.getTableStyle() != null) {
final TableStyle tableStyle = table.getTableStyle(); final TableStyle tableStyle = table.getTableStyle();
if (table.getCustomWriteHandlerList() == null) { 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.File;
import java.io.InputStream; 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.Converter;
import com.alibaba.excel.converters.ConverterKey; import com.alibaba.excel.converters.ConverterKey;
import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.converters.DefaultConverterLoader;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.read.listener.ModelBuildEventListener; import com.alibaba.excel.read.listener.ModelBuildEventListener;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.handler.DefaultWriteHandlerLoader; import com.alibaba.excel.write.handler.DefaultWriteHandlerLoader;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
@ -30,35 +32,25 @@ import com.alibaba.excel.write.handler.WriteHandler;
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class WorkbookHolder extends AbstractWriteConfiguration { public class ReadWorkbookHolder extends AbstractReadHolder {
private static final Logger LOGGER = LoggerFactory.getLogger(WorkbookHolder.class); private static final Logger LOGGER = LoggerFactory.getLogger(ReadWorkbookHolder.class);
/*** /***
* poi Workbook * poi Workbook
*/ */
private Workbook workbook; private Workbook workbook;
/**
* prevent duplicate creation of sheet objects
*/
private Map<Integer, SheetHolder> hasBeenInitializedSheet;
/** /**
* current param * current param
*/ */
private com.alibaba.excel.metadata.Workbook workbookParam; private ReadWorkbook readWorkbook;
/** /**
* Final output stream * Read InputStream
*/
private OutputStream outputStream;
/**
* <li>write: Template input stream
* <li>read: Read InputStream
* <p> * <p>
* If 'inputStream' and 'file' all not empty,file first * If 'inputStream' and 'file' all not empty,file first
*/ */
private InputStream inputStream; private InputStream inputStream;
/** /**
* <li>write: Template file * Read file
* <li>read: Read file
* <p> * <p>
* If 'inputStream' and 'file' all not empty,file first * 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} * A cache that stores temp data to save memory.Default use {@link com.alibaba.excel.cache.Ehcache}
*/ */
private ReadCache readCache; private ReadCache readCache;
/**
* true if date uses 1904 windowing, or false if using 1900 date windowing.
*
* @return
*/
private Boolean use1904windowing;
/** /**
* Mmandatory use 'inputStream' * Mmandatory use 'inputStream'
@ -115,7 +101,7 @@ public class WorkbookHolder extends AbstractWriteConfiguration {
@Deprecated @Deprecated
private com.alibaba.excel.event.WriteHandler writeHandler; 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 workbookHolder = buildBaseWorkbookHolder(workbook);
workbookHolder.setNewInitialization(Boolean.TRUE); workbookHolder.setNewInitialization(Boolean.TRUE);
if (workbook.getNeedHead() == null) { if (workbook.getNeedHead() == null) {
@ -149,7 +135,7 @@ public class WorkbookHolder extends AbstractWriteConfiguration {
return workbookHolder; 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); WorkbookHolder workbookHolder = buildBaseWorkbookHolder(workbook);
if (workbook.getFile() == null && workbookHolder.getInputStream() == null) { if (workbook.getFile() == null && workbookHolder.getInputStream() == null) {
throw new ExcelAnalysisException("Read excel 'file' and 'inputStream' cannot be empty at the same time!"); 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; 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 workbookHolder = new WorkbookHolder();
workbookHolder.setUse1904windowing(workbook.getUse1904windowing()); workbookHolder.setUse1904windowing(workbook.getUse1904windowing());
workbookHolder.setWorkbookParam(workbook); workbookHolder.setWorkbookParam(workbook);
@ -218,11 +204,11 @@ public class WorkbookHolder extends AbstractWriteConfiguration {
this.hasBeenInitializedSheet = hasBeenInitializedSheet; this.hasBeenInitializedSheet = hasBeenInitializedSheet;
} }
public com.alibaba.excel.metadata.Workbook getWorkbookParam() { public com.alibaba.excel.write.metadata.Workbook getWorkbookParam() {
return workbookParam; return workbookParam;
} }
public void setWorkbookParam(com.alibaba.excel.metadata.Workbook workbookParam) { public void setWorkbookParam(com.alibaba.excel.write.metadata.Workbook workbookParam) {
this.workbookParam = workbookParam; this.workbookParam = workbookParam;
} }
@ -321,4 +307,9 @@ public class WorkbookHolder extends AbstractWriteConfiguration {
public void setReadTempFile(File readTempFile) { public void setReadTempFile(File readTempFile) {
this.readTempFile = 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.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 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; import com.alibaba.excel.support.ExcelTypeEnum;
/** /**
@ -42,7 +42,7 @@ public class WorkBookUtil {
return new HSSFWorkbook(); 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() + ""); 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 java.util.List;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.write.metadata.Table;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; 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.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.write.metadata.Table;
import com.alibaba.excel.metadata.holder.write.WriteConfiguration; import com.alibaba.excel.write.metadata.holder.WriteConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.POITempFile; 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.CellWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.Workbook;
import net.sf.cglib.beans.BeanMap; import net.sf.cglib.beans.BeanMap;
@ -35,7 +36,7 @@ public class ExcelBuilderImpl implements ExcelBuilder {
private WriteContext context; private WriteContext context;
public ExcelBuilderImpl(com.alibaba.excel.metadata.Workbook workbook) { public ExcelBuilderImpl(Workbook workbook) {
// 初始化时候创建临时缓存目录,用于规避POI在并发写bug // 初始化时候创建临时缓存目录,用于规避POI在并发写bug
POITempFile.createPOIFilesDirectory(); POITempFile.createPOIFilesDirectory();
context = new WriteContextImpl(workbook); 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.ExcelWriter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.exception.ExcelGenerateException; 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.support.ExcelTypeEnum;
import com.alibaba.excel.write.handler.WriteHandler; 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 java.util.List;
import com.alibaba.excel.converters.Converter; 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; 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 java.util.List;
import com.alibaba.excel.converters.Converter; 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; 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 org.apache.poi.ss.usermodel.Row;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.metadata.holder.TableHolder;
import com.sun.istack.internal.Nullable; 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 org.apache.poi.ss.usermodel.Row;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.metadata.holder.TableHolder;
import com.sun.istack.internal.Nullable; 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; package com.alibaba.excel.write.handler;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.write.metadata.holder.WorkbookHolder;
/** /**
* intercepts handle sheet creation * 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; 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 * 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 org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.metadata.holder.TableHolder;
import com.alibaba.excel.write.handler.CellWriteHandler; 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.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; 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.support.ExcelTypeEnum;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
@ -15,14 +12,13 @@ import com.alibaba.excel.write.handler.WriteHandler;
* *
* @author zhuangjiaju * @author zhuangjiaju
**/ **/
public class Workbook extends BasicParameter { public class WriteWorkbook extends WriteBasicParameter {
/** /**
* Excel type * Excel type
*/ */
private ExcelTypeEnum excelType; private ExcelTypeEnum excelType;
/** /**
* Read final output stream * Final output stream
*/ */
private OutputStream outputStream; private OutputStream outputStream;
/** /**
@ -31,7 +27,7 @@ public class Workbook extends BasicParameter {
* <p> * <p>
* If 'inputStream' and 'file' all not empty,file first * If 'inputStream' and 'file' all not empty,file first
*/ */
private InputStream inputStream; private InputStream templateInputStream;
/** /**
* <li>write: Template file * <li>write: Template file
@ -39,30 +35,19 @@ public class Workbook extends BasicParameter {
* <p> * <p>
* If 'inputStream' and 'file' all not empty,file first * If 'inputStream' and 'file' all not empty,file first
*/ */
private File file; private File templateFile;
/** /**
* Default true * Default true
*/ */
private Boolean autoCloseStream; private Boolean autoCloseStream;
/** /**
* This object can be read in the Listener {@link AnalysisEventListener#invoke(Object, AnalysisContext)} * Mandatory use 'inputStream' .Default is false
* {@link AnalysisContext#getCustom()}
*
*/
private Object readCustomObject;
/**
* A cache that stores temp data to save memory.Default use {@link com.alibaba.excel.cache.Ehcache}
*/ */
private ReadCache readCache; private Boolean mandatoryUseInputStream;
/** /**
* true if date uses 1904 windowing, or false if using 1900 date windowing. * The default is all excel objects.Default is true.
* * <li>if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a field.
* @return * <li>if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed.
*/
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.
* *
* @deprecated Just to be compatible with historical data, The default is always going to be convert all 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; this.outputStream = outputStream;
} }
public InputStream getInputStream() { public InputStream getTemplateInputStream() {
return inputStream; return templateInputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public File getFile() {
return file;
} }
public void setFile(File file) { public void setTemplateInputStream(InputStream templateInputStream) {
this.file = file; this.templateInputStream = templateInputStream;
} }
public com.alibaba.excel.event.WriteHandler getWriteHandler() { public File getTemplateFile() {
return writeHandler; return templateFile;
} }
public void setWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) { public void setTemplateFile(File templateFile) {
this.writeHandler = writeHandler; this.templateFile = templateFile;
} }
public Boolean getAutoCloseStream() { public Boolean getAutoCloseStream() {
@ -124,35 +101,27 @@ public class Workbook extends BasicParameter {
this.autoCloseStream = autoCloseStream; this.autoCloseStream = autoCloseStream;
} }
public Boolean getConvertAllFiled() { public Boolean getMandatoryUseInputStream() {
return convertAllFiled; return mandatoryUseInputStream;
}
public void setConvertAllFiled(Boolean convertAllFiled) {
this.convertAllFiled = convertAllFiled;
}
public Object getReadCustomObject() {
return readCustomObject;
} }
public void setReadCustomObject(Object readCustomObject) { public void setMandatoryUseInputStream(Boolean mandatoryUseInputStream) {
this.readCustomObject = readCustomObject; this.mandatoryUseInputStream = mandatoryUseInputStream;
} }
public ReadCache getReadCache() { public Boolean getConvertAllFiled() {
return readCache; return convertAllFiled;
} }
public void setReadCache(ReadCache readCache) { public void setConvertAllFiled(Boolean convertAllFiled) {
this.readCache = readCache; this.convertAllFiled = convertAllFiled;
} }
public Boolean getUse1904windowing() { public com.alibaba.excel.event.WriteHandler getWriteHandler() {
return use1904windowing; return writeHandler;
} }
public void setUse1904windowing(Boolean use1904windowing) { public void setWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) {
this.use1904windowing = use1904windowing; 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.ArrayList;
import java.util.HashMap; 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.Cell;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter; 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.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.NotRepeatExecutor;
import com.alibaba.excel.event.Order; import com.alibaba.excel.event.Order;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.metadata.AbstractHolder;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.CellStyle; import com.alibaba.excel.metadata.CellStyle;
import com.alibaba.excel.metadata.Head; 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.CellStyleProperty;
import com.alibaba.excel.metadata.property.ExcelContentProperty; 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.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.CellWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler; import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.SheetWriteHandler; import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler; 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.AbstractColumnCellStyleStrategy;
import com.alibaba.excel.write.style.RowCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy; import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractHeadColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
/** /**
* sheet holder * Write holder configuration
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public abstract class AbstractWriteConfiguration public abstract class AbstractWriteHolder extends AbstractHolder implements WriteHolder {
implements WriteConfiguration, ReadConfiguration, ReadListenerRegistryCenter {
/** /**
* Need Head * Need Head
*/ */
private Boolean needHead; 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. * Writes the head relative to the existing contents of the sheet. Indexes are zero-based.
*/ */
private Integer writeRelativeHeadRowIndex; private Integer relativeHeadRowIndex;
/**
* Record whether it's new or from cache
*/
private Boolean newInitialization;
/** /**
* Count the number of added heads when read sheet. * Excel head property
*
* <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}
*/ */
private List<List<String>> head; private ExcelWriteHeadProperty excelWriteHeadProperty;
/** /**
* You can only choose one of the {@link AbstractWriteConfiguration#head} and * Write handler for workbook
* {@link AbstractWriteConfiguration#clazz}
*/ */
private Class clazz; private Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap;
/** /**
* Automatic trim includes sheet name and content * Converter for workbook
*/ */
private Boolean autoTrim; private Map<Class, Converter> converterMap;
public Boolean getNeedHead() { public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder,
return needHead; Boolean convertAllFiled) {
super(writeBasicParameter, parentAbstractWriteHolder);
if (writeBasicParameter.getUse1904windowing() == null) {
if (parentAbstractWriteHolder == null) {
getGlobalConfiguration().setUse1904windowing(Boolean.FALSE);
} else {
getGlobalConfiguration()
.setUse1904windowing(parentAbstractWriteHolder.getGlobalConfiguration().getUse1904windowing());
} }
} else {
public void setNeedHead(Boolean needHead) { getGlobalConfiguration().setUse1904windowing(writeBasicParameter.getUse1904windowing());
this.needHead = needHead;
} }
public Map<Class<? extends WriteHandler>, List<WriteHandler>> getWriteHandlerMap() { if (writeBasicParameter.getNeedHead() == null) {
return writeHandlerMap; if (parentAbstractWriteHolder == null) {
this.needHead = Boolean.TRUE;
} else {
this.needHead = parentAbstractWriteHolder.getNeedHead();
} }
} else {
public void setWriteHandlerMap(Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap) { this.needHead = writeBasicParameter.getNeedHead();
this.writeHandlerMap = writeHandlerMap;
} }
public ExcelHeadProperty getExcelHeadProperty() { if (writeBasicParameter.getRelativeHeadRowIndex() == null) {
return excelHeadProperty; if (parentAbstractWriteHolder == null) {
this.relativeHeadRowIndex = 0;
} else {
this.relativeHeadRowIndex = parentAbstractWriteHolder.getRelativeHeadRowIndex();
} }
} else {
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { this.relativeHeadRowIndex = writeBasicParameter.getRelativeHeadRowIndex();
this.excelHeadProperty = excelHeadProperty;
} }
public Integer getWriteRelativeHeadRowIndex() { // Initialization property
return writeRelativeHeadRowIndex; this.excelWriteHeadProperty = new ExcelWriteHeadProperty(getClazz(), getHead(), convertAllFiled);
}
public void setWriteRelativeHeadRowIndex(Integer writeRelativeHeadRowIndex) { // Compatible with old code
this.writeRelativeHeadRowIndex = writeRelativeHeadRowIndex; compatibleOldCode(writeBasicParameter);
}
public Boolean getNewInitialization() { // Set writeHandlerMap
return newInitialization; 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) { Map<Class<? extends WriteHandler>, List<WriteHandler>> parentWriteHandlerMap = null;
this.newInitialization = newInitialization; if (parentAbstractWriteHolder != null) {
parentWriteHandlerMap = parentAbstractWriteHolder.getWriteHandlerMap();
} }
this.writeHandlerMap = sortAndClearUpHandler(handlerList, parentWriteHandlerMap);
public List<List<String>> getHead() { // Set converterMap
return head; if (parentAbstractWriteHolder == null) {
this.converterMap = DefaultConverterLoader.loadDefaultWriteConverter();
} else {
this.converterMap = new HashMap<Class, Converter>(parentAbstractWriteHolder.getConverterMap());
} }
if (writeBasicParameter.getCustomConverterList() != null
public void setHead(List<List<String>> head) { && !writeBasicParameter.getCustomConverterList().isEmpty()) {
this.head = head; 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() { @Deprecated
return readListenerList; private void compatibleOldCodeCreateRowCellStyleStrategy(WriteBasicParameter writeBasicParameter,
TableStyle tableStyle) {
if (tableStyle == null) {
return;
} }
if (writeBasicParameter.getCustomWriteHandlerList() == null) {
public void setReadListenerList(List<ReadListener> readListenerList) { writeBasicParameter.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
this.readListenerList = readListenerList;
} }
CellStyle headCellStyle = new CellStyle();
public Map<ConverterKey, Converter> getReadConverterMap() { headCellStyle.setFont(tableStyle.getTableHeadFont());
return readConverterMap; 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) { @Deprecated
this.readConverterMap = readConverterMap; private void compatibleOldCodeCreateHeadColumnWidthStyleStrategy(WriteBasicParameter writeBasicParameter,
final Map<Integer, Integer> columnWidthMap) {
if (columnWidthMap == null || columnWidthMap.isEmpty()) {
return;
} }
if (writeBasicParameter.getCustomWriteHandlerList() == null) {
public Map<Class, Converter> getWriteConverterMap() { writeBasicParameter.setCustomWriteHandlerList(new ArrayList<WriteHandler>());
return writeConverterMap;
} }
writeBasicParameter.getCustomWriteHandlerList().add(new AbstractHeadColumnWidthStyleStrategy() {
public void setWriteConverterMap(Map<Class, Converter> writeConverterMap) { @Override
this.writeConverterMap = writeConverterMap; protected Integer columnWidth(Head head) {
if (columnWidthMap.containsKey(head.getColumnIndex())) {
columnWidthMap.get(head.getColumnIndex());
} }
return 20;
public Integer getReadHeadRowNumber() {
return readHeadRowNumber;
} }
});
public void setReadHeadRowNumber(Integer readHeadRowNumber) {
this.readHeadRowNumber = readHeadRowNumber;
} }
@Override protected void initAnnotationConfig(List<WriteHandler> handlerList) {
public void register(AnalysisEventListener listener) { if (!HeadKindEnum.CLASS.equals(getExcelWriteHeadProperty().getHeadKind())) {
readListenerList.add(listener); return;
} }
Map<Integer, Head> headMap = getExcelWriteHeadProperty().getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMap = getExcelWriteHeadProperty().getContentPropertyMap();
@Override boolean hasCellStyle = false;
public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) { boolean hasColumnWidth = false;
List<CellData> cellDataList = (List<CellData>)event.getAnalysisResult(); for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
if (analysisContext.currentRowNum() > analysisContext.currentSheetHolder().getReadHeadRowNumber()) { if (entry.getValue().getCellStyleProperty() != null) {
for (ReadListener readListener : readListenerList) { hasCellStyle = true;
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);
} }
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 (hasColumnWidth) {
if (analysisContext.currentRowNum().equals(analysisContext.currentSheetHolder().getReadHeadRowNumber())) { dealColumnWidth(handlerList);
buildHead(analysisContext, cellDataList);
} }
dealRowHigh(handlerList, contentPropertyMap);
} }
@Override private void dealRowHigh(List<WriteHandler> handlerList, Map<Integer, ExcelContentProperty> contentPropertyMap) {
public void notifyAfterAllAnalysed(AnalysisContext analysisContext) { RowHeightProperty headRowHeightProperty = getExcelWriteHeadProperty().getHeadRowHeightProperty();
for (ReadListener readListener : readListenerList) { RowHeightProperty contentRowHeightProperty = getExcelWriteHeadProperty().getContentRowHeightProperty();
readListener.doAfterAllAnalysed(analysisContext); if (headRowHeightProperty == null && contentRowHeightProperty == null) {
return;
} }
Short headRowHeight = null;
if (headRowHeightProperty != null) {
headRowHeight = headRowHeightProperty.getHeight();
} }
Short contentRowHeight = null;
private void buildHead(AnalysisContext analysisContext, List<CellData> cellDataList) { if (contentRowHeightProperty != null) {
if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelHeadProperty().getHeadKind())) { contentRowHeight = contentRowHeightProperty.getHeight();
return;
} }
List<String> dataList = (List<String>)buildStringList(cellDataList, analysisContext.currentConfiguration()); handlerList.add(new SimpleRowHeightStyleStrategy(headRowHeight, contentRowHeight));
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++) { private void dealColumnWidth(List<WriteHandler> handlerList) {
String headString = dataList.get(i); WriteHandler columnWidthStyleStrategy = new AbstractColumnWidthStyleStrategy() {
if (StringUtils.isEmpty(headString)) { @Override
continue; protected void setColumnWidth(Sheet sheet, Cell cell, Head head) {
if (head == null) {
return;
} }
if (analysisContext.currentSheetHolder().getAutoTrim()) { if (head.getColumnWidthProperty() != null) {
headString = headString.trim(); 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); CellStyleProperty cellStyleProperty = head.getCellStyleProperty();
excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(),
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors());
} }
private Object buildStringList(List<CellData> data, ReadConfiguration readConfiguration) { @Override
List<String> list = new ArrayList<String>(); protected CellStyle contentCellStyle(Head head) {
for (CellData cellData : data) { if (head == null) {
Converter converter = readConfiguration.readConverterMap() return null;
.get(ConverterKey.buildConverterKey(String.class, cellData.getType()));
if (converter == null) {
throw new ExcelDataConvertException(
"Converter not found, convert " + cellData.getType() + " to String");
} }
try { ExcelContentProperty excelContentProperty = contentPropertyMap.get(head.getColumnIndex());
list.add((String)(converter.convertToJavaData(cellData, null))); if (excelContentProperty == null || excelContentProperty.getCellStyleProperty() == null) {
} catch (Exception e) { return null;
throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e);
} }
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( protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpHandler(
@ -359,136 +352,68 @@ public abstract class AbstractWriteConfiguration
return result; return result;
} }
protected void initAnnotationConfig(List<WriteHandler> handlerList) { public Boolean getNeedHead() {
if (!HeadKindEnum.CLASS.equals(getExcelHeadProperty().getHeadKind())) { return needHead;
return;
} }
Map<Integer, Head> headMap = getExcelHeadProperty().getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMap = getExcelHeadProperty().getContentPropertyMap();
boolean hasCellStyle = false; public void setNeedHead(Boolean needHead) {
boolean hasColumnWidth = false; this.needHead = needHead;
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) { public Map<Class<? extends WriteHandler>, List<WriteHandler>> getWriteHandlerMap() {
dealCellStyle(handlerList, contentPropertyMap); return writeHandlerMap;
}
if (hasColumnWidth) {
dealColumnWidth(handlerList);
}
dealRowHigh(handlerList, contentPropertyMap);
} }
private void dealRowHigh(List<WriteHandler> handlerList, Map<Integer, ExcelContentProperty> contentPropertyMap) { public void setWriteHandlerMap(Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap) {
RowHeightProperty headRowHeightProperty = excelHeadProperty.getHeadRowHeightProperty(); this.writeHandlerMap = writeHandlerMap;
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));
} }
private void dealColumnWidth(List<WriteHandler> handlerList) { public Map<Class, Converter> getConverterMap() {
WriteHandler columnWidthStyleStrategy = new AbstractColumnWidthStyleStrategy() { return converterMap;
@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);
} }
private void dealCellStyle(List<WriteHandler> handlerList, public void setConverterMap(Map<Class, Converter> converterMap) {
final Map<Integer, ExcelContentProperty> contentPropertyMap) { this.converterMap = converterMap;
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());
} }
@Override public ExcelWriteHeadProperty getExcelWriteHeadProperty() {
protected CellStyle contentCellStyle(Head head) { return excelWriteHeadProperty;
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);
} }
@Override public void setExcelWriteHeadProperty(ExcelWriteHeadProperty excelWriteHeadProperty) {
public Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap() { this.excelWriteHeadProperty = excelWriteHeadProperty;
return getWriteHandlerMap();
} }
@Override public Integer getRelativeHeadRowIndex() {
public boolean needHead() { return relativeHeadRowIndex;
return getNeedHead();
} }
@Override public void setRelativeHeadRowIndex(Integer relativeHeadRowIndex) {
public int writeRelativeHeadRowIndex() { this.relativeHeadRowIndex = relativeHeadRowIndex;
return getWriteRelativeHeadRowIndex();
} }
@Override @Override
public ExcelHeadProperty excelHeadProperty() { public ExcelWriteHeadProperty excelWriteHeadProperty() {
return getExcelHeadProperty(); return getExcelWriteHeadProperty();
} }
@Override @Override
public boolean isNew() { public Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap() {
return getNewInitialization(); return getWriteHandlerMap();
} }
@Override @Override
public List<ReadListener> readListenerList() { public Map<Class, Converter> converterMap() {
return getReadListenerList(); return getConverterMap();
} }
@Override @Override
public Map<ConverterKey, Converter> readConverterMap() { public boolean needHead() {
return getReadConverterMap(); return getNeedHead();
} }
@Override @Override
public Map<Class, Converter> writeConverterMap() { public int relativeHeadRowIndex() {
return getWriteConverterMap(); 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.List;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
/** /**
* *
* Get the corresponding configuration * Get the corresponding Holder
* *
* @author zhuangjiaju * @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 * What handler does the currently operated cell need to execute
@ -27,7 +32,7 @@ public interface WriteConfiguration {
* *
* @return * @return
*/ */
Map<Class, Converter> writeConverterMap(); Map<Class, Converter> converterMap();
/** /**
* Whether a header is required for the currently operated cell * 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. * 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 * @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.event.NotRepeatExecutor;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.metadata.holder.TableHolder;
import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.write.metadata.holder.WorkbookHolder;
import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.WorkbookWriteHandler; 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.event.NotRepeatExecutor;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.metadata.holder.TableHolder;
import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.CellWriteHandler;
import com.sun.istack.internal.Nullable; 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 org.apache.poi.ss.usermodel.Row;
import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.event.NotRepeatExecutor;
import com.alibaba.excel.metadata.holder.write.SheetHolder; import com.alibaba.excel.write.metadata.holder.SheetHolder;
import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.metadata.holder.TableHolder;
import com.alibaba.excel.write.handler.RowWriteHandler; 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.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; 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.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
import com.alibaba.excel.metadata.Table; import com.alibaba.excel.write.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import org.junit.Test; 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.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
public class ReadTest { 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.EasyExcelFactory;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Simple data test * 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.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Simple data test * 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.easyexcel.test.wirte.order.OrderData;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Order data test * 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.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Order data test * 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.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.write.metadata.Sheet;
/** /**
* Simple data test * Simple data test

Loading…
Cancel
Save