mirror of https://github.com/alibaba/easyexcel
zhuangjiaju
6 years ago
59 changed files with 1751 additions and 2961 deletions
@ -0,0 +1,27 @@
|
||||
package com.alibaba.excel.event; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
|
||||
/** |
||||
* Receives the return of each piece of data parsed |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public abstract class AbstractIgnoreExceptionReadListener<T> implements ReadListener<T> { |
||||
|
||||
/** |
||||
* All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the |
||||
* entire read will terminate. |
||||
* |
||||
* @param exception |
||||
* @param context |
||||
*/ |
||||
@Override |
||||
public void onException(Exception exception, AnalysisContext context) {} |
||||
|
||||
@Override |
||||
public boolean hasNext(AnalysisContext context) { |
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
package com.alibaba.excel.event; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
|
||||
/** |
||||
* Synchronous data reading |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SyncReadListener extends AnalysisEventListener<Object> { |
||||
private List<Object> list = new ArrayList<Object>(); |
||||
|
||||
@Override |
||||
public void invoke(Object object, AnalysisContext context) { |
||||
list.add(object); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) {} |
||||
|
||||
public List<Object> getList() { |
||||
return list; |
||||
} |
||||
|
||||
public void setList(List<Object> list) { |
||||
this.list = list; |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.alibaba.excel.exception; |
||||
|
||||
/** |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ExcelCommonException extends RuntimeException { |
||||
|
||||
public ExcelCommonException() {} |
||||
|
||||
public ExcelCommonException(String message) { |
||||
super(message); |
||||
} |
||||
|
||||
public ExcelCommonException(String message, Throwable cause) { |
||||
super(message, cause); |
||||
} |
||||
|
||||
public ExcelCommonException(Throwable cause) { |
||||
super(cause); |
||||
} |
||||
} |
@ -1,494 +0,0 @@
|
||||
package com.alibaba.excel.metadata.holder.read; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.TreeMap; |
||||
|
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.converters.ConverterKey; |
||||
import com.alibaba.excel.enums.HeadKindEnum; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.event.NotRepeatExecutor; |
||||
import com.alibaba.excel.event.Order; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.exception.ExcelDataConvertException; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.CellStyle; |
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.holder.write.WriteConfiguration; |
||||
import com.alibaba.excel.metadata.property.CellStyleProperty; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.metadata.property.ExcelHeadProperty; |
||||
import com.alibaba.excel.metadata.property.RowHeightProperty; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
import com.alibaba.excel.read.listener.ReadListenerRegistryCenter; |
||||
import com.alibaba.excel.read.listener.event.AnalysisFinishEvent; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
import com.alibaba.excel.write.handler.CellWriteHandler; |
||||
import com.alibaba.excel.write.handler.RowWriteHandler; |
||||
import com.alibaba.excel.write.handler.SheetWriteHandler; |
||||
import com.alibaba.excel.write.handler.WorkbookWriteHandler; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
import com.alibaba.excel.write.style.AbstractColumnCellStyleStrategy; |
||||
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy; |
||||
import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public abstract class AbstractWriteConfiguration |
||||
implements WriteConfiguration, ReadConfiguration, ReadListenerRegistryCenter { |
||||
/** |
||||
* Need Head |
||||
*/ |
||||
private Boolean needHead; |
||||
/** |
||||
* Write handler for workbook |
||||
*/ |
||||
private Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap; |
||||
/** |
||||
* Read listener |
||||
*/ |
||||
private List<ReadListener> readListenerList; |
||||
/** |
||||
* Converter for workbook |
||||
*/ |
||||
private Map<ConverterKey, Converter> readConverterMap; |
||||
/** |
||||
* Converter for workbook |
||||
*/ |
||||
private Map<Class, Converter> writeConverterMap; |
||||
/** |
||||
* Excel head property |
||||
*/ |
||||
private ExcelHeadProperty excelHeadProperty; |
||||
/** |
||||
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based. |
||||
*/ |
||||
private Integer writeRelativeHeadRowIndex; |
||||
/** |
||||
* Record whether it's new or from cache |
||||
*/ |
||||
private Boolean newInitialization; |
||||
/** |
||||
* Count the number of added heads when read sheet. |
||||
* |
||||
* <li>0 - This Sheet has no head ,since the first row are the data |
||||
* <li>1 - This Sheet has one row head , this is the default |
||||
* <li>2 - This Sheet has two row head ,since the third row is the data |
||||
*/ |
||||
private Integer readHeadRowNumber; |
||||
/** |
||||
* You can only choose one of the {@link AbstractWriteConfiguration#head} and |
||||
* {@link AbstractWriteConfiguration#clazz} |
||||
*/ |
||||
private List<List<String>> head; |
||||
/** |
||||
* You can only choose one of the {@link AbstractWriteConfiguration#head} and |
||||
* {@link AbstractWriteConfiguration#clazz} |
||||
*/ |
||||
private Class clazz; |
||||
/** |
||||
* Automatic trim includes sheet name and content |
||||
*/ |
||||
private Boolean autoTrim; |
||||
|
||||
public Boolean getNeedHead() { |
||||
return needHead; |
||||
} |
||||
|
||||
public void setNeedHead(Boolean needHead) { |
||||
this.needHead = needHead; |
||||
} |
||||
|
||||
public Map<Class<? extends WriteHandler>, List<WriteHandler>> getWriteHandlerMap() { |
||||
return writeHandlerMap; |
||||
} |
||||
|
||||
public void setWriteHandlerMap(Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap) { |
||||
this.writeHandlerMap = writeHandlerMap; |
||||
} |
||||
|
||||
public ExcelHeadProperty getExcelHeadProperty() { |
||||
return excelHeadProperty; |
||||
} |
||||
|
||||
public void setExcelHeadProperty(ExcelHeadProperty excelHeadProperty) { |
||||
this.excelHeadProperty = excelHeadProperty; |
||||
} |
||||
|
||||
public Integer getWriteRelativeHeadRowIndex() { |
||||
return writeRelativeHeadRowIndex; |
||||
} |
||||
|
||||
public void setWriteRelativeHeadRowIndex(Integer writeRelativeHeadRowIndex) { |
||||
this.writeRelativeHeadRowIndex = writeRelativeHeadRowIndex; |
||||
} |
||||
|
||||
public Boolean getNewInitialization() { |
||||
return newInitialization; |
||||
} |
||||
|
||||
public void setNewInitialization(Boolean newInitialization) { |
||||
this.newInitialization = newInitialization; |
||||
} |
||||
|
||||
public List<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 Boolean getAutoTrim() { |
||||
return autoTrim; |
||||
} |
||||
|
||||
public void setAutoTrim(Boolean autoTrim) { |
||||
this.autoTrim = autoTrim; |
||||
} |
||||
|
||||
public List<ReadListener> getReadListenerList() { |
||||
return readListenerList; |
||||
} |
||||
|
||||
public void setReadListenerList(List<ReadListener> readListenerList) { |
||||
this.readListenerList = readListenerList; |
||||
} |
||||
|
||||
public Map<ConverterKey, Converter> getReadConverterMap() { |
||||
return readConverterMap; |
||||
} |
||||
|
||||
public void setReadConverterMap(Map<ConverterKey, Converter> readConverterMap) { |
||||
this.readConverterMap = readConverterMap; |
||||
} |
||||
|
||||
public Map<Class, Converter> getWriteConverterMap() { |
||||
return writeConverterMap; |
||||
} |
||||
|
||||
public void setWriteConverterMap(Map<Class, Converter> writeConverterMap) { |
||||
this.writeConverterMap = writeConverterMap; |
||||
} |
||||
|
||||
public Integer getReadHeadRowNumber() { |
||||
return readHeadRowNumber; |
||||
} |
||||
|
||||
public void setReadHeadRowNumber(Integer readHeadRowNumber) { |
||||
this.readHeadRowNumber = readHeadRowNumber; |
||||
} |
||||
|
||||
@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.currentRowNum() > analysisContext.currentSheetHolder().getReadHeadRowNumber()) { |
||||
for (ReadListener readListener : readListenerList) { |
||||
try { |
||||
readListener.invoke(analysisContext.currentRowAnalysisResult(), analysisContext); |
||||
} catch (Exception e) { |
||||
for (ReadListener readListenerException : readListenerList) { |
||||
try { |
||||
readListenerException.onException(e, analysisContext); |
||||
} catch (Exception exception) { |
||||
throw new ExcelAnalysisException("Listen error!", exception); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
// Now is header
|
||||
if (analysisContext.currentRowNum().equals(analysisContext.currentSheetHolder().getReadHeadRowNumber())) { |
||||
buildHead(analysisContext, cellDataList); |
||||
} |
||||
} |
||||
|
||||
@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().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; |
||||
} |
||||
|
||||
protected Map<Class<? extends WriteHandler>, List<WriteHandler>> sortAndClearUpHandler( |
||||
List<WriteHandler> handlerList, Map<Class<? extends WriteHandler>, List<WriteHandler>> parentHandlerMap) { |
||||
// add
|
||||
if (parentHandlerMap != null) { |
||||
for (List<WriteHandler> parentHandlerList : parentHandlerMap.values()) { |
||||
handlerList.addAll(parentHandlerList); |
||||
} |
||||
} |
||||
// sort
|
||||
Map<Integer, List<WriteHandler>> orderExcelWriteHandlerMap = new TreeMap<Integer, List<WriteHandler>>(); |
||||
for (WriteHandler handler : handlerList) { |
||||
int order = Integer.MIN_VALUE; |
||||
if (handler instanceof Order) { |
||||
order = ((Order)handler).order(); |
||||
} |
||||
if (orderExcelWriteHandlerMap.containsKey(order)) { |
||||
orderExcelWriteHandlerMap.get(order).add(handler); |
||||
} else { |
||||
List<WriteHandler> tempHandlerList = new ArrayList<WriteHandler>(); |
||||
tempHandlerList.add(handler); |
||||
orderExcelWriteHandlerMap.put(order, tempHandlerList); |
||||
} |
||||
} |
||||
// clean up
|
||||
Set<String> alreadyExistedHandlerSet = new HashSet<String>(); |
||||
List<WriteHandler> cleanUpHandlerList = new ArrayList<WriteHandler>(); |
||||
for (Map.Entry<Integer, List<WriteHandler>> entry : orderExcelWriteHandlerMap.entrySet()) { |
||||
for (WriteHandler handler : entry.getValue()) { |
||||
if (handler instanceof NotRepeatExecutor) { |
||||
String uniqueValue = ((NotRepeatExecutor)handler).uniqueValue(); |
||||
if (alreadyExistedHandlerSet.contains(uniqueValue)) { |
||||
continue; |
||||
} |
||||
alreadyExistedHandlerSet.add(uniqueValue); |
||||
} |
||||
cleanUpHandlerList.add(handler); |
||||
} |
||||
} |
||||
// classify
|
||||
Map<Class<? extends WriteHandler>, List<WriteHandler>> result = |
||||
new HashMap<Class<? extends WriteHandler>, List<WriteHandler>>(); |
||||
result.put(WriteHandler.class, new ArrayList<WriteHandler>()); |
||||
result.put(WorkbookWriteHandler.class, new ArrayList<WriteHandler>()); |
||||
result.put(SheetWriteHandler.class, new ArrayList<WriteHandler>()); |
||||
result.put(RowWriteHandler.class, new ArrayList<WriteHandler>()); |
||||
result.put(CellWriteHandler.class, new ArrayList<WriteHandler>()); |
||||
for (WriteHandler writeHandler : cleanUpHandlerList) { |
||||
if (writeHandler instanceof CellWriteHandler) { |
||||
result.get(CellWriteHandler.class).add(writeHandler); |
||||
} |
||||
if (writeHandler instanceof RowWriteHandler) { |
||||
result.get(RowWriteHandler.class).add(writeHandler); |
||||
} |
||||
if (writeHandler instanceof SheetWriteHandler) { |
||||
result.get(SheetWriteHandler.class).add(writeHandler); |
||||
} |
||||
if (writeHandler instanceof SheetWriteHandler) { |
||||
result.get(SheetWriteHandler.class).add(writeHandler); |
||||
} |
||||
if (writeHandler instanceof WorkbookWriteHandler) { |
||||
result.get(WorkbookWriteHandler.class).add(writeHandler); |
||||
} |
||||
result.get(WriteHandler.class).add(writeHandler); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
protected void initAnnotationConfig(List<WriteHandler> handlerList) { |
||||
if (!HeadKindEnum.CLASS.equals(getExcelHeadProperty().getHeadKind())) { |
||||
return; |
||||
} |
||||
Map<Integer, Head> headMap = getExcelHeadProperty().getHeadMap(); |
||||
Map<Integer, ExcelContentProperty> contentPropertyMap = getExcelHeadProperty().getContentPropertyMap(); |
||||
|
||||
boolean hasCellStyle = false; |
||||
boolean hasColumnWidth = false; |
||||
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) { |
||||
if (entry.getValue().getCellStyleProperty() != null) { |
||||
hasCellStyle = true; |
||||
} |
||||
if (entry.getValue().getColumnWidthProperty() != null) { |
||||
hasColumnWidth = true; |
||||
} |
||||
ExcelContentProperty excelContentProperty = contentPropertyMap.get(entry.getKey()); |
||||
if (excelContentProperty.getCellStyleProperty() != null) { |
||||
hasCellStyle = true; |
||||
} |
||||
} |
||||
|
||||
if (hasCellStyle) { |
||||
dealCellStyle(handlerList, contentPropertyMap); |
||||
} |
||||
if (hasColumnWidth) { |
||||
dealColumnWidth(handlerList); |
||||
} |
||||
dealRowHigh(handlerList, contentPropertyMap); |
||||
} |
||||
|
||||
private void dealRowHigh(List<WriteHandler> handlerList, Map<Integer, ExcelContentProperty> contentPropertyMap) { |
||||
RowHeightProperty headRowHeightProperty = excelHeadProperty.getHeadRowHeightProperty(); |
||||
RowHeightProperty contentRowHeightProperty = excelHeadProperty.getContentRowHeightProperty(); |
||||
if (headRowHeightProperty == null && contentRowHeightProperty == null) { |
||||
return; |
||||
} |
||||
Short headRowHeight = null; |
||||
if (headRowHeightProperty != null) { |
||||
headRowHeight = headRowHeightProperty.getHeight(); |
||||
} |
||||
Short contentRowHeight = null; |
||||
if (contentRowHeightProperty != null) { |
||||
contentRowHeight = contentRowHeightProperty.getHeight(); |
||||
} |
||||
handlerList.add(new SimpleRowHeightStyleStrategy(headRowHeight, contentRowHeight)); |
||||
} |
||||
|
||||
private void dealColumnWidth(List<WriteHandler> handlerList) { |
||||
WriteHandler columnWidthStyleStrategy = new AbstractColumnWidthStyleStrategy() { |
||||
@Override |
||||
protected void setColumnWidth(Sheet sheet, Cell cell, Head head) { |
||||
if (head == null) { |
||||
return; |
||||
} |
||||
if (head.getColumnWidthProperty() != null) { |
||||
sheet.setColumnWidth(head.getColumnIndex(), head.getColumnWidthProperty().getWidth()); |
||||
} |
||||
} |
||||
}; |
||||
handlerList.add(columnWidthStyleStrategy); |
||||
} |
||||
|
||||
private void dealCellStyle(List<WriteHandler> handlerList, |
||||
final Map<Integer, ExcelContentProperty> contentPropertyMap) { |
||||
WriteHandler columnCellStyleStrategy = new AbstractColumnCellStyleStrategy() { |
||||
@Override |
||||
protected CellStyle headCellStyle(Head head) { |
||||
if (head == null || head.getCellStyleProperty() == null) { |
||||
return null; |
||||
} |
||||
CellStyleProperty cellStyleProperty = head.getCellStyleProperty(); |
||||
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(), |
||||
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors()); |
||||
} |
||||
|
||||
@Override |
||||
protected CellStyle contentCellStyle(Head head) { |
||||
if (head == null) { |
||||
return null; |
||||
} |
||||
ExcelContentProperty excelContentProperty = contentPropertyMap.get(head.getColumnIndex()); |
||||
if (excelContentProperty == null || excelContentProperty.getCellStyleProperty() == null) { |
||||
return null; |
||||
} |
||||
CellStyleProperty cellStyleProperty = excelContentProperty.getCellStyleProperty(); |
||||
return new CellStyle(cellStyleProperty.getFontName(), cellStyleProperty.getFontHeightInPoints(), |
||||
cellStyleProperty.getBold(), cellStyleProperty.getIndexedColors()); |
||||
} |
||||
}; |
||||
handlerList.add(columnCellStyleStrategy); |
||||
} |
||||
|
||||
@Override |
||||
public Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap() { |
||||
return getWriteHandlerMap(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean needHead() { |
||||
return getNeedHead(); |
||||
} |
||||
|
||||
@Override |
||||
public int writeRelativeHeadRowIndex() { |
||||
return getWriteRelativeHeadRowIndex(); |
||||
} |
||||
|
||||
@Override |
||||
public ExcelHeadProperty excelHeadProperty() { |
||||
return getExcelHeadProperty(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isNew() { |
||||
return getNewInitialization(); |
||||
} |
||||
|
||||
@Override |
||||
public List<ReadListener> readListenerList() { |
||||
return getReadListenerList(); |
||||
} |
||||
|
||||
@Override |
||||
public Map<ConverterKey, Converter> readConverterMap() { |
||||
return getReadConverterMap(); |
||||
} |
||||
|
||||
@Override |
||||
public Map<Class, Converter> writeConverterMap() { |
||||
return getWriteConverterMap(); |
||||
} |
||||
} |
@ -1,39 +0,0 @@
|
||||
package com.alibaba.excel.metadata.holder.read; |
||||
|
||||
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.property.ExcelHeadProperty; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
|
||||
/** |
||||
* |
||||
* Get the corresponding configuration |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public interface ReadConfiguration { |
||||
/** |
||||
* 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> readConverterMap(); |
||||
|
||||
/** |
||||
* What 'ExcelHeadProperty' does the currently operated cell need to execute |
||||
* |
||||
* @return |
||||
*/ |
||||
ExcelHeadProperty excelHeadProperty(); |
||||
|
||||
} |
@ -1,235 +0,0 @@
|
||||
package com.alibaba.excel.metadata.holder.read; |
||||
|
||||
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.holder.write.AbstractWriteConfiguration; |
||||
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 SheetHolder extends AbstractWriteConfiguration { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SheetHolder.class); |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private Sheet sheet; |
||||
/*** |
||||
* sheetNo |
||||
*/ |
||||
private Integer sheetNo; |
||||
/*** |
||||
* sheetName |
||||
*/ |
||||
private String sheetName; |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private WorkbookHolder parentWorkBook; |
||||
/*** |
||||
* has been initialized table |
||||
*/ |
||||
private Map<Integer, TableHolder> hasBeenInitializedTable; |
||||
/** |
||||
* current param |
||||
*/ |
||||
private com.alibaba.excel.metadata.Sheet sheetParam; |
||||
|
||||
public static SheetHolder buildWriteWorkSheetHolder(com.alibaba.excel.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.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.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.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.metadata.Sheet getSheetParam() { |
||||
return sheetParam; |
||||
} |
||||
|
||||
public void setSheetParam(com.alibaba.excel.metadata.Sheet sheetParam) { |
||||
this.sheetParam = sheetParam; |
||||
} |
||||
} |
@ -1,138 +0,0 @@
|
||||
package com.alibaba.excel.metadata.holder.read; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.metadata.CellStyle; |
||||
import com.alibaba.excel.metadata.Table; |
||||
import com.alibaba.excel.metadata.TableStyle; |
||||
import com.alibaba.excel.metadata.holder.write.AbstractWriteConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelHeadProperty; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
import com.alibaba.excel.write.style.RowCellStyleStrategy; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class TableHolder extends AbstractWriteConfiguration { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TableHolder.class); |
||||
|
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private SheetHolder parentSheet; |
||||
/*** |
||||
* tableNo |
||||
*/ |
||||
private Integer tableNo; |
||||
/** |
||||
* current table param |
||||
*/ |
||||
private Table tableParam; |
||||
|
||||
public static TableHolder buildWriteWorkTableHolder(Table table, SheetHolder sheetHolder, |
||||
WorkbookHolder workbookHolder) { |
||||
TableHolder tableHolder = new TableHolder(); |
||||
tableHolder.setTableParam(table); |
||||
tableHolder.setParentSheet(sheetHolder); |
||||
tableHolder.setTableNo(table.getTableNo()); |
||||
boolean noHead = (table.getHead() == null || table.getHead().isEmpty()) && table.getClazz() == null; |
||||
if (noHead) { |
||||
// Use parent
|
||||
tableHolder.setHead(sheetHolder.getHead()); |
||||
tableHolder.setClazz(sheetHolder.getClazz()); |
||||
} else { |
||||
tableHolder.setHead(table.getHead()); |
||||
tableHolder.setClazz(table.getClazz()); |
||||
} |
||||
tableHolder.setNewInitialization(Boolean.TRUE); |
||||
// Initialization property
|
||||
tableHolder.setExcelHeadProperty( |
||||
new ExcelHeadProperty(tableHolder.getClazz(), tableHolder.getHead(), workbookHolder.getConvertAllFiled())); |
||||
|
||||
if (table.getNeedHead() == null) { |
||||
tableHolder.setNeedHead(sheetHolder.needHead()); |
||||
} else { |
||||
tableHolder.setNeedHead(table.getNeedHead()); |
||||
} |
||||
if (table.getWriteRelativeHeadRowIndex() == null) { |
||||
tableHolder.setWriteRelativeHeadRowIndex(sheetHolder.writeRelativeHeadRowIndex()); |
||||
} else { |
||||
tableHolder.setWriteRelativeHeadRowIndex(table.getWriteRelativeHeadRowIndex()); |
||||
} |
||||
// Compatible with old code
|
||||
compatibleOldCode(table); |
||||
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
||||
if (table.getCustomWriteHandlerList() != null && !table.getCustomWriteHandlerList().isEmpty()) { |
||||
handlerList.addAll(table.getCustomWriteHandlerList()); |
||||
} |
||||
// Initialization Annotation
|
||||
tableHolder.initAnnotationConfig(handlerList); |
||||
|
||||
tableHolder |
||||
.setWriteHandlerMap(tableHolder.sortAndClearUpHandler(handlerList, sheetHolder.getWriteHandlerMap())); |
||||
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(sheetHolder.getWriteConverterMap()); |
||||
if (table.getCustomConverterList() != null && !table.getCustomConverterList().isEmpty()) { |
||||
for (Converter converter : table.getCustomConverterList()) { |
||||
converterMap.put(converter.getClass(), converter); |
||||
} |
||||
} |
||||
tableHolder.setWriteConverterMap(converterMap); |
||||
if (LOGGER.isDebugEnabled()) { |
||||
LOGGER.debug("Table writeHandlerMap:{}", tableHolder.getWriteHandlerMap()); |
||||
} |
||||
return tableHolder; |
||||
} |
||||
|
||||
/** |
||||
* Compatible with old code |
||||
*/ |
||||
@Deprecated |
||||
private static void compatibleOldCode(Table table) { |
||||
if (table.getTableStyle() != null) { |
||||
final TableStyle tableStyle = table.getTableStyle(); |
||||
if (table.getCustomWriteHandlerList() == null) { |
||||
table.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()); |
||||
table.getCustomWriteHandlerList().add(new RowCellStyleStrategy(headCellStyle, contentCellStyle)); |
||||
} |
||||
} |
||||
|
||||
public SheetHolder getParentSheet() { |
||||
return parentSheet; |
||||
} |
||||
|
||||
public void setParentSheet(SheetHolder parentSheet) { |
||||
this.parentSheet = parentSheet; |
||||
} |
||||
|
||||
public Integer getTableNo() { |
||||
return tableNo; |
||||
} |
||||
|
||||
public void setTableNo(Integer tableNo) { |
||||
this.tableNo = tableNo; |
||||
} |
||||
|
||||
public Table getTableParam() { |
||||
return tableParam; |
||||
} |
||||
|
||||
public void setTableParam(Table tableParam) { |
||||
this.tableParam = tableParam; |
||||
} |
||||
} |
@ -1,325 +0,0 @@
|
||||
package com.alibaba.excel.metadata.holder.read; |
||||
|
||||
import java.io.File; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.cache.ReadCache; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.converters.ConverterKey; |
||||
import com.alibaba.excel.converters.DefaultConverterLoader; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.metadata.holder.write.AbstractWriteConfiguration; |
||||
import com.alibaba.excel.read.listener.ModelBuildEventListener; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
import com.alibaba.excel.write.handler.DefaultWriteHandlerLoader; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
|
||||
/** |
||||
* Workbook holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class WorkbookHolder extends AbstractWriteConfiguration { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WorkbookHolder.class); |
||||
|
||||
/*** |
||||
* poi Workbook |
||||
*/ |
||||
private Workbook workbook; |
||||
/** |
||||
* prevent duplicate creation of sheet objects |
||||
*/ |
||||
private Map<Integer, SheetHolder> hasBeenInitializedSheet; |
||||
/** |
||||
* current param |
||||
*/ |
||||
private com.alibaba.excel.metadata.Workbook workbookParam; |
||||
/** |
||||
* Final output stream |
||||
*/ |
||||
private OutputStream outputStream; |
||||
/** |
||||
* <li>write: Template input stream |
||||
* <li>read: Read InputStream |
||||
* <p> |
||||
* If 'inputStream' and 'file' all not empty,file first |
||||
*/ |
||||
private InputStream inputStream; |
||||
/** |
||||
* <li>write: Template file |
||||
* <li>read: Read file |
||||
* <p> |
||||
* If 'inputStream' and 'file' all not empty,file first |
||||
*/ |
||||
private File file; |
||||
/** |
||||
* Default true |
||||
*/ |
||||
private Boolean autoCloseStream; |
||||
/** |
||||
* Excel type |
||||
*/ |
||||
private ExcelTypeEnum excelType; |
||||
/** |
||||
* This object can be read in the Listener {@link AnalysisEventListener#invoke(Object, AnalysisContext)} |
||||
* {@link AnalysisContext#getCustom()} |
||||
* |
||||
*/ |
||||
private Object readCustomObject; |
||||
/** |
||||
* A cache that stores temp data to save memory.Default use {@link com.alibaba.excel.cache.Ehcache} |
||||
*/ |
||||
private ReadCache readCache; |
||||
/** |
||||
* true if date uses 1904 windowing, or false if using 1900 date windowing. |
||||
* |
||||
* @return |
||||
*/ |
||||
private Boolean use1904windowing; |
||||
|
||||
/** |
||||
* Mmandatory use 'inputStream' |
||||
*/ |
||||
private Boolean mandatoryUseInputStream; |
||||
|
||||
/** |
||||
* Temporary files when reading excel |
||||
*/ |
||||
private File readTempFile; |
||||
|
||||
/** |
||||
* 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 |
||||
private Boolean convertAllFiled; |
||||
|
||||
/** |
||||
* Write handler |
||||
* |
||||
* @deprecated please use {@link WriteHandler} |
||||
*/ |
||||
@Deprecated |
||||
private com.alibaba.excel.event.WriteHandler writeHandler; |
||||
|
||||
public static WorkbookHolder buildWriteWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { |
||||
WorkbookHolder workbookHolder = buildBaseWorkbookHolder(workbook); |
||||
workbookHolder.setNewInitialization(Boolean.TRUE); |
||||
if (workbook.getNeedHead() == null) { |
||||
workbookHolder.setNeedHead(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setNeedHead(workbook.getNeedHead()); |
||||
} |
||||
if (workbook.getWriteRelativeHeadRowIndex() == null) { |
||||
workbookHolder.setWriteRelativeHeadRowIndex(0); |
||||
} else { |
||||
workbookHolder.setWriteRelativeHeadRowIndex(workbook.getWriteRelativeHeadRowIndex()); |
||||
} |
||||
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
||||
if (workbook.getCustomWriteHandlerList() != null && !workbook.getCustomWriteHandlerList().isEmpty()) { |
||||
handlerList.addAll(workbook.getCustomWriteHandlerList()); |
||||
} |
||||
handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler()); |
||||
workbookHolder.setWriteHandlerMap(workbookHolder.sortAndClearUpHandler(handlerList, null)); |
||||
|
||||
Map<Class, Converter> converterMap = DefaultConverterLoader.loadDefaultWriteConverter(); |
||||
if (workbook.getCustomConverterList() != null && !workbook.getCustomConverterList().isEmpty()) { |
||||
for (Converter converter : workbook.getCustomConverterList()) { |
||||
converterMap.put(converter.getClass(), converter); |
||||
} |
||||
} |
||||
workbookHolder.setWriteConverterMap(converterMap); |
||||
workbookHolder.setHasBeenInitializedSheet(new HashMap<Integer, SheetHolder>()); |
||||
if (LOGGER.isDebugEnabled()) { |
||||
LOGGER.debug("Wookbook writeHandlerMap:{}", workbookHolder.getWriteHandlerMap()); |
||||
} |
||||
return workbookHolder; |
||||
} |
||||
|
||||
public static WorkbookHolder buildReadWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { |
||||
WorkbookHolder workbookHolder = buildBaseWorkbookHolder(workbook); |
||||
if (workbook.getFile() == null && workbookHolder.getInputStream() == null) { |
||||
throw new ExcelAnalysisException("Read excel 'file' and 'inputStream' cannot be empty at the same time!"); |
||||
} |
||||
workbookHolder.setReadCustomObject(workbook.getReadCustomObject()); |
||||
workbookHolder.setReadHeadRowNumber(workbook.getReadHeadRowNumber()); |
||||
workbookHolder.setReadCache(workbook.getReadCache()); |
||||
|
||||
Map<ConverterKey, Converter> converterMap = DefaultConverterLoader.loadDefaultReadConverter(); |
||||
if (workbook.getCustomConverterList() != null && !workbook.getCustomConverterList().isEmpty()) { |
||||
for (Converter converter : workbook.getCustomConverterList()) { |
||||
converterMap.put(ConverterKey.buildConverterKey(converter), converter); |
||||
} |
||||
} |
||||
workbookHolder.setReadConverterMap(converterMap); |
||||
|
||||
List<ReadListener> readListenerList = new ArrayList<ReadListener>(); |
||||
readListenerList.add(new ModelBuildEventListener()); |
||||
if (workbook.getCustomReadListenerList() != null && !workbook.getCustomReadListenerList().isEmpty()) { |
||||
readListenerList.addAll(workbook.getCustomReadListenerList()); |
||||
} |
||||
workbookHolder.setReadListenerList(readListenerList); |
||||
return workbookHolder; |
||||
} |
||||
|
||||
private static WorkbookHolder buildBaseWorkbookHolder(com.alibaba.excel.metadata.Workbook workbook) { |
||||
WorkbookHolder workbookHolder = new WorkbookHolder(); |
||||
workbookHolder.setUse1904windowing(workbook.getUse1904windowing()); |
||||
workbookHolder.setWorkbookParam(workbook); |
||||
workbookHolder.setInputStream(workbook.getInputStream()); |
||||
workbookHolder.setFile(workbook.getFile()); |
||||
workbookHolder.setExcelType(workbook.getExcelType()); |
||||
workbookHolder.setHead(workbook.getHead()); |
||||
workbookHolder.setClazz(workbook.getClazz()); |
||||
if (workbook.getConvertAllFiled() == null) { |
||||
workbookHolder.setConvertAllFiled(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setConvertAllFiled(workbook.getConvertAllFiled()); |
||||
} |
||||
if (workbook.getAutoCloseStream() == null) { |
||||
workbookHolder.setAutoCloseStream(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setAutoCloseStream(workbook.getAutoCloseStream()); |
||||
} |
||||
if (workbook.getAutoTrim() == null) { |
||||
workbookHolder.setAutoTrim(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setAutoTrim(workbook.getNeedHead()); |
||||
} |
||||
return workbookHolder; |
||||
} |
||||
|
||||
public Workbook getWorkbook() { |
||||
return workbook; |
||||
} |
||||
|
||||
public void setWorkbook(Workbook workbook) { |
||||
this.workbook = workbook; |
||||
} |
||||
|
||||
public Map<Integer, SheetHolder> getHasBeenInitializedSheet() { |
||||
return hasBeenInitializedSheet; |
||||
} |
||||
|
||||
public void setHasBeenInitializedSheet(Map<Integer, SheetHolder> hasBeenInitializedSheet) { |
||||
this.hasBeenInitializedSheet = hasBeenInitializedSheet; |
||||
} |
||||
|
||||
public com.alibaba.excel.metadata.Workbook getWorkbookParam() { |
||||
return workbookParam; |
||||
} |
||||
|
||||
public void setWorkbookParam(com.alibaba.excel.metadata.Workbook workbookParam) { |
||||
this.workbookParam = workbookParam; |
||||
} |
||||
|
||||
public OutputStream getOutputStream() { |
||||
return outputStream; |
||||
} |
||||
|
||||
public void setOutputStream(OutputStream outputStream) { |
||||
this.outputStream = outputStream; |
||||
} |
||||
|
||||
public InputStream getInputStream() { |
||||
return inputStream; |
||||
} |
||||
|
||||
public void setInputStream(InputStream inputStream) { |
||||
this.inputStream = inputStream; |
||||
} |
||||
|
||||
public com.alibaba.excel.event.WriteHandler getWriteHandler() { |
||||
return writeHandler; |
||||
} |
||||
|
||||
public void setWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) { |
||||
this.writeHandler = writeHandler; |
||||
} |
||||
|
||||
public Boolean getAutoCloseStream() { |
||||
return autoCloseStream; |
||||
} |
||||
|
||||
public void setAutoCloseStream(Boolean autoCloseStream) { |
||||
this.autoCloseStream = autoCloseStream; |
||||
} |
||||
|
||||
public Boolean getConvertAllFiled() { |
||||
return convertAllFiled; |
||||
} |
||||
|
||||
public void setConvertAllFiled(Boolean convertAllFiled) { |
||||
this.convertAllFiled = convertAllFiled; |
||||
} |
||||
|
||||
public File getFile() { |
||||
return file; |
||||
} |
||||
|
||||
public void setFile(File file) { |
||||
this.file = file; |
||||
} |
||||
|
||||
public ExcelTypeEnum getExcelType() { |
||||
return excelType; |
||||
} |
||||
|
||||
public void setExcelType(ExcelTypeEnum excelType) { |
||||
this.excelType = excelType; |
||||
} |
||||
|
||||
public Object getReadCustomObject() { |
||||
return readCustomObject; |
||||
} |
||||
|
||||
public void setReadCustomObject(Object readCustomObject) { |
||||
this.readCustomObject = readCustomObject; |
||||
} |
||||
|
||||
public ReadCache getReadCache() { |
||||
return readCache; |
||||
} |
||||
|
||||
public void setReadCache(ReadCache readCache) { |
||||
this.readCache = readCache; |
||||
} |
||||
|
||||
public Boolean getUse1904windowing() { |
||||
return use1904windowing; |
||||
} |
||||
|
||||
public void setUse1904windowing(Boolean use1904windowing) { |
||||
this.use1904windowing = use1904windowing; |
||||
} |
||||
|
||||
public Boolean getMandatoryUseInputStream() { |
||||
return mandatoryUseInputStream; |
||||
} |
||||
|
||||
public void setMandatoryUseInputStream(Boolean mandatoryUseInputStream) { |
||||
this.mandatoryUseInputStream = mandatoryUseInputStream; |
||||
} |
||||
|
||||
public File getReadTempFile() { |
||||
return readTempFile; |
||||
} |
||||
|
||||
public void setReadTempFile(File readTempFile) { |
||||
this.readTempFile = readTempFile; |
||||
} |
||||
} |
@ -1,234 +0,0 @@
|
||||
package com.alibaba.excel.metadata.holder.write; |
||||
|
||||
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 SheetHolder extends AbstractWriteConfiguration { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SheetHolder.class); |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private Sheet sheet; |
||||
/*** |
||||
* sheetNo |
||||
*/ |
||||
private Integer sheetNo; |
||||
/*** |
||||
* sheetName |
||||
*/ |
||||
private String sheetName; |
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private WorkbookHolder parentWorkBook; |
||||
/*** |
||||
* has been initialized table |
||||
*/ |
||||
private Map<Integer, TableHolder> hasBeenInitializedTable; |
||||
/** |
||||
* current param |
||||
*/ |
||||
private com.alibaba.excel.metadata.Sheet sheetParam; |
||||
|
||||
public static SheetHolder buildWriteWorkSheetHolder(com.alibaba.excel.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.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.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.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.metadata.Sheet getSheetParam() { |
||||
return sheetParam; |
||||
} |
||||
|
||||
public void setSheetParam(com.alibaba.excel.metadata.Sheet sheetParam) { |
||||
this.sheetParam = sheetParam; |
||||
} |
||||
} |
@ -0,0 +1,189 @@
|
||||
package com.alibaba.excel.read.builder; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import com.alibaba.excel.ExcelReader; |
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.event.SyncReadListener; |
||||
import com.alibaba.excel.exception.ExcelGenerateException; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
|
||||
/** |
||||
* Build sheet |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ExcelReaderSheetBuilder { |
||||
private ExcelReader excelReader; |
||||
/** |
||||
* Sheet |
||||
*/ |
||||
private ReadSheet readSheet; |
||||
|
||||
public ExcelReaderSheetBuilder() { |
||||
this.readSheet = new ReadSheet(); |
||||
} |
||||
|
||||
public ExcelReaderSheetBuilder(ExcelReader excelReader) { |
||||
this.readSheet = new ReadSheet(); |
||||
this.excelReader = excelReader; |
||||
} |
||||
|
||||
/** |
||||
* Starting from 0 |
||||
* |
||||
* @param sheetNo |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder sheetNo(Integer sheetNo) { |
||||
readSheet.setSheetNo(sheetNo); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* sheet name |
||||
* |
||||
* @param sheetName |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder sheetName(String sheetName) { |
||||
readSheet.setSheetName(sheetName); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* 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 |
||||
* |
||||
* @param headRowNumber |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder headRowNumber(Integer headRowNumber) { |
||||
readSheet.setHeadRowNumber(headRowNumber); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* You can only choose one of the {@link ExcelReaderBuilder#head(List)} and {@link ExcelReaderBuilder#head(Class)} |
||||
* |
||||
* @param head |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder head(List<List<String>> head) { |
||||
readSheet.setHead(head); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* You can only choose one of the {@link ExcelReaderBuilder#head(List)} and {@link ExcelReaderBuilder#head(Class)} |
||||
* |
||||
* @param clazz |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder head(Class clazz) { |
||||
readSheet.setClazz(clazz); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Custom type conversions override the default. |
||||
* |
||||
* @param converter |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder registerConverter(Converter converter) { |
||||
if (readSheet.getCustomConverterList() == null) { |
||||
readSheet.setCustomConverterList(new ArrayList<Converter>()); |
||||
} |
||||
readSheet.getCustomConverterList().add(converter); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Custom type listener run after default |
||||
* |
||||
* @param readListener |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder registerReadListener(ReadListener readListener) { |
||||
if (readSheet.getCustomReadListenerList() == null) { |
||||
readSheet.setCustomReadListenerList(new ArrayList<ReadListener>()); |
||||
} |
||||
readSheet.getCustomReadListenerList().add(readListener); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* true if date uses 1904 windowing, or false if using 1900 date windowing. |
||||
* |
||||
* default is false |
||||
* |
||||
* @param use1904windowing |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder use1904windowing(Boolean use1904windowing) { |
||||
readSheet.setUse1904windowing(use1904windowing); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Automatic trim includes sheet name and content |
||||
* |
||||
* @param autoTrim |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder autoTrim(Boolean autoTrim) { |
||||
readSheet.setAutoTrim(autoTrim); |
||||
return this; |
||||
} |
||||
|
||||
public ReadSheet build() { |
||||
return readSheet; |
||||
} |
||||
|
||||
/** |
||||
* Sax read |
||||
* |
||||
* @return |
||||
*/ |
||||
public ExcelReaderSheetBuilder doRead() { |
||||
if (excelReader == null) { |
||||
throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); |
||||
} |
||||
excelReader.read(build()); |
||||
return new ExcelReaderSheetBuilder(excelReader); |
||||
} |
||||
|
||||
/** |
||||
* Sax read |
||||
* |
||||
* @return |
||||
*/ |
||||
public void finish() { |
||||
if (excelReader == null) { |
||||
throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); |
||||
} |
||||
excelReader.finish(); |
||||
} |
||||
|
||||
/** |
||||
* Synchronous reads return results |
||||
* |
||||
* @return |
||||
*/ |
||||
public List<Object> doReadSync() { |
||||
if (excelReader == null) { |
||||
throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); |
||||
} |
||||
SyncReadListener syncReadListener = new SyncReadListener(); |
||||
registerReadListener(syncReadListener); |
||||
excelReader.read(build()); |
||||
return syncReadListener.getList(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,85 @@
|
||||
package com.alibaba.excel.read.metadata.holder; |
||||
|
||||
import com.alibaba.excel.enums.HolderEnum; |
||||
import com.alibaba.excel.read.metadata.ReadSheet; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ReadSheetHolder extends AbstractReadHolder { |
||||
/** |
||||
* current param |
||||
*/ |
||||
private ReadSheet readSheet; |
||||
/*** |
||||
* parent |
||||
*/ |
||||
private ReadWorkbookHolder parentReadWorkbookHolder; |
||||
/*** |
||||
* sheetNo |
||||
*/ |
||||
private Integer sheetNo; |
||||
/*** |
||||
* sheetName |
||||
*/ |
||||
private String sheetName; |
||||
/** |
||||
* get total row , Data may be inaccurate |
||||
*/ |
||||
@Deprecated |
||||
private Integer total; |
||||
|
||||
public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { |
||||
super(readSheet, null, readWorkbookHolder.getReadWorkbook().getConvertAllFiled()); |
||||
this.readSheet = readSheet; |
||||
this.parentReadWorkbookHolder = readWorkbookHolder; |
||||
this.sheetNo = readSheet.getSheetNo(); |
||||
} |
||||
|
||||
public ReadSheet getReadSheet() { |
||||
return readSheet; |
||||
} |
||||
|
||||
public void setReadSheet(ReadSheet readSheet) { |
||||
this.readSheet = readSheet; |
||||
} |
||||
|
||||
public ReadWorkbookHolder getParentReadWorkbookHolder() { |
||||
return parentReadWorkbookHolder; |
||||
} |
||||
|
||||
public void setParentReadWorkbookHolder(ReadWorkbookHolder parentReadWorkbookHolder) { |
||||
this.parentReadWorkbookHolder = parentReadWorkbookHolder; |
||||
} |
||||
|
||||
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 Integer getTotal() { |
||||
return total; |
||||
} |
||||
|
||||
public void setTotal(Integer total) { |
||||
this.total = total; |
||||
} |
||||
|
||||
@Override |
||||
public HolderEnum holderType() { |
||||
return HolderEnum.SHEET; |
||||
} |
||||
} |
@ -0,0 +1,185 @@
|
||||
package com.alibaba.excel.read.metadata.holder; |
||||
|
||||
import java.io.File; |
||||
import java.io.InputStream; |
||||
|
||||
import com.alibaba.excel.cache.ReadCache; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.enums.HolderEnum; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.read.metadata.ReadWorkbook; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
|
||||
/** |
||||
* Workbook holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ReadWorkbookHolder extends AbstractReadHolder { |
||||
/** |
||||
* current param |
||||
*/ |
||||
private ReadWorkbook readWorkbook; |
||||
/** |
||||
* 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; |
||||
/** |
||||
* Mandatory use 'inputStream' .Default is false. |
||||
* <li>if false,Will transfer 'inputStream' to temporary files to improve efficiency |
||||
*/ |
||||
private Boolean mandatoryUseInputStream; |
||||
/** |
||||
* Default true |
||||
*/ |
||||
private Boolean autoCloseStream; |
||||
/** |
||||
* Excel type |
||||
*/ |
||||
private ExcelTypeEnum excelType; |
||||
/** |
||||
* This object can be read in the Listener {@link AnalysisEventListener#invoke(Object, AnalysisContext)} |
||||
* {@link AnalysisContext#getCustom()} |
||||
* |
||||
*/ |
||||
private Object customObject; |
||||
/** |
||||
* A cache that stores temp data to save memory.Default use {@link com.alibaba.excel.cache.Ehcache} |
||||
*/ |
||||
private ReadCache readCache; |
||||
|
||||
/** |
||||
* Temporary files when reading excel |
||||
*/ |
||||
private File tempFile; |
||||
|
||||
/** |
||||
* 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 |
||||
private Boolean convertAllFiled; |
||||
|
||||
public ReadWorkbookHolder(ReadWorkbook readWorkbook) { |
||||
super(readWorkbook, null, readWorkbook.getConvertAllFiled()); |
||||
this.readWorkbook = readWorkbook; |
||||
if (file == null && inputStream == null) { |
||||
throw new ExcelAnalysisException("File and inputStream must be a non-null."); |
||||
} |
||||
this.inputStream = readWorkbook.getInputStream(); |
||||
this.file = readWorkbook.getFile(); |
||||
if (readWorkbook.getMandatoryUseInputStream() == null) { |
||||
this.mandatoryUseInputStream = Boolean.FALSE; |
||||
} else { |
||||
this.mandatoryUseInputStream = readWorkbook.getMandatoryUseInputStream(); |
||||
} |
||||
if (readWorkbook.getAutoCloseStream() == null) { |
||||
this.autoCloseStream = Boolean.TRUE; |
||||
} else { |
||||
this.autoCloseStream = readWorkbook.getAutoCloseStream(); |
||||
} |
||||
if (readWorkbook.getExcelType() == null) { |
||||
this.excelType = ExcelTypeEnum.valueOf(file, inputStream); |
||||
} else { |
||||
this.excelType = readWorkbook.getExcelType(); |
||||
} |
||||
this.customObject = readWorkbook.getCustomObject(); |
||||
this.readCache = readWorkbook.getReadCache(); |
||||
} |
||||
|
||||
public ReadWorkbook getReadWorkbook() { |
||||
return readWorkbook; |
||||
} |
||||
|
||||
public void setReadWorkbook(ReadWorkbook readWorkbook) { |
||||
this.readWorkbook = readWorkbook; |
||||
} |
||||
|
||||
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 ExcelTypeEnum getExcelType() { |
||||
return excelType; |
||||
} |
||||
|
||||
public void setExcelType(ExcelTypeEnum excelType) { |
||||
this.excelType = excelType; |
||||
} |
||||
|
||||
public Object getCustomObject() { |
||||
return customObject; |
||||
} |
||||
|
||||
public void setCustomObject(Object customObject) { |
||||
this.customObject = customObject; |
||||
} |
||||
|
||||
public ReadCache getReadCache() { |
||||
return readCache; |
||||
} |
||||
|
||||
public void setReadCache(ReadCache readCache) { |
||||
this.readCache = readCache; |
||||
} |
||||
|
||||
public Boolean getMandatoryUseInputStream() { |
||||
return mandatoryUseInputStream; |
||||
} |
||||
|
||||
public void setMandatoryUseInputStream(Boolean mandatoryUseInputStream) { |
||||
this.mandatoryUseInputStream = mandatoryUseInputStream; |
||||
} |
||||
|
||||
public File getTempFile() { |
||||
return tempFile; |
||||
} |
||||
|
||||
public void setTempFile(File tempFile) { |
||||
this.tempFile = tempFile; |
||||
} |
||||
|
||||
public Boolean getConvertAllFiled() { |
||||
return convertAllFiled; |
||||
} |
||||
|
||||
public void setConvertAllFiled(Boolean convertAllFiled) { |
||||
this.convertAllFiled = convertAllFiled; |
||||
} |
||||
|
||||
@Override |
||||
public HolderEnum holderType() { |
||||
return HolderEnum.WORKBOOK; |
||||
} |
||||
} |
@ -1,239 +0,0 @@
|
||||
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; |
||||
} |
||||
} |
@ -1,138 +0,0 @@
|
||||
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.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.metadata.CellStyle; |
||||
import com.alibaba.excel.write.metadata.Table; |
||||
import com.alibaba.excel.metadata.TableStyle; |
||||
import com.alibaba.excel.write.metadata.holder.AbstractWriteConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelHeadProperty; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
import com.alibaba.excel.write.style.RowCellStyleStrategy; |
||||
|
||||
/** |
||||
* sheet holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ReadTableHolder extends AbstractReadHolder { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TableHolder.class); |
||||
|
||||
/*** |
||||
* poi sheet |
||||
*/ |
||||
private SheetHolder parentSheet; |
||||
/*** |
||||
* tableNo |
||||
*/ |
||||
private Integer tableNo; |
||||
/** |
||||
* current table param |
||||
*/ |
||||
private Table tableParam; |
||||
|
||||
public static TableHolder buildWriteWorkTableHolder(Table table, SheetHolder sheetHolder, |
||||
WorkbookHolder workbookHolder) { |
||||
TableHolder tableHolder = new TableHolder(); |
||||
tableHolder.setTableParam(table); |
||||
tableHolder.setParentSheet(sheetHolder); |
||||
tableHolder.setTableNo(table.getTableNo()); |
||||
boolean noHead = (table.getHead() == null || table.getHead().isEmpty()) && table.getClazz() == null; |
||||
if (noHead) { |
||||
// Use parent
|
||||
tableHolder.setHead(sheetHolder.getHead()); |
||||
tableHolder.setClazz(sheetHolder.getClazz()); |
||||
} else { |
||||
tableHolder.setHead(table.getHead()); |
||||
tableHolder.setClazz(table.getClazz()); |
||||
} |
||||
tableHolder.setNewInitialization(Boolean.TRUE); |
||||
// Initialization property
|
||||
tableHolder.setExcelHeadProperty( |
||||
new ExcelHeadProperty(tableHolder.getClazz(), tableHolder.getHead(), workbookHolder.getConvertAllFiled())); |
||||
|
||||
if (table.getNeedHead() == null) { |
||||
tableHolder.setNeedHead(sheetHolder.needHead()); |
||||
} else { |
||||
tableHolder.setNeedHead(table.getNeedHead()); |
||||
} |
||||
if (table.getWriteRelativeHeadRowIndex() == null) { |
||||
tableHolder.setWriteRelativeHeadRowIndex(sheetHolder.writeRelativeHeadRowIndex()); |
||||
} else { |
||||
tableHolder.setWriteRelativeHeadRowIndex(table.getWriteRelativeHeadRowIndex()); |
||||
} |
||||
// Compatible with old code
|
||||
compatibleOldCode(table); |
||||
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
||||
if (table.getCustomWriteHandlerList() != null && !table.getCustomWriteHandlerList().isEmpty()) { |
||||
handlerList.addAll(table.getCustomWriteHandlerList()); |
||||
} |
||||
// Initialization Annotation
|
||||
tableHolder.initAnnotationConfig(handlerList); |
||||
|
||||
tableHolder |
||||
.setWriteHandlerMap(tableHolder.sortAndClearUpHandler(handlerList, sheetHolder.getWriteHandlerMap())); |
||||
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(sheetHolder.getWriteConverterMap()); |
||||
if (table.getCustomConverterList() != null && !table.getCustomConverterList().isEmpty()) { |
||||
for (Converter converter : table.getCustomConverterList()) { |
||||
converterMap.put(converter.getClass(), converter); |
||||
} |
||||
} |
||||
tableHolder.setWriteConverterMap(converterMap); |
||||
if (LOGGER.isDebugEnabled()) { |
||||
LOGGER.debug("Table writeHandlerMap:{}", tableHolder.getWriteHandlerMap()); |
||||
} |
||||
return tableHolder; |
||||
} |
||||
|
||||
/** |
||||
* Compatible with old code |
||||
*/ |
||||
@Deprecated |
||||
private static void compatibleOldCode(Table table) { |
||||
if (table.getTableStyle() != null) { |
||||
final TableStyle tableStyle = table.getTableStyle(); |
||||
if (table.getCustomWriteHandlerList() == null) { |
||||
table.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()); |
||||
table.getCustomWriteHandlerList().add(new RowCellStyleStrategy(headCellStyle, contentCellStyle)); |
||||
} |
||||
} |
||||
|
||||
public SheetHolder getParentSheet() { |
||||
return parentSheet; |
||||
} |
||||
|
||||
public void setParentSheet(SheetHolder parentSheet) { |
||||
this.parentSheet = parentSheet; |
||||
} |
||||
|
||||
public Integer getTableNo() { |
||||
return tableNo; |
||||
} |
||||
|
||||
public void setTableNo(Integer tableNo) { |
||||
this.tableNo = tableNo; |
||||
} |
||||
|
||||
public Table getTableParam() { |
||||
return tableParam; |
||||
} |
||||
|
||||
public void setTableParam(Table tableParam) { |
||||
this.tableParam = tableParam; |
||||
} |
||||
} |
@ -1,315 +0,0 @@
|
||||
package com.alibaba.excel.read.metadata.holder; |
||||
|
||||
import java.io.File; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.usermodel.Workbook; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.cache.ReadCache; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.converters.ConverterKey; |
||||
import com.alibaba.excel.converters.DefaultConverterLoader; |
||||
import com.alibaba.excel.enums.HolderEnum; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.read.listener.ModelBuildEventListener; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
import com.alibaba.excel.read.metadata.ReadWorkbook; |
||||
import com.alibaba.excel.support.ExcelTypeEnum; |
||||
import com.alibaba.excel.write.handler.DefaultWriteHandlerLoader; |
||||
import com.alibaba.excel.write.handler.WriteHandler; |
||||
|
||||
/** |
||||
* Workbook holder |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ReadWorkbookHolder extends AbstractReadHolder { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReadWorkbookHolder.class); |
||||
|
||||
/*** |
||||
* poi Workbook |
||||
*/ |
||||
private Workbook workbook; |
||||
/** |
||||
* current param |
||||
*/ |
||||
private ReadWorkbook readWorkbook; |
||||
/** |
||||
* 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; |
||||
/** |
||||
* Excel type |
||||
*/ |
||||
private ExcelTypeEnum excelType; |
||||
/** |
||||
* This object can be read in the Listener {@link AnalysisEventListener#invoke(Object, AnalysisContext)} |
||||
* {@link AnalysisContext#getCustom()} |
||||
* |
||||
*/ |
||||
private Object readCustomObject; |
||||
/** |
||||
* A cache that stores temp data to save memory.Default use {@link com.alibaba.excel.cache.Ehcache} |
||||
*/ |
||||
private ReadCache readCache; |
||||
|
||||
/** |
||||
* Mmandatory use 'inputStream' |
||||
*/ |
||||
private Boolean mandatoryUseInputStream; |
||||
|
||||
/** |
||||
* Temporary files when reading excel |
||||
*/ |
||||
private File readTempFile; |
||||
|
||||
/** |
||||
* 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 |
||||
private Boolean convertAllFiled; |
||||
|
||||
/** |
||||
* Write handler |
||||
* |
||||
* @deprecated please use {@link WriteHandler} |
||||
*/ |
||||
@Deprecated |
||||
private com.alibaba.excel.event.WriteHandler writeHandler; |
||||
|
||||
public static WorkbookHolder buildWriteWorkbookHolder(com.alibaba.excel.write.metadata.Workbook workbook) { |
||||
WorkbookHolder workbookHolder = buildBaseWorkbookHolder(workbook); |
||||
workbookHolder.setNewInitialization(Boolean.TRUE); |
||||
if (workbook.getNeedHead() == null) { |
||||
workbookHolder.setNeedHead(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setNeedHead(workbook.getNeedHead()); |
||||
} |
||||
if (workbook.getWriteRelativeHeadRowIndex() == null) { |
||||
workbookHolder.setWriteRelativeHeadRowIndex(0); |
||||
} else { |
||||
workbookHolder.setWriteRelativeHeadRowIndex(workbook.getWriteRelativeHeadRowIndex()); |
||||
} |
||||
List<WriteHandler> handlerList = new ArrayList<WriteHandler>(); |
||||
if (workbook.getCustomWriteHandlerList() != null && !workbook.getCustomWriteHandlerList().isEmpty()) { |
||||
handlerList.addAll(workbook.getCustomWriteHandlerList()); |
||||
} |
||||
handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler()); |
||||
workbookHolder.setWriteHandlerMap(workbookHolder.sortAndClearUpHandler(handlerList, null)); |
||||
|
||||
Map<Class, Converter> converterMap = DefaultConverterLoader.loadDefaultWriteConverter(); |
||||
if (workbook.getCustomConverterList() != null && !workbook.getCustomConverterList().isEmpty()) { |
||||
for (Converter converter : workbook.getCustomConverterList()) { |
||||
converterMap.put(converter.getClass(), converter); |
||||
} |
||||
} |
||||
workbookHolder.setWriteConverterMap(converterMap); |
||||
workbookHolder.setHasBeenInitializedSheet(new HashMap<Integer, SheetHolder>()); |
||||
if (LOGGER.isDebugEnabled()) { |
||||
LOGGER.debug("Wookbook writeHandlerMap:{}", workbookHolder.getWriteHandlerMap()); |
||||
} |
||||
return workbookHolder; |
||||
} |
||||
|
||||
public static WorkbookHolder buildReadWorkbookHolder(com.alibaba.excel.write.metadata.Workbook workbook) { |
||||
WorkbookHolder workbookHolder = buildBaseWorkbookHolder(workbook); |
||||
if (workbook.getFile() == null && workbookHolder.getInputStream() == null) { |
||||
throw new ExcelAnalysisException("Read excel 'file' and 'inputStream' cannot be empty at the same time!"); |
||||
} |
||||
workbookHolder.setReadCustomObject(workbook.getReadCustomObject()); |
||||
workbookHolder.setReadHeadRowNumber(workbook.getReadHeadRowNumber()); |
||||
workbookHolder.setReadCache(workbook.getReadCache()); |
||||
|
||||
Map<ConverterKey, Converter> converterMap = DefaultConverterLoader.loadDefaultReadConverter(); |
||||
if (workbook.getCustomConverterList() != null && !workbook.getCustomConverterList().isEmpty()) { |
||||
for (Converter converter : workbook.getCustomConverterList()) { |
||||
converterMap.put(ConverterKey.buildConverterKey(converter), converter); |
||||
} |
||||
} |
||||
workbookHolder.setReadConverterMap(converterMap); |
||||
|
||||
List<ReadListener> readListenerList = new ArrayList<ReadListener>(); |
||||
readListenerList.add(new ModelBuildEventListener()); |
||||
if (workbook.getCustomReadListenerList() != null && !workbook.getCustomReadListenerList().isEmpty()) { |
||||
readListenerList.addAll(workbook.getCustomReadListenerList()); |
||||
} |
||||
workbookHolder.setReadListenerList(readListenerList); |
||||
return workbookHolder; |
||||
} |
||||
|
||||
private static WorkbookHolder buildBaseWorkbookHolder(com.alibaba.excel.write.metadata.Workbook workbook) { |
||||
WorkbookHolder workbookHolder = new WorkbookHolder(); |
||||
workbookHolder.setUse1904windowing(workbook.getUse1904windowing()); |
||||
workbookHolder.setWorkbookParam(workbook); |
||||
workbookHolder.setInputStream(workbook.getInputStream()); |
||||
workbookHolder.setFile(workbook.getFile()); |
||||
workbookHolder.setExcelType(workbook.getExcelType()); |
||||
workbookHolder.setHead(workbook.getHead()); |
||||
workbookHolder.setClazz(workbook.getClazz()); |
||||
if (workbook.getConvertAllFiled() == null) { |
||||
workbookHolder.setConvertAllFiled(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setConvertAllFiled(workbook.getConvertAllFiled()); |
||||
} |
||||
if (workbook.getAutoCloseStream() == null) { |
||||
workbookHolder.setAutoCloseStream(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setAutoCloseStream(workbook.getAutoCloseStream()); |
||||
} |
||||
if (workbook.getAutoTrim() == null) { |
||||
workbookHolder.setAutoTrim(Boolean.TRUE); |
||||
} else { |
||||
workbookHolder.setAutoTrim(workbook.getNeedHead()); |
||||
} |
||||
return workbookHolder; |
||||
} |
||||
|
||||
public Workbook getWorkbook() { |
||||
return workbook; |
||||
} |
||||
|
||||
public void setWorkbook(Workbook workbook) { |
||||
this.workbook = workbook; |
||||
} |
||||
|
||||
public Map<Integer, SheetHolder> getHasBeenInitializedSheet() { |
||||
return hasBeenInitializedSheet; |
||||
} |
||||
|
||||
public void setHasBeenInitializedSheet(Map<Integer, SheetHolder> hasBeenInitializedSheet) { |
||||
this.hasBeenInitializedSheet = hasBeenInitializedSheet; |
||||
} |
||||
|
||||
public com.alibaba.excel.write.metadata.Workbook getWorkbookParam() { |
||||
return workbookParam; |
||||
} |
||||
|
||||
public void setWorkbookParam(com.alibaba.excel.write.metadata.Workbook workbookParam) { |
||||
this.workbookParam = workbookParam; |
||||
} |
||||
|
||||
public OutputStream getOutputStream() { |
||||
return outputStream; |
||||
} |
||||
|
||||
public void setOutputStream(OutputStream outputStream) { |
||||
this.outputStream = outputStream; |
||||
} |
||||
|
||||
public InputStream getInputStream() { |
||||
return inputStream; |
||||
} |
||||
|
||||
public void setInputStream(InputStream inputStream) { |
||||
this.inputStream = inputStream; |
||||
} |
||||
|
||||
public com.alibaba.excel.event.WriteHandler getWriteHandler() { |
||||
return writeHandler; |
||||
} |
||||
|
||||
public void setWriteHandler(com.alibaba.excel.event.WriteHandler writeHandler) { |
||||
this.writeHandler = writeHandler; |
||||
} |
||||
|
||||
public Boolean getAutoCloseStream() { |
||||
return autoCloseStream; |
||||
} |
||||
|
||||
public void setAutoCloseStream(Boolean autoCloseStream) { |
||||
this.autoCloseStream = autoCloseStream; |
||||
} |
||||
|
||||
public Boolean getConvertAllFiled() { |
||||
return convertAllFiled; |
||||
} |
||||
|
||||
public void setConvertAllFiled(Boolean convertAllFiled) { |
||||
this.convertAllFiled = convertAllFiled; |
||||
} |
||||
|
||||
public File getFile() { |
||||
return file; |
||||
} |
||||
|
||||
public void setFile(File file) { |
||||
this.file = file; |
||||
} |
||||
|
||||
public ExcelTypeEnum getExcelType() { |
||||
return excelType; |
||||
} |
||||
|
||||
public void setExcelType(ExcelTypeEnum excelType) { |
||||
this.excelType = excelType; |
||||
} |
||||
|
||||
public Object getReadCustomObject() { |
||||
return readCustomObject; |
||||
} |
||||
|
||||
public void setReadCustomObject(Object readCustomObject) { |
||||
this.readCustomObject = readCustomObject; |
||||
} |
||||
|
||||
public ReadCache getReadCache() { |
||||
return readCache; |
||||
} |
||||
|
||||
public void setReadCache(ReadCache readCache) { |
||||
this.readCache = readCache; |
||||
} |
||||
|
||||
public Boolean getUse1904windowing() { |
||||
return use1904windowing; |
||||
} |
||||
|
||||
public void setUse1904windowing(Boolean use1904windowing) { |
||||
this.use1904windowing = use1904windowing; |
||||
} |
||||
|
||||
public Boolean getMandatoryUseInputStream() { |
||||
return mandatoryUseInputStream; |
||||
} |
||||
|
||||
public void setMandatoryUseInputStream(Boolean mandatoryUseInputStream) { |
||||
this.mandatoryUseInputStream = mandatoryUseInputStream; |
||||
} |
||||
|
||||
public File getReadTempFile() { |
||||
return readTempFile; |
||||
} |
||||
|
||||
public void setReadTempFile(File readTempFile) { |
||||
this.readTempFile = readTempFile; |
||||
} |
||||
|
||||
@Override |
||||
public HolderEnum holderType() { |
||||
return HolderEnum.WORKBOOK; |
||||
} |
||||
} |
@ -1,86 +0,0 @@
|
||||
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; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue