Browse Source

修改读文件策略

bugfix
zhuangjiaju 5 years ago
parent
commit
de671dae09
  1. 7
      pom.xml
  2. 10
      src/main/java/com/alibaba/excel/ExcelReader.java
  3. 1
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  4. 52
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  5. 85
      src/main/java/com/alibaba/excel/cache/Ehcache.java
  6. 36
      src/main/java/com/alibaba/excel/cache/Ehcache2.java
  7. 123
      src/main/java/com/alibaba/excel/cache/EhcacheFile.java
  8. 123
      src/main/java/com/alibaba/excel/cache/EhcacheMix.java
  9. 2
      src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  10. 23
      src/main/java/com/alibaba/excel/event/AnalysisEventListener.java
  11. 2
      src/main/java/com/alibaba/excel/event/Handler.java
  12. 8
      src/main/java/com/alibaba/excel/event/Listener.java
  13. 37
      src/main/java/com/alibaba/excel/event/ReadListener.java
  14. 13
      src/main/java/com/alibaba/excel/metadata/BasicParameter.java
  15. 35
      src/main/java/com/alibaba/excel/metadata/Workbook.java
  16. 29
      src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java
  17. 13
      src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java

7
pom.xml

@ -56,6 +56,13 @@
</licenses> </licenses>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId> <artifactId>poi</artifactId>

10
src/main/java/com/alibaba/excel/ExcelReader.java

@ -184,10 +184,10 @@ public class ExcelReader {
* @param eventListener * @param eventListener
*/ */
private void validateParam(InputStream in, AnalysisEventListener eventListener) { private void validateParam(InputStream in, AnalysisEventListener eventListener) {
if (eventListener == null) { // if (eventListener == null) {
throw new IllegalArgumentException("AnalysisEventListener can not null"); // throw new IllegalArgumentException("AnalysisEventListener can not null");
} else if (in == null) { // } else if (in == null) {
throw new IllegalArgumentException("InputStream can not null"); // throw new IllegalArgumentException("InputStream can not null");
} // }
} }
} }

1
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -75,6 +75,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
throw new ExcelAnalysisException("File type error,io must be available markSupported,you can do like " throw new ExcelAnalysisException("File type error,io must be available markSupported,you can do like "
+ "this <code> new BufferedInputStream(new FileInputStream(\\\"/xxxx\\\"))</code> \"", e); + "this <code> new BufferedInputStream(new FileInputStream(\\\"/xxxx\\\"))</code> \"", e);
} }
return this.saxAnalyser; return this.saxAnalyser;
} }

52
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -14,16 +14,13 @@ import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
import org.xml.sax.ContentHandler; import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import com.alibaba.excel.analysis.BaseSaxAnalyser; import com.alibaba.excel.analysis.BaseSaxAnalyser;
import com.alibaba.excel.cache.Cache; import com.alibaba.excel.cache.Cache;
import com.alibaba.excel.cache.Ehcache; import com.alibaba.excel.cache.EhcacheFile;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
@ -46,12 +43,31 @@ public class XlsxSaxAnalyser extends BaseSaxAnalyser {
this.analysisContext = analysisContext; this.analysisContext = analysisContext;
analysisContext.setCurrentRowNum(0); analysisContext.setCurrentRowNum(0);
OPCPackage pkg = OPCPackage.open(analysisContext.getInputStream()); // OPCPackage pkg =
// OPCPackage.open(new File("D:\\git\\easyexcel\\src\\test\\resources\\read\\large\\large07.xlsx"));
// InputStream input =
// Thread.currentThread().getContextClassLoader().getResourceAsStream("read/large/large07.xlsx");
// File ff = new File("D:\\git\\easyexcel\\src\\test\\resources\\read\\large\\large0722.xlsx");
// OutputStream os = new FileOutputStream(ff);
// int bytesRead = 0;
// byte[] buffer = new byte[8192];
// while ((bytesRead = input.read(buffer, 0, 8192)) != -1) {
// os.write(buffer, 0, bytesRead);
// }
// os.close();
// input.close();
// OPCPackage pkg = OPCPackage.open(ff);
OPCPackage pkg =
OPCPackage.open(Thread.currentThread().getContextClassLoader().getResourceAsStream("read/large/large07.xlsx"));
this.xssfReader = new XSSFReader(pkg); this.xssfReader = new XSSFReader(pkg);
ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()); ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
PackagePart packagePart = parts.get(0); PackagePart packagePart = parts.get(0);
InputSource sheetSource1 = new InputSource(packagePart.getInputStream()); // InputStream sheet = Thread.currentThread().getContextClassLoader()
this.cache = new Ehcache(); // .getResourceAsStream("read/large/large07/xl/sharedStrings.xml");
InputStream sheet = packagePart.getInputStream();
InputSource sheetSource1 = new InputSource(sheet);
this.cache = new EhcacheFile();
try { try {
SAXParserFactory saxFactory = SAXParserFactory.newInstance(); SAXParserFactory saxFactory = SAXParserFactory.newInstance();
saxFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); saxFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
@ -62,23 +78,27 @@ public class XlsxSaxAnalyser extends BaseSaxAnalyser {
ContentHandler handler = new SharedStringsTableHandler(cache); ContentHandler handler = new SharedStringsTableHandler(cache);
xmlReader.setContentHandler(handler); xmlReader.setContentHandler(handler);
xmlReader.parse(sheetSource1); xmlReader.parse(sheetSource1);
packagePart.getInputStream().close(); sheet.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new ExcelAnalysisException(e); throw new ExcelAnalysisException(e);
} }
cache.finish();
InputStream workbookXml = xssfReader.getWorkbookData(); // this.cache=new SharedStringsTableCache(xssfReader.getSharedStringsTable());
WorkbookDocument ctWorkbook = WorkbookDocument.Factory.parse(workbookXml); // InputStream workbookXml = xssfReader.getWorkbookData();
CTWorkbook wb = ctWorkbook.getWorkbook(); // WorkbookDocument ctWorkbook = WorkbookDocument.Factory.parse(workbookXml);
CTWorkbookPr prefix = wb.getWorkbookPr(); // CTWorkbook wb = ctWorkbook.getWorkbook();
if (prefix != null) { // CTWorkbookPr prefix = wb.getWorkbookPr();
this.use1904WindowDate = prefix.getDate1904(); // if (prefix != null) {
} // this.use1904WindowDate = prefix.getDate1904();
// }
this.analysisContext.setUse1904WindowDate(use1904WindowDate); this.analysisContext.setUse1904WindowDate(use1904WindowDate);
// sheetSourceList.add(new SheetSource("test", Thread.currentThread().getContextClassLoader()
// .getResourceAsStream("read/large/large07/xl/worksheets/sheet1.xml")));
XSSFReader.SheetIterator ite; XSSFReader.SheetIterator ite;
sheetSourceList = new ArrayList<SheetSource>(); sheetSourceList = new ArrayList<SheetSource>();
ite = (XSSFReader.SheetIterator)xssfReader.getSheetsData(); ite = (XSSFReader.SheetIterator)xssfReader.getSheetsData();
while (ite.hasNext()) { while (ite.hasNext()) {
InputStream inputStream = ite.next(); InputStream inputStream = ite.next();

85
src/main/java/com/alibaba/excel/cache/Ehcache.java vendored

@ -1,15 +1,23 @@
package com.alibaba.excel.cache; package com.alibaba.excel.cache;
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.ehcache.PersistentCacheManager; import org.ehcache.PersistentCacheManager;
import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.MemoryUnit; 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.POITempFile;
import com.alibaba.excel.util.StringUtils;
/** /**
* Default cache * Default cache
@ -17,7 +25,17 @@ import com.alibaba.excel.util.POITempFile;
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class Ehcache implements Cache { public class Ehcache implements Cache {
private org.ehcache.Cache<Integer, Map<Integer, String>> cache; private static final Logger LOGGER = LoggerFactory.getLogger(Ehcache.class);
private static final int BATCH = 500;
// private org.ehcache.Cache<Integer, String> cache;
int index = 0;
int expirekey = 0;
private Map<Integer, String> cache = new HashMap<Integer, String>();
private Map<Integer, Map<Integer, String>> cacheMap = new HashMap<Integer, Map<Integer, String>>();
private TreeMap<Integer, Integer> expire = new TreeMap<Integer, Integer>();
private StringBuilder sb = new StringBuilder();
private Set<Integer> count = new HashSet<Integer>();
private int getCount = 1;
public Ehcache() { public Ehcache() {
File file = POITempFile.createCacheTmpFile(); File file = POITempFile.createCacheTmpFile();
@ -26,21 +44,80 @@ public class Ehcache implements Cache {
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class, .withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB))) ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)))
.build(true); .build(true);
this.cache = persistentCacheManager.getCache("cache", Integer.class, String.class); // this.cache = persistentCacheManager.getCache("cache", Integer.class, String.class);
} }
@Override @Override
public void put(Integer key, String value) { public void put(Integer key, String value) {
cache.put(key, 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 @Override
public String get(Integer key) { public String get(Integer key) {
return cache.get(key); int route = key / BATCH;
route++;
if (!cacheMap.containsKey(route)) {
// LOGGER.info("key:{},route:{},missing", key, route);
Map<Integer, String> map = new HashMap<Integer, String>(10000 / 3 * 4 + 1);
String s = cache.get(route);
String[] values = s.split(",");
for (String value : values) {
String[] vv = value.split(":");
map.put(Integer.valueOf(vv[0]), vv[1]);
}
cacheMap.put(route, map);
expire.put(expirekey++, route);
}
count.add(route);
if (getCount++ % 1000 == 0) {
Iterator<Map.Entry<Integer, Map<Integer, String>>> iterator = cacheMap.entrySet().iterator();
// LOGGER.info("size:{}", cacheMap.size());
while (iterator.hasNext()) {
Map.Entry<Integer, Map<Integer, String>> entry = iterator.next();
if (!count.contains(entry.getKey())) {
// LOGGER.info("route:{},remove", entry.getKey());
iterator.remove();
Iterator<Map.Entry<Integer, Integer>> ex = expire.entrySet().iterator();
while (ex.hasNext()) {
Map.Entry<Integer, Integer> e = ex.next();
if (e.getValue().equals(entry.getKey())) {
ex.remove();
break;
}
}
}
}
count.clear();
}
if (expire.size() > 10) {
int value1=expire.firstEntry().getValue();
int key1=expire.firstEntry().getKey();
cacheMap.remove(value1);
expire.remove(key1);
}
return cacheMap.get(route).get(key);
} }
@Override @Override
public void finish() { 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());
} }
} }

36
src/main/java/com/alibaba/excel/cache/Ehcache2.java vendored

@ -0,0 +1,36 @@
package com.alibaba.excel.cache;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Default cache
*
* @author zhuangjiaju
*/
public class Ehcache2 implements Cache {
private static final Logger LOGGER = LoggerFactory.getLogger(Ehcache2.class);
int index = 0;
// private org.ehcache.Cache<Integer, String> cache;
private Map<Integer, String> cache = new HashMap<Integer, String>();
public Ehcache2() {}
@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() {}
}

123
src/main/java/com/alibaba/excel/cache/EhcacheFile.java vendored

@ -0,0 +1,123 @@
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 Cache {
private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheFile.class);
private static final int BATCH = 500;
int index = 0;
int expirekey = 0;
private org.ehcache.Cache<Integer, String> cache;
// private Map<Integer, String> cache = new HashMap<Integer, String>();
private Map<Integer, Map<Integer, String>> cacheMap = new HashMap<Integer, Map<Integer, String>>();
private TreeMap<Integer, Integer> expire = new TreeMap<Integer, Integer>();
private StringBuilder sb = new StringBuilder();
private Set<Integer> count = new HashSet<Integer>();
private int getCount = 1;
public EhcacheFile() {
File file = POITempFile.createCacheTmpFile();
PersistentCacheManager persistentCacheManager =
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(file))
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)))
.build(true);
this.cache = persistentCacheManager.getCache("cache", Integer.class, String.class);
}
@Override
public void put(Integer key, String value) {
sb.append(index++).append(":").append(value).append(",");
if (index != 0 && index % BATCH == 0) {
int key1 = index / BATCH;
cache.put(key1, sb.toString());
// LOGGER.info("put:key:{},{}", key1, sb.toString());
sb = new StringBuilder();
}
}
@Override
public String get(Integer key) {
int route = key / BATCH;
route++;
if (!cacheMap.containsKey(route)) {
// LOGGER.info("key:{},route:{},missing", key, route);
Map<Integer, String> map = new HashMap<Integer, String>(10000 / 3 * 4 + 1);
String s = cache.get(route);
String[] values = s.split(",");
for (String value : values) {
String[] vv = value.split(":");
map.put(Integer.valueOf(vv[0]), vv[1]);
}
cacheMap.put(route, map);
expire.put(expirekey++, route);
}
count.add(route);
if (getCount++ % 1000 == 0) {
Iterator<Map.Entry<Integer, Map<Integer, String>>> iterator = cacheMap.entrySet().iterator();
// LOGGER.info("size:{}", cacheMap.size());
while (iterator.hasNext()) {
Map.Entry<Integer, Map<Integer, String>> entry = iterator.next();
if (!count.contains(entry.getKey())) {
// LOGGER.info("route:{},remove", entry.getKey());
iterator.remove();
Iterator<Map.Entry<Integer, Integer>> ex = expire.entrySet().iterator();
while (ex.hasNext()) {
Map.Entry<Integer, Integer> e = ex.next();
if (e.getValue().equals(entry.getKey())) {
ex.remove();
break;
}
}
}
}
count.clear();
}
if (expire.size() > 10) {
int value1 = expire.firstEntry().getValue();
int key1 = expire.firstEntry().getKey();
cacheMap.remove(value1);
expire.remove(key1);
}
return cacheMap.get(route).get(key);
}
@Override
public void finish() {
if (StringUtils.isEmpty(sb.toString())) {
return;
}
int key1 = index / BATCH;
if (index % BATCH != 0) {
key1++;
}
cache.put(key1, sb.toString());
// LOGGER.info("put:key:{},{}", key1 + 1, sb.toString());
}
}

123
src/main/java/com/alibaba/excel/cache/EhcacheMix.java vendored

@ -0,0 +1,123 @@
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 Cache {
private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheMix.class);
private static final int BATCH = 500;
int index = 0;
int expirekey = 0;
private org.ehcache.Cache<Integer, String> cache;
private Map<Integer, Map<Integer, String>> cacheMap = new HashMap<Integer, Map<Integer, String>>();
private TreeMap<Integer, Integer> expire = new TreeMap<Integer, Integer>();
private StringBuilder sb = new StringBuilder();
private Set<Integer> count = new HashSet<Integer>();
private int getCount = 1;
private int countRead = 0;
public EhcacheMix() {
File file = POITempFile.createCacheTmpFile();
PersistentCacheManager persistentCacheManager = CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence(file))
.withCache("cache", CacheConfigurationBuilder.newCacheConfigurationBuilder(Integer.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(20, MemoryUnit.MB).disk(2, MemoryUnit.GB, false)))
.build(true);
this.cache = persistentCacheManager.getCache("cache", Integer.class, String.class);
}
@Override
public void put(Integer key, String value) {
sb.append(index++).append(":").append(value).append(",");
if (index != 0 && index % BATCH == 0) {
int key1 = index / BATCH;
cache.put(key1, sb.toString());
// LOGGER.info("put:key:{},{}", key1, sb.toString());
sb = new StringBuilder();
}
}
@Override
public String get(Integer key) {
int route = key / BATCH;
route++;
if (!cacheMap.containsKey(route)) {
Map<Integer, String> map = new HashMap<Integer, String>(10000 / 3 * 4 + 1);
String s = cache.get(route);
String[] values = s.split(",");
for (String value : values) {
String[] vv = value.split(":");
map.put(Integer.valueOf(vv[0]), vv[1]);
}
cacheMap.put(route, map);
expire.put(expirekey++, route);
}
count.add(route);
if (getCount++ % 1000 == 0) {
Iterator<Map.Entry<Integer, Map<Integer, String>>> iterator = cacheMap.entrySet().iterator();
// LOGGER.info("size:{}", cacheMap.size());
while (iterator.hasNext()) {
Map.Entry<Integer, Map<Integer, String>> entry = iterator.next();
if (!count.contains(entry.getKey())) {
// LOGGER.info("route:{},remove", entry.getKey());
iterator.remove();
Iterator<Map.Entry<Integer, Integer>> ex = expire.entrySet().iterator();
while (ex.hasNext()) {
Map.Entry<Integer, Integer> e = ex.next();
if (e.getValue().equals(entry.getKey())) {
ex.remove();
break;
}
}
}
}
count.clear();
}
if (expire.size() > 10) {
int value1 = expire.firstEntry().getValue();
int key1 = expire.firstEntry().getKey();
cacheMap.remove(value1);
expire.remove(key1);
}
return cacheMap.get(route).get(key);
}
@Override
public void finish() {
if (StringUtils.isEmpty(sb.toString())) {
return;
}
int key1 = index / BATCH;
if (index % BATCH != 0) {
key1++;
}
cache.put(key1, sb.toString());
// LOGGER.info("put:key:{},{}", key1 + 1, sb.toString());
}
}

2
src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java

@ -74,7 +74,6 @@ public class AnalysisContextImpl implements AnalysisContext {
this.converterRegistryCenter = converterRegistryCenter; this.converterRegistryCenter = converterRegistryCenter;
} }
@Override @Override
public void setCurrentSheet(Sheet currentSheet) { public void setCurrentSheet(Sheet currentSheet) {
cleanCurrentSheet(); cleanCurrentSheet();
@ -85,6 +84,7 @@ public class AnalysisContextImpl implements AnalysisContext {
} }
} }
private void cleanCurrentSheet() { private void cleanCurrentSheet() {
this.currentSheet = null; this.currentSheet = null;
this.excelHeadProperty = null; this.excelHeadProperty = null;

23
src/main/java/com/alibaba/excel/event/AnalysisEventListener.java

@ -3,24 +3,31 @@ package com.alibaba.excel.event;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
/** /**
* * Receives the return of each piece of data parsed
* *
* @author jipengfei * @author jipengfei
*/ */
public abstract class AnalysisEventListener<T> { public abstract class AnalysisEventListener<T> implements ReadListener<T> {
/** /**
* when analysis one row trigger invoke function * All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the
* entire read will terminate.
* *
* @param object one row value * @param exception
* @param context analysis context * @param context
*/ */
public abstract void invoke(T object, AnalysisContext context); @Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
throw exception;
}
/** /**
* if have something to do after all analysis * Verify that there is another piece of data.You can stop the read by returning false
* *
* @param context * @param context
* @return
*/ */
public abstract void doAfterAllAnalysed(AnalysisContext context); public boolean hasNext(AnalysisContext context) {
return true;
}
} }

2
src/main/java/com/alibaba/excel/event/Handler.java

@ -1,7 +1,7 @@
package com.alibaba.excel.event; package com.alibaba.excel.event;
/** /**
* intercepts handle some business logic * Intercepts handle some business logic
* *
* @author zhuangjiaju * @author zhuangjiaju
**/ **/

8
src/main/java/com/alibaba/excel/event/Listener.java

@ -0,0 +1,8 @@
package com.alibaba.excel.event;
/**
* Interface to listen for processing results
*
* @author zhuangjiaju
*/
public interface Listener {}

37
src/main/java/com/alibaba/excel/event/ReadListener.java

@ -0,0 +1,37 @@
package com.alibaba.excel.event;
import com.alibaba.excel.context.AnalysisContext;
/**
* Interface to listen for read results
*
* @author zhuangjiaju
*/
public interface ReadListener<T> extends Listener {
/**
* All listeners receive this method when any one Listener does an error report. If an exception is thrown here, the
* * entire read will terminate.
*
* @param exception
* @param context
* @throws Exception
*/
void onException(Exception exception, AnalysisContext context) throws Exception;
/**
* when analysis one row trigger invoke function
*
* @param object
* one row value
* @param context
* analysis context
*/
void invoke(T object, AnalysisContext context);
/**
* if have something to do after all analysis
*
* @param context
*/
void doAfterAllAnalysed(AnalysisContext context);
}

13
src/main/java/com/alibaba/excel/metadata/BasicParameter.java

@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.ReadListener;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
/** /**
@ -46,6 +47,10 @@ public class BasicParameter {
* Custom type handler override the default * Custom type handler override the default
*/ */
private List<WriteHandler> customWriteHandlerList = new ArrayList<WriteHandler>(); private List<WriteHandler> customWriteHandlerList = new ArrayList<WriteHandler>();
/**
* Custom type listener run after default
*/
private List<ReadListener> customReadListenerList = new ArrayList<ReadListener>();
public Integer getReadHeadRowNumber() { public Integer getReadHeadRowNumber() {
return readHeadRowNumber; return readHeadRowNumber;
@ -102,4 +107,12 @@ public class BasicParameter {
public void setCustomWriteHandlerList(List<WriteHandler> customWriteHandlerList) { public void setCustomWriteHandlerList(List<WriteHandler> customWriteHandlerList) {
this.customWriteHandlerList = customWriteHandlerList; this.customWriteHandlerList = customWriteHandlerList;
} }
public List<ReadListener> getCustomReadListenerList() {
return customReadListenerList;
}
public void setCustomReadListenerList(List<ReadListener> customReadListenerList) {
this.customReadListenerList = customReadListenerList;
}
} }

35
src/main/java/com/alibaba/excel/metadata/Workbook.java

@ -1,5 +1,6 @@
package com.alibaba.excel.metadata; package com.alibaba.excel.metadata;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -16,14 +17,26 @@ public class Workbook extends BasicParameter {
* Excel type * Excel type
*/ */
private ExcelTypeEnum excelType; private ExcelTypeEnum excelType;
/** /**
* Final output stream * Read final output stream
*/ */
private OutputStream outputStream; private OutputStream outputStream;
/** /**
* Template input stream * <li>write: Template input stream
* <li>read: Read InputStream
* <p>
* If 'inputStream' and 'file' all not empty,file first
*/
private InputStream inputStream;
/**
* <li>write: Template file
* <li>read: Read file
* <p>
* If 'inputStream' and 'file' all not empty,file first
*/ */
private InputStream templateInputStream; private File file;
/** /**
* Default true * Default true
*/ */
@ -60,12 +73,20 @@ public class Workbook extends BasicParameter {
this.outputStream = outputStream; this.outputStream = outputStream;
} }
public InputStream getTemplateInputStream() { public InputStream getInputStream() {
return templateInputStream; return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public File getFile() {
return file;
} }
public void setTemplateInputStream(InputStream templateInputStream) { public void setFile(File file) {
this.templateInputStream = templateInputStream; this.file = file;
} }
public com.alibaba.excel.event.WriteHandler getWriteHandler() { public com.alibaba.excel.event.WriteHandler getWriteHandler() {

29
src/main/java/com/alibaba/excel/support/ExcelTypeEnum.java

@ -27,20 +27,21 @@ public enum ExcelTypeEnum {
} }
public static ExcelTypeEnum valueOf(InputStream inputStream){ public static ExcelTypeEnum valueOf(InputStream inputStream){
try {
if (!inputStream.markSupported()) {
return null;
}
FileMagic fileMagic = FileMagic.valueOf(inputStream);
if(FileMagic.OLE2.equals(fileMagic)){
return XLS;
}
if(FileMagic.OOXML.equals(fileMagic)){
return XLSX; return XLSX;
} // try {
return null; // if (!inputStream.markSupported()) {
} catch (IOException e) { // return null;
throw new RuntimeException(e); // }
} // FileMagic fileMagic = FileMagic.valueOf(inputStream);
// if(FileMagic.OLE2.equals(fileMagic)){
// return XLS;
// }
// if(FileMagic.OOXML.equals(fileMagic)){
// return XLSX;
// }
// return null;
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
} }
} }

13
src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java

@ -1,12 +1,9 @@
package com.alibaba.easyexcel.test.read.large; package com.alibaba.easyexcel.test.read.large;
import java.io.InputStream;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
@ -23,11 +20,15 @@ public class LargeData07Test {
public void large() throws Exception { public void large() throws Exception {
LOGGER.info("start"); LOGGER.info("start");
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
InputStream inputStream = FileUtil.readFile("large/large07.xlsx"); // InputStream inputStream = FileUtil.readFile("large/large07.xlsx");
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, new LargeDataListener()); ExcelReader excelReader = EasyExcelFactory.getReader(null, new LargeDataListener());
excelReader.read(new Sheet(1, 1)); excelReader.read(new Sheet(1, 1));
inputStream.close(); // inputStream.close();
LOGGER.info("time:{}", System.currentTimeMillis() - start); LOGGER.info("time:{}", System.currentTimeMillis() - start);
} }
@Test
public void hello() throws Exception {
LOGGER.info("start");
}
} }

Loading…
Cancel
Save