forked from fanruan/easyexcel
zhuangjiaju
5 years ago
39 changed files with 1432 additions and 442 deletions
@ -1,123 +0,0 @@
|
||||
package com.alibaba.excel.cache; |
||||
|
||||
import java.io.File; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.Iterator; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.TreeMap; |
||||
|
||||
import org.ehcache.PersistentCacheManager; |
||||
import org.ehcache.config.builders.CacheConfigurationBuilder; |
||||
import org.ehcache.config.builders.CacheManagerBuilder; |
||||
import org.ehcache.config.builders.ResourcePoolsBuilder; |
||||
import org.ehcache.config.units.MemoryUnit; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.util.POITempFile; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* Default cache |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class EhcacheFile implements ReadCache { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheFile.class); |
||||
private static final int BATCH = 500; |
||||
int index = 0; |
||||
int expirekey = 0; |
||||
private org.ehcache.Cache<Integer, String> cache; |
||||
// private Map<Integer, String> cache = new HashMap<Integer, String>();
|
||||
private Map<Integer, Map<Integer, String>> cacheMap = new HashMap<Integer, Map<Integer, String>>(); |
||||
private TreeMap<Integer, Integer> expire = new TreeMap<Integer, Integer>(); |
||||
private StringBuilder sb = new StringBuilder(); |
||||
private Set<Integer> count = new HashSet<Integer>(); |
||||
private int getCount = 1; |
||||
|
||||
public EhcacheFile() { |
||||
File file = POITempFile.createCacheTmpFile(); |
||||
PersistentCacheManager persistentCacheManager = |
||||
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(file)) |
||||
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, |
||||
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB))) |
||||
.build(true); |
||||
this.cache = persistentCacheManager.getCache("cache", Integer.class, String.class); |
||||
} |
||||
|
||||
@Override |
||||
public void put(Integer key, String value) { |
||||
sb.append(index++).append(":").append(value).append(","); |
||||
if (index != 0 && index % BATCH == 0) { |
||||
int key1 = index / BATCH; |
||||
cache.put(key1, sb.toString()); |
||||
// LOGGER.info("put:key:{},{}", key1, sb.toString());
|
||||
sb = new StringBuilder(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String get(Integer key) { |
||||
int route = key / BATCH; |
||||
route++; |
||||
if (!cacheMap.containsKey(route)) { |
||||
// LOGGER.info("key:{},route:{},missing", key, route);
|
||||
Map<Integer, String> map = new HashMap<Integer, String>(10000 / 3 * 4 + 1); |
||||
String s = cache.get(route); |
||||
String[] values = s.split(","); |
||||
for (String value : values) { |
||||
String[] vv = value.split(":"); |
||||
map.put(Integer.valueOf(vv[0]), vv[1]); |
||||
} |
||||
cacheMap.put(route, map); |
||||
expire.put(expirekey++, route); |
||||
} |
||||
count.add(route); |
||||
|
||||
if (getCount++ % 1000 == 0) { |
||||
Iterator<Map.Entry<Integer, Map<Integer, String>>> iterator = cacheMap.entrySet().iterator(); |
||||
// LOGGER.info("size:{}", cacheMap.size());
|
||||
while (iterator.hasNext()) { |
||||
Map.Entry<Integer, Map<Integer, String>> entry = iterator.next(); |
||||
if (!count.contains(entry.getKey())) { |
||||
// LOGGER.info("route:{},remove", entry.getKey());
|
||||
iterator.remove(); |
||||
Iterator<Map.Entry<Integer, Integer>> ex = expire.entrySet().iterator(); |
||||
while (ex.hasNext()) { |
||||
Map.Entry<Integer, Integer> e = ex.next(); |
||||
if (e.getValue().equals(entry.getKey())) { |
||||
ex.remove(); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
count.clear(); |
||||
} |
||||
|
||||
if (expire.size() > 10) { |
||||
int value1 = expire.firstEntry().getValue(); |
||||
int key1 = expire.firstEntry().getKey(); |
||||
cacheMap.remove(value1); |
||||
expire.remove(key1); |
||||
} |
||||
|
||||
return cacheMap.get(route).get(key); |
||||
} |
||||
|
||||
@Override |
||||
public void finish() { |
||||
if (StringUtils.isEmpty(sb.toString())) { |
||||
return; |
||||
} |
||||
int key1 = index / BATCH; |
||||
if (index % BATCH != 0) { |
||||
key1++; |
||||
} |
||||
|
||||
cache.put(key1, sb.toString()); |
||||
// LOGGER.info("put:key:{},{}", key1 + 1, sb.toString());
|
||||
} |
||||
} |
@ -1,123 +0,0 @@
|
||||
package com.alibaba.excel.cache; |
||||
|
||||
import java.io.File; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.Iterator; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.TreeMap; |
||||
|
||||
import org.ehcache.PersistentCacheManager; |
||||
import org.ehcache.config.builders.CacheConfigurationBuilder; |
||||
import org.ehcache.config.builders.CacheManagerBuilder; |
||||
import org.ehcache.config.builders.ResourcePoolsBuilder; |
||||
import org.ehcache.config.units.MemoryUnit; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.util.POITempFile; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* Default cache |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class EhcacheMix implements ReadCache { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheMix.class); |
||||
private static final int BATCH = 500; |
||||
int index = 0; |
||||
int expirekey = 0; |
||||
private org.ehcache.Cache<Integer, String> cache; |
||||
private Map<Integer, Map<Integer, String>> cacheMap = new HashMap<Integer, Map<Integer, String>>(); |
||||
private TreeMap<Integer, Integer> expire = new TreeMap<Integer, Integer>(); |
||||
private StringBuilder sb = new StringBuilder(); |
||||
private Set<Integer> count = new HashSet<Integer>(); |
||||
private int getCount = 1; |
||||
|
||||
private int countRead = 0; |
||||
|
||||
public EhcacheMix() { |
||||
File file = POITempFile.createCacheTmpFile(); |
||||
PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder() |
||||
.with(CacheManagerBuilder.persistence(file)) |
||||
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, |
||||
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(20, MemoryUnit.MB).disk(2, MemoryUnit.GB, false))) |
||||
.build(true); |
||||
this.cache = persistentCacheManager.getCache("cache", Integer.class, String.class); |
||||
} |
||||
|
||||
@Override |
||||
public void put(Integer key, String value) { |
||||
sb.append(index++).append(":").append(value).append(","); |
||||
if (index != 0 && index % BATCH == 0) { |
||||
int key1 = index / BATCH; |
||||
cache.put(key1, sb.toString()); |
||||
// LOGGER.info("put:key:{},{}", key1, sb.toString());
|
||||
sb = new StringBuilder(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String get(Integer key) { |
||||
int route = key / BATCH; |
||||
route++; |
||||
if (!cacheMap.containsKey(route)) { |
||||
Map<Integer, String> map = new HashMap<Integer, String>(10000 / 3 * 4 + 1); |
||||
String s = cache.get(route); |
||||
String[] values = s.split(","); |
||||
for (String value : values) { |
||||
String[] vv = value.split(":"); |
||||
map.put(Integer.valueOf(vv[0]), vv[1]); |
||||
} |
||||
cacheMap.put(route, map); |
||||
expire.put(expirekey++, route); |
||||
} |
||||
count.add(route); |
||||
|
||||
if (getCount++ % 1000 == 0) { |
||||
Iterator<Map.Entry<Integer, Map<Integer, String>>> iterator = cacheMap.entrySet().iterator(); |
||||
// LOGGER.info("size:{}", cacheMap.size());
|
||||
while (iterator.hasNext()) { |
||||
Map.Entry<Integer, Map<Integer, String>> entry = iterator.next(); |
||||
if (!count.contains(entry.getKey())) { |
||||
// LOGGER.info("route:{},remove", entry.getKey());
|
||||
iterator.remove(); |
||||
Iterator<Map.Entry<Integer, Integer>> ex = expire.entrySet().iterator(); |
||||
while (ex.hasNext()) { |
||||
Map.Entry<Integer, Integer> e = ex.next(); |
||||
if (e.getValue().equals(entry.getKey())) { |
||||
ex.remove(); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
count.clear(); |
||||
} |
||||
|
||||
if (expire.size() > 10) { |
||||
int value1 = expire.firstEntry().getValue(); |
||||
int key1 = expire.firstEntry().getKey(); |
||||
cacheMap.remove(value1); |
||||
expire.remove(key1); |
||||
} |
||||
|
||||
return cacheMap.get(route).get(key); |
||||
} |
||||
|
||||
@Override |
||||
public void finish() { |
||||
if (StringUtils.isEmpty(sb.toString())) { |
||||
return; |
||||
} |
||||
int key1 = index / BATCH; |
||||
if (index % BATCH != 0) { |
||||
key1++; |
||||
} |
||||
|
||||
cache.put(key1, sb.toString()); |
||||
// LOGGER.info("put:key:{},{}", key1 + 1, sb.toString());
|
||||
} |
||||
} |
@ -1,31 +0,0 @@
|
||||
package com.alibaba.excel.cache; |
||||
|
||||
import org.apache.poi.xssf.model.SharedStringsTable; |
||||
|
||||
/** |
||||
* Default cache |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SharedStringsTableReadCache implements ReadCache { |
||||
private SharedStringsTable sharedStringsTable; |
||||
|
||||
public SharedStringsTableReadCache(SharedStringsTable sharedStringsTable) { |
||||
this.sharedStringsTable = sharedStringsTable; |
||||
} |
||||
|
||||
@Override |
||||
public void put(Integer key, String value) { |
||||
throw new UnsupportedOperationException("Can not put value"); |
||||
} |
||||
|
||||
@Override |
||||
public String get(Integer key) { |
||||
return sharedStringsTable.getItemAt(key).toString(); |
||||
} |
||||
|
||||
@Override |
||||
public void finish() { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,494 @@
|
||||
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,4 +1,4 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
package com.alibaba.excel.metadata.holder.read; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
@ -0,0 +1,235 @@
|
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,138 @@
|
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,325 @@
|
||||
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,4 +1,4 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
package com.alibaba.excel.metadata.holder.write; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
@ -1,4 +1,4 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
package com.alibaba.excel.metadata.holder.write; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
@ -1,4 +1,4 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
package com.alibaba.excel.metadata.holder.write; |
||||
|
||||
import java.io.File; |
||||
import java.io.InputStream; |
@ -1,4 +1,4 @@
|
||||
package com.alibaba.excel.metadata.holder; |
||||
package com.alibaba.excel.metadata.holder.write; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
@ -1,4 +1,4 @@
|
||||
package com.alibaba.excel.event; |
||||
package com.alibaba.excel.read.listener.event; |
||||
|
||||
public interface AnalysisFinishEvent { |
||||
Object getAnalysisResult(); |
@ -1,4 +1,4 @@
|
||||
package com.alibaba.excel.event; |
||||
package com.alibaba.excel.read.listener.event; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
Loading…
Reference in new issue