diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index 13f8496b..d952a496 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -27,7 +27,7 @@ import com.alibaba.excel.analysis.v03.handlers.NumberRecordHandler; import com.alibaba.excel.analysis.v03.handlers.RKRecordHandler; import com.alibaba.excel.analysis.v03.handlers.SSTRecordHandler; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.EachRowAnalysisFinishEvent; +import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.util.CollectionUtils; diff --git a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java index e187b07f..f6a8d307 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java @@ -1,9 +1,5 @@ package com.alibaba.excel.analysis.v07; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import com.alibaba.excel.cache.ReadCache; @@ -14,11 +10,9 @@ import com.alibaba.excel.cache.ReadCache; * @author zhuangjiaju */ public class SharedStringsTableHandler extends DefaultHandler { - private static final Logger LOGGER = LoggerFactory.getLogger(SharedStringsTableHandler.class); - + private static final String T_TAG = "t"; private String currentData; private boolean isT; - private int index = 0; private ReadCache readCache; public SharedStringsTableHandler(ReadCache readCache) { @@ -26,28 +20,14 @@ public class SharedStringsTableHandler extends DefaultHandler { } @Override - public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { - if ("t".equals(name)) { - currentData = null; - isT = true; - } - } - - @Override - public void endElement(String uri, String localName, String name) throws SAXException { - if ("t".equals(name)) { - currentData = null; - isT = false; + public void endElement(String uri, String localName, String name) { + if (T_TAG.equals(name)) { + readCache.put(currentData); } } @Override - public void characters(char[] ch, int start, int length) throws SAXException { - if (isT) { - readCache.put(index++, new String(ch, start, length)); - if (index % 100000 == 0) { - LOGGER.info("row:{} ,mem:{},data:{}", index, Runtime.getRuntime().totalMemory()); - } - } + public void characters(char[] ch, int start, int length) { + currentData = new String(ch, start, length); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index 7a356f61..40964968 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -27,7 +27,7 @@ import com.alibaba.excel.cache.Ehcache; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.metadata.Sheet; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.util.FileUtils; /** @@ -46,6 +46,8 @@ public class XlsxSaxAnalyser implements ExcelExecutor { if (workbookHolder.getReadCache() == null) { workbookHolder.setReadCache(new Ehcache()); } + workbookHolder.getReadCache().init(analysisContext); + OPCPackage pkg = readOpcPackage(workbookHolder); // Analysis sharedStringsTable.xml diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java index 3e592c0e..8ae92804 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java @@ -7,7 +7,7 @@ import org.xml.sax.Attributes; import com.alibaba.excel.analysis.v07.XlsxCellHandler; import com.alibaba.excel.analysis.v07.XlsxRowResultHolder; import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.event.EachRowAnalysisFinishEvent; +import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent; public class ProcessResultCellHandler implements XlsxCellHandler { private AnalysisContext analysisContext; @@ -28,7 +28,7 @@ public class ProcessResultCellHandler implements XlsxCellHandler { @Override public void endHandle(String name) { - analysisContext.currentSheetHolder().notifyAll( + analysisContext.currentSheetHolder().notifyEndOneRow( new EachRowAnalysisFinishEvent(rowResultHandler.getCurRowContent(), rowResultHandler.getColumnSize()), analysisContext); rowResultHandler.clearResult(); diff --git a/src/main/java/com/alibaba/excel/cache/Ehcache.java b/src/main/java/com/alibaba/excel/cache/Ehcache.java index ff54fedb..375b499f 100644 --- a/src/main/java/com/alibaba/excel/cache/Ehcache.java +++ b/src/main/java/com/alibaba/excel/cache/Ehcache.java @@ -4,10 +4,12 @@ import java.io.File; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedList; import java.util.Map; import java.util.Set; -import java.util.TreeMap; +import java.util.UUID; +import org.ehcache.CacheManager; import org.ehcache.PersistentCacheManager; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; @@ -16,7 +18,8 @@ import org.ehcache.config.units.MemoryUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.excel.util.POITempFile; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.StringUtils; /** @@ -25,99 +28,168 @@ import com.alibaba.excel.util.StringUtils; * @author zhuangjiaju */ public class Ehcache implements ReadCache { + private static final Logger LOGGER = LoggerFactory.getLogger(Ehcache.class); - private static final int BATCH = 500; - // private org.ehcache.ReadCache cache; - int index = 0; - int expirekey = 0; - private Map cache = new HashMap(); + + private static final int BATCH_COUNT = 500; + private static final int CHECK_INTERVAL = 1000; + private static final int MAX_CACHE_ACTIVATE = 10; + + private static final String CACHE = "cache"; + private static final String DATA_SEPARATOR = "@"; + private static final String KEY_VALUE_SEPARATOR = "!"; + private static final String SPECIAL_SEPARATOR = "&"; + private static final String ESCAPED_DATA_SEPARATOR = "&d;"; + private static final String ESCAPED_KEY_VALUE_SEPARATOR = "&kv;"; + private static final String ESCAPED_SPECIAL_SEPARATOR = "&s;"; + + private int index = 0; + private StringBuilder data = new StringBuilder(); + private CacheManager cacheManager; + /** + * Bulk storage data + */ + private org.ehcache.Cache cache; + /** + * Currently active cache + */ private Map> cacheMap = new HashMap>(); - private TreeMap expire = new TreeMap(); - private StringBuilder sb = new StringBuilder(); - private Set count = new HashSet(); - private int getCount = 1; + /** + * Count how many times get + */ + private int getCount = 0; + /** + * Count active cache + * + */ + private LinkedList countList = new LinkedList(); - public Ehcache() { - File file = POITempFile.createCacheTmpFile(); + /** + * Count the last {@link #CHECK_INTERVAL} used + */ + private Set lastCheckIntervalUsedSet = new HashSet(); + + /** + * Count the number of cache misses + */ + private int cacheMiss = 0; + + @Override + public void init(AnalysisContext analysisContext) { + File readTempFile = analysisContext.currentWorkbookHolder().getReadTempFile(); + if (readTempFile == null) { + readTempFile = FileUtils.createCacheTmpFile(); + analysisContext.currentWorkbookHolder().setReadTempFile(readTempFile); + } + File cacheFile = new File(readTempFile.getPath(), UUID.randomUUID().toString()); PersistentCacheManager persistentCacheManager = - CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(file)) - .withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, + CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)) + .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); + cacheManager = persistentCacheManager; + 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(); + public void put(String value) { + data.append(index).append(KEY_VALUE_SEPARATOR).append(escape(value)).append(DATA_SEPARATOR); + if ((index + 1) % BATCH_COUNT == 0) { + cache.put(index / BATCH_COUNT, data.toString()); + data = new StringBuilder(); } + index++; + } + + private String escape(String str) { + if (StringUtils.isEmpty(str)) { + return str; + } + str = str.replaceAll(SPECIAL_SEPARATOR, ESCAPED_SPECIAL_SEPARATOR); + str = str.replaceAll(DATA_SEPARATOR, ESCAPED_DATA_SEPARATOR); + str = str.replaceAll(KEY_VALUE_SEPARATOR, ESCAPED_KEY_VALUE_SEPARATOR); + return str; + } + + private String unescape(String str) { + if (StringUtils.isEmpty(str)) { + return str; + } + str = str.replaceAll(ESCAPED_KEY_VALUE_SEPARATOR, KEY_VALUE_SEPARATOR); + str = str.replaceAll(ESCAPED_DATA_SEPARATOR, DATA_SEPARATOR); + str = str.replaceAll(ESCAPED_SPECIAL_SEPARATOR, SPECIAL_SEPARATOR); + return str; } @Override public String get(Integer key) { - int route = key / BATCH; - route++; - if (!cacheMap.containsKey(route)) { - // LOGGER.info("key:{},route:{},missing", key, route); - Map map = new HashMap(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); + if (key == null || key < 0) { + return null; } - count.add(route); - - if (getCount++ % 1000 == 0) { - Iterator>> iterator = cacheMap.entrySet().iterator(); - // LOGGER.info("size:{}", cacheMap.size()); - while (iterator.hasNext()) { - Map.Entry> entry = iterator.next(); - if (!count.contains(entry.getKey())) { - // LOGGER.info("route:{},remove", entry.getKey()); - iterator.remove(); - Iterator> ex = expire.entrySet().iterator(); - while (ex.hasNext()) { - Map.Entry e = ex.next(); - if (e.getValue().equals(entry.getKey())) { - ex.remove(); - break; - } - } - } - } - count.clear(); + getCount++; + int route = key / BATCH_COUNT; + if (cacheMap.containsKey(route)) { + lastCheckIntervalUsedSet.add(route); + countList.add(route); + checkClear(); + return cacheMap.get(route).get(key); + } + Map tempCacheMap = new HashMap(BATCH_COUNT / 3 * 4 + 1); + String batchData = cache.get(route); + String[] dataStrings = batchData.split(DATA_SEPARATOR); + for (String dataString : dataStrings) { + String[] keyValue = dataString.split(KEY_VALUE_SEPARATOR); + tempCacheMap.put(Integer.valueOf(keyValue[0]), unescape(keyValue[1])); + } + cacheMap.put(route, tempCacheMap); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Cache misses count:{}", cacheMiss++); } + lastCheckIntervalUsedSet.add(route); + checkClear(); + return tempCacheMap.get(key); + } - if (expire.size() > 10) { - int value1=expire.firstEntry().getValue(); - int key1=expire.firstEntry().getKey(); - cacheMap.remove(value1); - expire.remove(key1); + private void checkClear() { + if (countList.size() > MAX_CACHE_ACTIVATE) { + Integer route = countList.getFirst(); + countList.removeFirst(); + cacheMap.remove(route); + } + if (getCount++ % CHECK_INTERVAL != 0) { + return; } - - return cacheMap.get(route).get(key); + Iterator>> iterator = cacheMap.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry> entry = iterator.next(); + if (lastCheckIntervalUsedSet.contains(entry.getKey())) { + continue; + } + // Last 'CHECK_INTERVAL' not use + iterator.remove(); + Iterator countIterator = countList.iterator(); + while (countIterator.hasNext()) { + Integer route = countIterator.next(); + if (route.equals(entry.getKey())) { + countIterator.remove(); + break; + } + } + } + lastCheckIntervalUsedSet.clear(); } @Override - public void finish() { - if (StringUtils.isEmpty(sb.toString())) { + public void putFinished() { + if (StringUtils.isEmpty(data.toString())) { return; } - int key1 = index / BATCH; - if (index % BATCH != 0) { - key1++; - } + cache.put(index / BATCH_COUNT, data.toString()); + } - cache.put(key1, sb.toString()); - // LOGGER.info("put:key:{},{}", key1 + 1, sb.toString()); + @Override + public void destroy() { + cacheManager.close(); } + } diff --git a/src/main/java/com/alibaba/excel/cache/EhcacheFile.java b/src/main/java/com/alibaba/excel/cache/EhcacheFile.java deleted file mode 100644 index 2a2f5a2f..00000000 --- a/src/main/java/com/alibaba/excel/cache/EhcacheFile.java +++ /dev/null @@ -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 cache; - // private Map cache = new HashMap(); - private Map> cacheMap = new HashMap>(); - private TreeMap expire = new TreeMap(); - private StringBuilder sb = new StringBuilder(); - private Set count = new HashSet(); - 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 map = new HashMap(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>> iterator = cacheMap.entrySet().iterator(); - // LOGGER.info("size:{}", cacheMap.size()); - while (iterator.hasNext()) { - Map.Entry> entry = iterator.next(); - if (!count.contains(entry.getKey())) { - // LOGGER.info("route:{},remove", entry.getKey()); - iterator.remove(); - Iterator> ex = expire.entrySet().iterator(); - while (ex.hasNext()) { - Map.Entry 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()); - } -} diff --git a/src/main/java/com/alibaba/excel/cache/EhcacheMix.java b/src/main/java/com/alibaba/excel/cache/EhcacheMix.java deleted file mode 100644 index c9eef8fc..00000000 --- a/src/main/java/com/alibaba/excel/cache/EhcacheMix.java +++ /dev/null @@ -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 cache; - private Map> cacheMap = new HashMap>(); - private TreeMap expire = new TreeMap(); - private StringBuilder sb = new StringBuilder(); - private Set count = new HashSet(); - 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 map = new HashMap(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>> iterator = cacheMap.entrySet().iterator(); - // LOGGER.info("size:{}", cacheMap.size()); - while (iterator.hasNext()) { - Map.Entry> entry = iterator.next(); - if (!count.contains(entry.getKey())) { - // LOGGER.info("route:{},remove", entry.getKey()); - iterator.remove(); - Iterator> ex = expire.entrySet().iterator(); - while (ex.hasNext()) { - Map.Entry 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()); - } -} diff --git a/src/main/java/com/alibaba/excel/cache/Ehcache2.java b/src/main/java/com/alibaba/excel/cache/MapCache.java similarity index 55% rename from src/main/java/com/alibaba/excel/cache/Ehcache2.java rename to src/main/java/com/alibaba/excel/cache/MapCache.java index 4a6aa4cd..4aaf7e7c 100644 --- a/src/main/java/com/alibaba/excel/cache/Ehcache2.java +++ b/src/main/java/com/alibaba/excel/cache/MapCache.java @@ -3,6 +3,8 @@ package com.alibaba.excel.cache; import java.util.HashMap; import java.util.Map; +import com.alibaba.excel.context.AnalysisContext; + /** * * Putting temporary data directly into a map is a little more efficient but very memory intensive @@ -11,19 +13,28 @@ import java.util.Map; */ public class MapCache implements ReadCache { private Map cache = new HashMap(); + private int index = 0; - public MapCache() {} + @Override + public void init(AnalysisContext analysisContext) {} @Override - public void put(Integer key, String value) { - cache.put(key, value); + public void put(String value) { + cache.put(index++, value); } @Override public String get(Integer key) { + if (key == null || key < 0) { + return null; + } return cache.get(key); } @Override - public void finish() {} + public void putFinished() {} + + @Override + public void destroy() {} + } diff --git a/src/main/java/com/alibaba/excel/cache/ReadCache.java b/src/main/java/com/alibaba/excel/cache/ReadCache.java index ce4cd9bc..e31f7c21 100644 --- a/src/main/java/com/alibaba/excel/cache/ReadCache.java +++ b/src/main/java/com/alibaba/excel/cache/ReadCache.java @@ -1,11 +1,21 @@ package com.alibaba.excel.cache; +import com.alibaba.excel.context.AnalysisContext; + /** * Read cache * * @author zhuangjiaju */ public interface ReadCache { + + /** + * Initialize cache + * + * @param analysisContext + */ + void init(AnalysisContext analysisContext); + /** * Automatically generate the key and put it in the cache.Key start from 0 * diff --git a/src/main/java/com/alibaba/excel/cache/SharedStringsTableReadCache.java b/src/main/java/com/alibaba/excel/cache/SharedStringsTableReadCache.java deleted file mode 100644 index 078d9b69..00000000 --- a/src/main/java/com/alibaba/excel/cache/SharedStringsTableReadCache.java +++ /dev/null @@ -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() { - - } -} diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContext.java b/src/main/java/com/alibaba/excel/context/AnalysisContext.java index 44a9e3b0..5ea5a318 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContext.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContext.java @@ -1,9 +1,8 @@ package com.alibaba.excel.context; -import com.alibaba.excel.event.EachRowAnalysisFinishEvent; -import com.alibaba.excel.metadata.holder.ReadConfiguration; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.read.ReadConfiguration; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; /** * diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index 08000439..73ffd9d3 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -3,8 +3,8 @@ package com.alibaba.excel.context; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; /** * diff --git a/src/main/java/com/alibaba/excel/context/WriteContext.java b/src/main/java/com/alibaba/excel/context/WriteContext.java index 8a27660a..26b2e02c 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContext.java +++ b/src/main/java/com/alibaba/excel/context/WriteContext.java @@ -1,10 +1,10 @@ package com.alibaba.excel.context; import com.alibaba.excel.metadata.Table; -import com.alibaba.excel.metadata.holder.WriteConfiguration; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.WriteConfiguration; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; /** * Write context diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index 1c0a4bf4..1d44b2c3 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -14,10 +14,10 @@ import org.slf4j.LoggerFactory; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Table; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; -import com.alibaba.excel.metadata.holder.WorkbookHolder; -import com.alibaba.excel.metadata.holder.WriteConfiguration; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.WriteConfiguration; import com.alibaba.excel.metadata.property.ExcelHeadProperty; import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.write.handler.CellWriteHandler; diff --git a/src/main/java/com/alibaba/excel/metadata/holder/read/AbstractWriteConfiguration.java b/src/main/java/com/alibaba/excel/metadata/holder/read/AbstractWriteConfiguration.java new file mode 100644 index 00000000..e1ea1c50 --- /dev/null +++ b/src/main/java/com/alibaba/excel/metadata/holder/read/AbstractWriteConfiguration.java @@ -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, List> writeHandlerMap; + /** + * Read listener + */ + private List readListenerList; + /** + * Converter for workbook + */ + private Map readConverterMap; + /** + * Converter for workbook + */ + private Map 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. + * + *
  • 0 - This Sheet has no head ,since the first row are the data + *
  • 1 - This Sheet has one row head , this is the default + *
  • 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> 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, List> getWriteHandlerMap() { + return writeHandlerMap; + } + + public void setWriteHandlerMap(Map, List> 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> getHead() { + return head; + } + + public void setHead(List> 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 getReadListenerList() { + return readListenerList; + } + + public void setReadListenerList(List readListenerList) { + this.readListenerList = readListenerList; + } + + public Map getReadConverterMap() { + return readConverterMap; + } + + public void setReadConverterMap(Map readConverterMap) { + this.readConverterMap = readConverterMap; + } + + public Map getWriteConverterMap() { + return writeConverterMap; + } + + public void setWriteConverterMap(Map 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 cellDataList = (List)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 cellDataList) { + if (!HeadKindEnum.CLASS.equals(analysisContext.currentConfiguration().excelHeadProperty().getHeadKind())) { + return; + } + List dataList = (List)buildStringList(cellDataList, analysisContext.currentConfiguration()); + ExcelHeadProperty excelHeadPropertyData = analysisContext.currentConfiguration().excelHeadProperty(); + Map headMapData = excelHeadPropertyData.getHeadMap(); + Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); + Map tmpHeadMap = new HashMap(headMapData.size() * 4 / 3 + 1); + Map tmpContentPropertyMap = + new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); + for (Map.Entry 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 data, ReadConfiguration readConfiguration) { + List list = new ArrayList(); + 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, List> sortAndClearUpHandler( + List handlerList, Map, List> parentHandlerMap) { + // add + if (parentHandlerMap != null) { + for (List parentHandlerList : parentHandlerMap.values()) { + handlerList.addAll(parentHandlerList); + } + } + // sort + Map> orderExcelWriteHandlerMap = new TreeMap>(); + 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 tempHandlerList = new ArrayList(); + tempHandlerList.add(handler); + orderExcelWriteHandlerMap.put(order, tempHandlerList); + } + } + // clean up + Set alreadyExistedHandlerSet = new HashSet(); + List cleanUpHandlerList = new ArrayList(); + for (Map.Entry> 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, List> result = + new HashMap, List>(); + result.put(WriteHandler.class, new ArrayList()); + result.put(WorkbookWriteHandler.class, new ArrayList()); + result.put(SheetWriteHandler.class, new ArrayList()); + result.put(RowWriteHandler.class, new ArrayList()); + result.put(CellWriteHandler.class, new ArrayList()); + 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 handlerList) { + if (!HeadKindEnum.CLASS.equals(getExcelHeadProperty().getHeadKind())) { + return; + } + Map headMap = getExcelHeadProperty().getHeadMap(); + Map contentPropertyMap = getExcelHeadProperty().getContentPropertyMap(); + + boolean hasCellStyle = false; + boolean hasColumnWidth = false; + for (Map.Entry 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 handlerList, Map 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 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 handlerList, + final Map 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, List> 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 readListenerList() { + return getReadListenerList(); + } + + @Override + public Map readConverterMap() { + return getReadConverterMap(); + } + + @Override + public Map writeConverterMap() { + return getWriteConverterMap(); + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/holder/ReadConfiguration.java b/src/main/java/com/alibaba/excel/metadata/holder/read/ReadConfiguration.java similarity index 94% rename from src/main/java/com/alibaba/excel/metadata/holder/ReadConfiguration.java rename to src/main/java/com/alibaba/excel/metadata/holder/read/ReadConfiguration.java index a9358be6..c6381da2 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/ReadConfiguration.java +++ b/src/main/java/com/alibaba/excel/metadata/holder/read/ReadConfiguration.java @@ -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; diff --git a/src/main/java/com/alibaba/excel/metadata/holder/read/SheetHolder.java b/src/main/java/com/alibaba/excel/metadata/holder/read/SheetHolder.java new file mode 100644 index 00000000..3082a167 --- /dev/null +++ b/src/main/java/com/alibaba/excel/metadata/holder/read/SheetHolder.java @@ -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 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 handlerList = new ArrayList(); + if (sheet.getCustomWriteHandlerList() != null && !sheet.getCustomWriteHandlerList().isEmpty()) { + handlerList.addAll(sheet.getCustomWriteHandlerList()); + } + // Initialization Annotation + sheetHolder.initAnnotationConfig(handlerList); + + sheetHolder + .setWriteHandlerMap(sheetHolder.sortAndClearUpHandler(handlerList, workbookHolder.getWriteHandlerMap())); + Map converterMap = new HashMap(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()); + 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 converterMap = + new HashMap(workbookHolder.getReadConverterMap()); + if (sheet.getCustomConverterList() != null && !sheet.getCustomConverterList().isEmpty()) { + for (Converter converter : sheet.getCustomConverterList()) { + converterMap.put(ConverterKey.buildConverterKey(converter), converter); + } + } + sheetHolder.setReadConverterMap(converterMap); + + List readListenerList = new ArrayList(); + 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 columnWidthMap = sheet.getColumnWidthMap(); + if (sheet.getCustomWriteHandlerList() == null) { + sheet.setCustomWriteHandlerList(new ArrayList()); + } + 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()); + } + 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 getHasBeenInitializedTable() { + return hasBeenInitializedTable; + } + + public void setHasBeenInitializedTable(Map 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; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/holder/read/TableHolder.java b/src/main/java/com/alibaba/excel/metadata/holder/read/TableHolder.java new file mode 100644 index 00000000..1cf0bf35 --- /dev/null +++ b/src/main/java/com/alibaba/excel/metadata/holder/read/TableHolder.java @@ -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 handlerList = new ArrayList(); + if (table.getCustomWriteHandlerList() != null && !table.getCustomWriteHandlerList().isEmpty()) { + handlerList.addAll(table.getCustomWriteHandlerList()); + } + // Initialization Annotation + tableHolder.initAnnotationConfig(handlerList); + + tableHolder + .setWriteHandlerMap(tableHolder.sortAndClearUpHandler(handlerList, sheetHolder.getWriteHandlerMap())); + Map converterMap = new HashMap(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()); + } + 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; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/holder/read/WorkbookHolder.java b/src/main/java/com/alibaba/excel/metadata/holder/read/WorkbookHolder.java new file mode 100644 index 00000000..f945c6f9 --- /dev/null +++ b/src/main/java/com/alibaba/excel/metadata/holder/read/WorkbookHolder.java @@ -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 hasBeenInitializedSheet; + /** + * current param + */ + private com.alibaba.excel.metadata.Workbook workbookParam; + /** + * Final output stream + */ + private OutputStream outputStream; + /** + *
  • write: Template input stream + *
  • read: Read InputStream + *

    + * If 'inputStream' and 'file' all not empty,file first + */ + private InputStream inputStream; + /** + *

  • write: Template file + *
  • read: Read file + *

    + * 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 handlerList = new ArrayList(); + if (workbook.getCustomWriteHandlerList() != null && !workbook.getCustomWriteHandlerList().isEmpty()) { + handlerList.addAll(workbook.getCustomWriteHandlerList()); + } + handlerList.addAll(DefaultWriteHandlerLoader.loadDefaultHandler()); + workbookHolder.setWriteHandlerMap(workbookHolder.sortAndClearUpHandler(handlerList, null)); + + Map 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()); + 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 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 readListenerList = new ArrayList(); + 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 getHasBeenInitializedSheet() { + return hasBeenInitializedSheet; + } + + public void setHasBeenInitializedSheet(Map 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; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/holder/AbstractWriteConfiguration.java b/src/main/java/com/alibaba/excel/metadata/holder/write/AbstractWriteConfiguration.java similarity index 99% rename from src/main/java/com/alibaba/excel/metadata/holder/AbstractWriteConfiguration.java rename to src/main/java/com/alibaba/excel/metadata/holder/write/AbstractWriteConfiguration.java index b4586b1f..b04c5e3b 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/AbstractWriteConfiguration.java +++ b/src/main/java/com/alibaba/excel/metadata/holder/write/AbstractWriteConfiguration.java @@ -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; @@ -16,7 +16,8 @@ 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.AnalysisFinishEvent; +import com.alibaba.excel.metadata.holder.read.ReadConfiguration; +import com.alibaba.excel.read.listener.event.AnalysisFinishEvent; import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.event.Order; import com.alibaba.excel.exception.ExcelAnalysisException; diff --git a/src/main/java/com/alibaba/excel/metadata/holder/SheetHolder.java b/src/main/java/com/alibaba/excel/metadata/holder/write/SheetHolder.java similarity index 96% rename from src/main/java/com/alibaba/excel/metadata/holder/SheetHolder.java rename to src/main/java/com/alibaba/excel/metadata/holder/write/SheetHolder.java index a1517a67..03c2f563 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/SheetHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/holder/write/SheetHolder.java @@ -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; @@ -53,7 +53,7 @@ public class SheetHolder extends AbstractWriteConfiguration { private com.alibaba.excel.metadata.Sheet sheetParam; public static SheetHolder buildWriteWorkSheetHolder(com.alibaba.excel.metadata.Sheet sheet, - WorkbookHolder workbookHolder) { + WorkbookHolder workbookHolder) { SheetHolder sheetHolder = buildBaseSheetHolder(sheet, workbookHolder); sheetHolder.setNewInitialization(Boolean.TRUE); @@ -93,7 +93,7 @@ public class SheetHolder extends AbstractWriteConfiguration { } public static SheetHolder buildReadWorkSheetHolder(com.alibaba.excel.metadata.Sheet sheet, - WorkbookHolder workbookHolder) { + WorkbookHolder workbookHolder) { SheetHolder sheetHolder = buildBaseSheetHolder(sheet, workbookHolder); if (sheet.getReadHeadRowNumber() == null) { if (workbookHolder.getReadHeadRowNumber() == null) { @@ -123,7 +123,7 @@ public class SheetHolder extends AbstractWriteConfiguration { } private static SheetHolder buildBaseSheetHolder(com.alibaba.excel.metadata.Sheet sheet, - WorkbookHolder workbookHolder) { + WorkbookHolder workbookHolder) { SheetHolder sheetHolder = new SheetHolder(); sheetHolder.setSheetParam(sheet); sheetHolder.setParentWorkBook(workbookHolder); diff --git a/src/main/java/com/alibaba/excel/metadata/holder/TableHolder.java b/src/main/java/com/alibaba/excel/metadata/holder/write/TableHolder.java similarity index 99% rename from src/main/java/com/alibaba/excel/metadata/holder/TableHolder.java rename to src/main/java/com/alibaba/excel/metadata/holder/write/TableHolder.java index 4fa74add..bebc30f9 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/TableHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/holder/write/TableHolder.java @@ -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; diff --git a/src/main/java/com/alibaba/excel/metadata/holder/WorkbookHolder.java b/src/main/java/com/alibaba/excel/metadata/holder/write/WorkbookHolder.java similarity index 99% rename from src/main/java/com/alibaba/excel/metadata/holder/WorkbookHolder.java rename to src/main/java/com/alibaba/excel/metadata/holder/write/WorkbookHolder.java index b65b8d6a..c9a011f0 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/WorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/holder/write/WorkbookHolder.java @@ -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; diff --git a/src/main/java/com/alibaba/excel/metadata/holder/WriteConfiguration.java b/src/main/java/com/alibaba/excel/metadata/holder/write/WriteConfiguration.java similarity index 96% rename from src/main/java/com/alibaba/excel/metadata/holder/WriteConfiguration.java rename to src/main/java/com/alibaba/excel/metadata/holder/write/WriteConfiguration.java index 3a88a05d..68401b97 100644 --- a/src/main/java/com/alibaba/excel/metadata/holder/WriteConfiguration.java +++ b/src/main/java/com/alibaba/excel/metadata/holder/write/WriteConfiguration.java @@ -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; diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index 136e0338..04db6d58 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -23,7 +23,7 @@ import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.CellRange; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.util.StringUtils; /** diff --git a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java index 68cf5179..65da952e 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -14,7 +14,7 @@ import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.ReadConfiguration; +import com.alibaba.excel.metadata.holder.read.ReadConfiguration; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelHeadProperty; diff --git a/src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java b/src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java index 745d5e97..b92a3d1b 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java +++ b/src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java @@ -2,7 +2,7 @@ package com.alibaba.excel.read.listener; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.excel.event.AnalysisFinishEvent; +import com.alibaba.excel.read.listener.event.AnalysisFinishEvent; /** * Registry center. diff --git a/src/main/java/com/alibaba/excel/event/AnalysisFinishEvent.java b/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java similarity index 61% rename from src/main/java/com/alibaba/excel/event/AnalysisFinishEvent.java rename to src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java index 4da100c4..fb935429 100644 --- a/src/main/java/com/alibaba/excel/event/AnalysisFinishEvent.java +++ b/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java @@ -1,4 +1,4 @@ -package com.alibaba.excel.event; +package com.alibaba.excel.read.listener.event; public interface AnalysisFinishEvent { Object getAnalysisResult(); diff --git a/src/main/java/com/alibaba/excel/event/EachRowAnalysisFinishEvent.java b/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java similarity index 93% rename from src/main/java/com/alibaba/excel/event/EachRowAnalysisFinishEvent.java rename to src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java index 1732e596..aa58ca6d 100644 --- a/src/main/java/com/alibaba/excel/event/EachRowAnalysisFinishEvent.java +++ b/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java @@ -1,4 +1,4 @@ -package com.alibaba.excel.event; +package com.alibaba.excel.read.listener.event; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java index fa68e0b7..650c2a78 100644 --- a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java +++ b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java @@ -13,7 +13,7 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.support.ExcelTypeEnum; /** diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index d3f05eb1..4f1c82ea 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -17,7 +17,7 @@ import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Table; -import com.alibaba.excel.metadata.holder.WriteConfiguration; +import com.alibaba.excel.metadata.holder.write.WriteConfiguration; import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.POITempFile; diff --git a/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java index 770320b9..b62c6e68 100644 --- a/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/CellWriteHandler.java @@ -4,8 +4,8 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; import com.sun.istack.internal.Nullable; /** @@ -26,7 +26,7 @@ public interface CellWriteHandler extends WriteHandler { * @param isHead */ void beforeCellCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, Row row, Head head, - int relativeRowIndex, boolean isHead); + int relativeRowIndex, boolean isHead); /** * called after the cell is created @@ -39,5 +39,5 @@ public interface CellWriteHandler extends WriteHandler { * @param isHead */ void afterCellCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, Cell cell, Head head, - int relativeRowIndex, boolean isHead); + int relativeRowIndex, boolean isHead); } diff --git a/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java index 7a08e18b..75f086c5 100644 --- a/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/RowWriteHandler.java @@ -2,8 +2,8 @@ package com.alibaba.excel.write.handler; import org.apache.poi.ss.usermodel.Row; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; import com.sun.istack.internal.Nullable; /** @@ -23,7 +23,7 @@ public interface RowWriteHandler extends WriteHandler { * @param isHead */ void beforeRowCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, int rowIndex, int relativeRowIndex, - boolean isHead); + boolean isHead); /** * called after the row is created @@ -35,5 +35,5 @@ public interface RowWriteHandler extends WriteHandler { * @param isHead */ void afterRowCreate(SheetHolder sheetHolder, @Nullable TableHolder tableHolder, Row row, int relativeRowIndex, - boolean isHead); + boolean isHead); } diff --git a/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java index 5290c3b4..f69b909f 100644 --- a/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/SheetWriteHandler.java @@ -1,7 +1,7 @@ package com.alibaba.excel.write.handler; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; /** * intercepts handle sheet creation diff --git a/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java b/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java index 2b6a9e0b..846a8995 100644 --- a/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java +++ b/src/main/java/com/alibaba/excel/write/handler/WorkbookWriteHandler.java @@ -1,6 +1,6 @@ package com.alibaba.excel.write.handler; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; /** * intercepts handle Workbook creation diff --git a/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java b/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java index b9b8adc7..ecc45659 100644 --- a/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java +++ b/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java @@ -5,8 +5,8 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.handler.CellWriteHandler; /** @@ -18,11 +18,11 @@ public abstract class AbstractMergeStrategy implements CellWriteHandler { @Override public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, - int relativeRowIndex, boolean isHead) {} + int relativeRowIndex, boolean isHead) {} @Override public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, - int relativeRowIndex, boolean isHead) { + int relativeRowIndex, boolean isHead) { if (isHead) { return; } diff --git a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java index 887def2b..9725ca51 100644 --- a/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/AbstractCellStyleStrategy.java @@ -6,9 +6,9 @@ import org.apache.poi.ss.usermodel.Workbook; import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; -import com.alibaba.excel.metadata.holder.WorkbookHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; +import com.alibaba.excel.metadata.holder.write.WorkbookHolder; import com.alibaba.excel.write.handler.CellWriteHandler; import com.alibaba.excel.write.handler.WorkbookWriteHandler; @@ -33,11 +33,11 @@ public abstract class AbstractCellStyleStrategy implements CellWriteHandler, Wor @Override public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, - int relativeRowIndex, boolean isHead) {} + int relativeRowIndex, boolean isHead) {} @Override public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, - int relativeRowIndex, boolean isHead) { + int relativeRowIndex, boolean isHead) { if (isHead) { setHeadCellStyle(cell, head, relativeRowIndex); } else { diff --git a/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java index 610aa026..5d158e04 100644 --- a/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/column/AbstractColumnWidthStyleStrategy.java @@ -6,8 +6,8 @@ import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.handler.CellWriteHandler; import com.sun.istack.internal.Nullable; @@ -25,11 +25,11 @@ public abstract class AbstractColumnWidthStyleStrategy implements CellWriteHandl @Override public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, - int relativeRowIndex, boolean isHead) {} + int relativeRowIndex, boolean isHead) {} @Override public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, - int relativeRowIndex, boolean isHead) { + int relativeRowIndex, boolean isHead) { if (!isHead && relativeRowIndex != 0) { return; } diff --git a/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java b/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java index 9961c705..cd335c24 100644 --- a/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java +++ b/src/main/java/com/alibaba/excel/write/style/row/AbstractRowHeightStyleStrategy.java @@ -3,8 +3,8 @@ package com.alibaba.excel.write.style.row; import org.apache.poi.ss.usermodel.Row; import com.alibaba.excel.event.NotRepeatExecutor; -import com.alibaba.excel.metadata.holder.SheetHolder; -import com.alibaba.excel.metadata.holder.TableHolder; +import com.alibaba.excel.metadata.holder.write.SheetHolder; +import com.alibaba.excel.metadata.holder.write.TableHolder; import com.alibaba.excel.write.handler.RowWriteHandler; /** @@ -21,13 +21,13 @@ public abstract class AbstractRowHeightStyleStrategy implements RowWriteHandler, @Override public void beforeRowCreate(SheetHolder sheetHolder, TableHolder tableHolder, int rowIndex, int relativeRowIndex, - boolean isHead) { + boolean isHead) { } @Override public void afterRowCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, int relativeRowIndex, - boolean isHead) { + boolean isHead) { if (isHead) { setHeadColumnHeight(row, relativeRowIndex); } else {