zhuangjiaju
5 years ago
28 changed files with 489 additions and 68 deletions
@ -0,0 +1,53 @@
|
||||
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.Cache; |
||||
|
||||
/** |
||||
* Sax read sharedStringsTable.xml |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SharedStringsTableHandler extends DefaultHandler { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SharedStringsTableHandler.class); |
||||
|
||||
private String currentData; |
||||
private boolean isT; |
||||
private int index = 0; |
||||
private Cache cache; |
||||
|
||||
public SharedStringsTableHandler(Cache cache) { |
||||
this.cache = cache; |
||||
} |
||||
|
||||
@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; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void characters(char[] ch, int start, int length) throws SAXException { |
||||
if (isT) { |
||||
cache.put(index++, new String(ch, start, length)); |
||||
if (index % 100000 == 0) { |
||||
LOGGER.info("row:{} ,mem:{},data:{}", index, Runtime.getRuntime().totalMemory()); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,27 @@
|
||||
package com.alibaba.excel.cache; |
||||
|
||||
/** |
||||
* cache |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public interface Cache { |
||||
|
||||
/** |
||||
* put |
||||
* |
||||
* @param key |
||||
* @param value |
||||
*/ |
||||
void put(Integer key, String value); |
||||
|
||||
/** |
||||
* get |
||||
* |
||||
* @param key |
||||
* @return |
||||
*/ |
||||
String get(Integer key); |
||||
|
||||
void finish(); |
||||
} |
@ -0,0 +1,46 @@
|
||||
package com.alibaba.excel.cache; |
||||
|
||||
import java.io.File; |
||||
import java.util.Map; |
||||
|
||||
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 com.alibaba.excel.util.POITempFile; |
||||
|
||||
/** |
||||
* Default cache |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class Ehcache implements Cache { |
||||
private org.ehcache.Cache<Integer, Map<Integer, String>> cache; |
||||
|
||||
public Ehcache() { |
||||
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) { |
||||
cache.put(key, value); |
||||
} |
||||
|
||||
@Override |
||||
public String get(Integer key) { |
||||
return cache.get(key); |
||||
} |
||||
|
||||
@Override |
||||
public void finish() { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
package com.alibaba.excel.cache; |
||||
|
||||
import org.apache.poi.xssf.model.SharedStringsTable; |
||||
|
||||
/** |
||||
* Default cache |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SharedStringsTableCache implements Cache { |
||||
private SharedStringsTable sharedStringsTable; |
||||
|
||||
public SharedStringsTableCache(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,40 @@
|
||||
package com.alibaba.easyexcel.test.ehcache; |
||||
|
||||
import java.io.File; |
||||
|
||||
import org.ehcache.Cache; |
||||
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.junit.Ignore; |
||||
import org.junit.Test; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.util.POITempFile; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Ignore |
||||
public class EncacheTest { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EncacheTest.class); |
||||
|
||||
@Test |
||||
public void cache() { |
||||
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); |
||||
Cache<Integer, String> cache = persistentCacheManager.getCache("cache", Integer.class, String.class); |
||||
cache.put(1, "测试1"); |
||||
LOGGER.info("cache:{}", cache.get(1)); |
||||
persistentCacheManager.close(); |
||||
LOGGER.info("close"); |
||||
POITempFile.delete(file); |
||||
} |
||||
} |
@ -0,0 +1,33 @@
|
||||
package com.alibaba.easyexcel.test.read.large; |
||||
|
||||
import java.io.InputStream; |
||||
|
||||
import org.junit.Test; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.util.FileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.ExcelReader; |
||||
import com.alibaba.excel.metadata.Sheet; |
||||
|
||||
/** |
||||
* Simple data test |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class LargeData07Test { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(LargeData07Test.class); |
||||
|
||||
@Test |
||||
public void large() throws Exception { |
||||
LOGGER.info("start"); |
||||
long start = System.currentTimeMillis(); |
||||
InputStream inputStream = FileUtil.readFile("large/large07.xlsx"); |
||||
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, new LargeDataListener()); |
||||
excelReader.read(new Sheet(1, 1)); |
||||
inputStream.close(); |
||||
LOGGER.info("time:{}", System.currentTimeMillis() - start); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,29 @@
|
||||
package com.alibaba.easyexcel.test.read.large; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.read.simple.SimpleDataListener; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class LargeDataListener extends AnalysisEventListener<Object> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class); |
||||
private int count = 0; |
||||
|
||||
@Override |
||||
public void invoke(Object object, AnalysisContext context) { |
||||
count++; |
||||
if (count % 100000 == 0) { |
||||
LOGGER.info("row:{} ,mem:{},data:{}", count, Runtime.getRuntime().totalMemory(), JSON.toJSONString(object)); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) {} |
||||
|
||||
} |
@ -0,0 +1,16 @@
|
||||
package com.alibaba.easyexcel.test.read.simple; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class SimpleData { |
||||
@ExcelProperty("字符串1") |
||||
private String string1; |
||||
@ExcelProperty("字符串2") |
||||
private String string2; |
||||
} |
@ -0,0 +1,26 @@
|
||||
package com.alibaba.easyexcel.test.read.simple; |
||||
|
||||
import java.io.InputStream; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import com.alibaba.easyexcel.test.util.FileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.ExcelReader; |
||||
import com.alibaba.excel.metadata.Sheet; |
||||
|
||||
/** |
||||
* Simple data test |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SimpleData07Test { |
||||
|
||||
@Test |
||||
public void simple() throws Exception { |
||||
InputStream inputStream = FileUtil.readFile("simple/simple07.xlsx"); |
||||
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, new SimpleDataListener()); |
||||
excelReader.read(new Sheet(1, 1), SimpleData.class); |
||||
inputStream.close(); |
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
package com.alibaba.easyexcel.test.read.simple; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SimpleDataListener extends AnalysisEventListener<Object> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class); |
||||
|
||||
List<Object> list = new ArrayList<Object>(); |
||||
|
||||
@Override |
||||
public void invoke(Object object, AnalysisContext context) { |
||||
list.add(object); |
||||
LOGGER.info("data:{}", JSON.toJSONString(object)); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
assert list.size() == 10; |
||||
} |
||||
} |
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue