forked from fanruan/easyexcel
zhuangjiaju
5 years ago
67 changed files with 2233 additions and 869 deletions
@ -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; |
||||
} |
@ -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(); |
||||
} |
||||
|
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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(); |
||||
} |
@ -1,51 +1,60 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import com.alibaba.excel.write.style.RowCellStyleStrategy; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* table |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class Table extends BasicParameter { |
||||
public class Table { |
||||
/** |
||||
* Starting from 0 |
||||
*/ |
||||
private Integer tableNo; |
||||
private Class<? extends BaseRowModel> clazz; |
||||
|
||||
/** |
||||
*/ |
||||
private List<List<String>> head; |
||||
|
||||
/** |
||||
*/ |
||||
private int tableNo; |
||||
|
||||
/** |
||||
* |
||||
* @deprecated please use{@link RowCellStyleStrategy} |
||||
*/ |
||||
@Deprecated |
||||
private TableStyle tableStyle; |
||||
|
||||
public Table() { |
||||
super(); |
||||
public TableStyle getTableStyle() { |
||||
return tableStyle; |
||||
} |
||||
|
||||
public void setTableStyle(TableStyle tableStyle) { |
||||
this.tableStyle = tableStyle; |
||||
} |
||||
|
||||
public Table(Integer tableNo) { |
||||
super(); |
||||
this.tableNo = tableNo; |
||||
} |
||||
|
||||
public Integer getTableNo() { |
||||
return tableNo; |
||||
public Class<? extends BaseRowModel> getClazz() { |
||||
return clazz; |
||||
} |
||||
|
||||
public void setTableNo(Integer tableNo) { |
||||
this.tableNo = tableNo; |
||||
public void setClazz(Class<? extends BaseRowModel> clazz) { |
||||
this.clazz = clazz; |
||||
} |
||||
|
||||
public TableStyle getTableStyle() { |
||||
return tableStyle; |
||||
public List<List<String>> getHead() { |
||||
return head; |
||||
} |
||||
|
||||
public void setTableStyle(TableStyle tableStyle) { |
||||
this.tableStyle = tableStyle; |
||||
public void setHead(List<List<String>> head) { |
||||
this.head = head; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Table{" + "tableNo=" + tableNo + '}'; |
||||
public int getTableNo() { |
||||
return tableNo; |
||||
} |
||||
|
||||
public void setTableNo(int tableNo) { |
||||
this.tableNo = tableNo; |
||||
} |
||||
} |
||||
|
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
|
||||
} |
@ -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(); |
||||
|
||||
} |
@ -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(); |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
|
||||
|
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue