diff --git a/src/main/java/com/alibaba/excel/EasyExcelFactory.java b/src/main/java/com/alibaba/excel/EasyExcelFactory.java index 97a52c4a..16a3d3b8 100644 --- a/src/main/java/com/alibaba/excel/EasyExcelFactory.java +++ b/src/main/java/com/alibaba/excel/EasyExcelFactory.java @@ -1,7 +1,9 @@ package com.alibaba.excel; +import java.io.File; import java.io.InputStream; import java.io.OutputStream; +import java.net.URI; import java.util.ArrayList; import java.util.List; @@ -11,6 +13,7 @@ import com.alibaba.excel.event.WriteHandler; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.read.builder.ExcelReaderBuilder; import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder; +import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; @@ -19,6 +22,20 @@ import com.alibaba.excel.write.builder.ExcelWriterTableBuilder; /** * Reader and writer factory class * + *

Quick start

+ *

Read

+ *

Sample1

+ * + *

Sample2

+ * + *

Write

+ * + *

Sample1

+ * + *

Sample2

+ * + * + * * @author jipengfei */ public class EasyExcelFactory { @@ -31,7 +48,7 @@ public class EasyExcelFactory { * @param sheet * read sheet. * @return analysis result. - * @deprecated please use 'EasyExcelFactory.read().file(in).sheet(sheetNo).doReadSync();' + * @deprecated please use 'EasyExcelFactory.read(in).sheet(sheetNo).doReadSync();' */ @Deprecated public static List read(InputStream in, Sheet sheet) { @@ -57,7 +74,7 @@ public class EasyExcelFactory { * read sheet. * @param listener * Callback method after each row is parsed. - * @deprecated please use 'EasyExcelFactory.read().registerReadListener(listener).file(in).sheet(sheetNo).doRead().finish();' + * @deprecated please use 'EasyExcelFactory.read(in,head,listener).sheet(sheetNo).doRead().finish();' */ @Deprecated public static void readBySax(InputStream in, Sheet sheet, AnalysisEventListener listener) { @@ -153,23 +170,406 @@ public class EasyExcelFactory { .registerWriteHandler(handler).autoCloseStream(Boolean.FALSE).convertAllFiled(Boolean.FALSE).build(); } + /** + * Build excel the write + * + * @return + */ public static ExcelWriterBuilder write() { return new ExcelWriterBuilder(); } + /** + * Build excel the write + * + * @param file + * @return + */ + public static ExcelWriterBuilder write(File file) { + return write(file, null); + } + + /** + * Build excel the write + * + * @param file + * @param head + * @return + */ + public static ExcelWriterBuilder write(File file, Class head) { + ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder(); + excelWriterBuilder.file(file); + if (head != null) { + excelWriterBuilder.head(head); + } + return excelWriterBuilder; + } + + /** + * Build excel the write + * + * @param pathName + * @return + */ + public static ExcelWriterBuilder write(String pathName) { + return write(pathName, null); + } + + /** + * Build excel the write + * + * @param pathName + * @param head + * @return + */ + public static ExcelWriterBuilder write(String pathName, Class head) { + ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder(); + excelWriterBuilder.file(pathName); + if (head != null) { + excelWriterBuilder.head(head); + } + return excelWriterBuilder; + } + + /** + * Build excel the write + * + * @param outputStream + * @return + */ + public static ExcelWriterBuilder write(OutputStream outputStream) { + return write(outputStream, null); + } + + /** + * Build excel the write + * + * @param outputStream + * @param head + * @return + */ + public static ExcelWriterBuilder write(OutputStream outputStream, Class head) { + ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder(); + excelWriterBuilder.file(outputStream); + if (head != null) { + excelWriterBuilder.head(head); + } + return excelWriterBuilder; + } + + /** + * Build excel the write + * + * @param uri + * @return + */ + public static ExcelWriterBuilder write(URI uri) { + return write(uri, null); + } + + /** + * Build excel the write + * + * @param uri + * @param head + * @return + */ + public static ExcelWriterBuilder write(URI uri, Class head) { + ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder(); + excelWriterBuilder.file(uri); + if (head != null) { + excelWriterBuilder.head(head); + } + return excelWriterBuilder; + } + + /** + * Build excel the 'writerSheet' + * + * @return + */ public static ExcelWriterSheetBuilder writerSheet() { + return writerSheet(null, null); + } + + /** + * Build excel the 'writerSheet' + * + * @param sheetNo + * @return + */ + public static ExcelWriterSheetBuilder writerSheet(Integer sheetNo) { + return writerSheet(sheetNo, null); + } + + /** + * Build excel the 'writerSheet' + * + * @param sheetName + * @return + */ + public static ExcelWriterSheetBuilder writerSheet(String sheetName) { + return writerSheet(null, sheetName); + } + + /** + * Build excel the 'writerSheet' + * + * @param sheetNo + * @param sheetName + * @return + */ + public static ExcelWriterSheetBuilder writerSheet(Integer sheetNo, String sheetName) { + ExcelWriterSheetBuilder excelWriterSheetBuilder = new ExcelWriterSheetBuilder(); + if (sheetNo != null) { + excelWriterSheetBuilder.sheetNo(sheetNo); + } + if (sheetName != null) { + excelWriterSheetBuilder.sheetName(sheetName); + } return new ExcelWriterSheetBuilder(); } + /** + * Build excel the 'writerTable' + * + * @return + */ public static ExcelWriterTableBuilder writerTable() { - return new ExcelWriterTableBuilder(); + return writerTable(null); } + /** + * Build excel the 'writerTable' + * + * @param tableNo + * @return + */ + public static ExcelWriterTableBuilder writerTable(Integer tableNo) { + ExcelWriterTableBuilder excelWriterTableBuilder = new ExcelWriterTableBuilder(); + if (tableNo != null) { + excelWriterTableBuilder.tableNo(tableNo); + } + return excelWriterTableBuilder; + } + + /** + * Build excel the read + * + * @return + */ public static ExcelReaderBuilder read() { return new ExcelReaderBuilder(); } + /** + * Build excel the read + * + * @param file + * @return + */ + public static ExcelReaderBuilder read(File file) { + return read(file, null, null); + } + + /** + * Build excel the read + * + * @param file + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(File file, ReadListener readListener) { + return read(file, null, readListener); + } + + /** + * Build excel the read + * + * @param file + * @param head + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(File file, Class head, ReadListener readListener) { + ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder(); + excelReaderBuilder.file(file); + if (head != null) { + excelReaderBuilder.head(head); + } + if (readListener != null) { + excelReaderBuilder.registerReadListener(readListener); + } + return excelReaderBuilder; + } + + /** + * Build excel the read + * + * @param pathName + * @return + */ + public static ExcelReaderBuilder read(String pathName) { + return read(pathName, null, null); + } + + /** + * Build excel the read + * + * @param pathName + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(String pathName, ReadListener readListener) { + return read(pathName, null, readListener); + } + + /** + * Build excel the read + * + * @param pathName + * @param head + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(String pathName, Class head, ReadListener readListener) { + ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder(); + excelReaderBuilder.file(pathName); + if (head != null) { + excelReaderBuilder.head(head); + } + if (readListener != null) { + excelReaderBuilder.registerReadListener(readListener); + } + return excelReaderBuilder; + } + + /** + * Build excel the read + * + * @param inputStream + * @return + */ + public static ExcelReaderBuilder read(InputStream inputStream) { + return read(inputStream, null, null); + } + + /** + * Build excel the read + * + * @param inputStream + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(InputStream inputStream, ReadListener readListener) { + return read(inputStream, null, readListener); + } + + /** + * Build excel the read + * + * @param inputStream + * @param head + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(InputStream inputStream, Class head, ReadListener readListener) { + ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder(); + excelReaderBuilder.file(inputStream); + if (head != null) { + excelReaderBuilder.head(head); + } + if (readListener != null) { + excelReaderBuilder.registerReadListener(readListener); + } + return excelReaderBuilder; + } + + /** + * Build excel the read + * + * @param uri + * @return + */ + public static ExcelReaderBuilder read(URI uri) { + return read(uri, null, null); + } + + /** + * Build excel the read + * + * @param uri + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(URI uri, ReadListener readListener) { + return read(uri, null, readListener); + } + + /** + * Build excel the read + * + * @param uri + * @param head + * @param readListener + * @return + */ + public static ExcelReaderBuilder read(URI uri, Class head, ReadListener readListener) { + ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder(); + excelReaderBuilder.file(uri); + if (head != null) { + excelReaderBuilder.head(head); + } + if (readListener != null) { + excelReaderBuilder.registerReadListener(readListener); + } + return excelReaderBuilder; + } + + /** + * Build excel the 'readSheet' + * + * @return + */ public static ExcelReaderSheetBuilder readSheet() { + return readSheet(null, null); + } + + /** + * Build excel the 'readSheet' + * + * @param sheetNo + * @return + */ + public static ExcelReaderSheetBuilder readSheet(Integer sheetNo) { + return readSheet(sheetNo, null); + } + + /** + * Build excel the 'readSheet' + * + * @param sheetName + * @return + */ + public static ExcelReaderSheetBuilder readSheet(String sheetName) { + return readSheet(null, sheetName); + } + + /** + * Build excel the 'readSheet' + * + * @param sheetNo + * @param sheetName + * @return + */ + public static ExcelReaderSheetBuilder readSheet(Integer sheetNo, String sheetName) { + ExcelReaderSheetBuilder excelReaderSheetBuilder = new ExcelReaderSheetBuilder(); + if (sheetNo != null) { + excelReaderSheetBuilder.sheetNo(sheetNo); + } + if (sheetName != null) { + excelReaderSheetBuilder.sheetName(sheetName); + } return new ExcelReaderSheetBuilder(); } } diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 1709cd84..77cbd220 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -1,5 +1,7 @@ package com.alibaba.excel.analysis; +import java.io.IOException; + import com.alibaba.excel.analysis.v03.XlsSaxAnalyser; import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser; import com.alibaba.excel.context.AnalysisContext; @@ -7,7 +9,9 @@ import com.alibaba.excel.context.AnalysisContextImpl; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadWorkbook; +import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.support.ExcelTypeEnum; +import com.alibaba.excel.util.FileUtils; /** * @author jipengfei @@ -19,8 +23,16 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { private ExcelExecutor excelExecutor; public ExcelAnalyserImpl(ReadWorkbook readWorkbook) { - analysisContext = new AnalysisContextImpl(readWorkbook); - choiceExcelExecutor(); + try { + analysisContext = new AnalysisContextImpl(readWorkbook); + choiceExcelExecutor(); + } catch (RuntimeException e) { + finish(); + throw e; + } catch (Throwable e) { + finish(); + throw new ExcelAnalysisException(e); + } } private void choiceExcelExecutor() { @@ -47,13 +59,38 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { @Override public void analysis(ReadSheet readSheet) { - analysisContext.currentSheet(excelExecutor, readSheet); - excelExecutor.execute(); - analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext); + try { + analysisContext.currentSheet(excelExecutor, readSheet); + excelExecutor.execute(); + analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext); + } catch (RuntimeException e) { + finish(); + throw e; + } catch (Throwable e) { + finish(); + throw new ExcelAnalysisException(e); + } } @Override - public void finish() {} + public void finish() { + if (analysisContext == null || analysisContext.readWorkbookHolder() == null) { + return; + } + ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); + try { + readWorkbookHolder.getReadCache().destroy(); + if (analysisContext.readWorkbookHolder().getAutoCloseStream() + && readWorkbookHolder.getInputStream() != null) { + readWorkbookHolder.getInputStream().close(); + } + if (readWorkbookHolder.getTempFile() != null) { + FileUtils.delete(readWorkbookHolder.getTempFile()); + } + } catch (IOException e) { + throw new ExcelAnalysisException("Can not close IO", e); + } + } @Override public com.alibaba.excel.analysis.ExcelExecutor excelExecutor() { 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 63220928..a2f37305 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -112,12 +112,6 @@ public class XlsxSaxAnalyser implements ExcelExecutor { return sheetList; } - @Override - public void execute() { - parseXmlSource(sheetMap.get(analysisContext.readSheetHolder().getSheetNo()), - new XlsxRowHandler(analysisContext)); - } - private void parseXmlSource(InputStream inputStream, ContentHandler handler) { InputSource inputSource = new InputSource(inputStream); try { @@ -143,4 +137,10 @@ public class XlsxSaxAnalyser implements ExcelExecutor { } } + @Override + public void execute() { + parseXmlSource(sheetMap.get(analysisContext.readSheetHolder().getSheetNo()), + new XlsxRowHandler(analysisContext)); + } + } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java index a697570f..f4afdebc 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java @@ -11,10 +11,11 @@ import com.alibaba.excel.context.AnalysisContext; public class CountRowCellHandler implements XlsxCellHandler { private final AnalysisContext analysisContext; - + public CountRowCellHandler(AnalysisContext analysisContext) { this.analysisContext = analysisContext; } + @Override public boolean support(String name) { return DIMENSION.equals(name); @@ -25,12 +26,12 @@ public class CountRowCellHandler implements XlsxCellHandler { String d = attributes.getValue(DIMENSION_REF); String totalStr = d.substring(d.indexOf(":") + 1, d.length()); String c = totalStr.toUpperCase().replaceAll("[A-Z]", ""); - analysisContext.setTotalCount(Integer.parseInt(c)); + analysisContext.readSheetHolder().setTotal(Integer.parseInt(c)); } @Override public void endHandle(String name) { - + } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index e10d520d..7e8d2f39 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -17,6 +17,7 @@ import com.alibaba.excel.constant.ExcelXmlConstants; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.StringUtils; @@ -55,7 +56,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder if (nextRow > curRow) { curRow = nextRow; } - analysisContext.setCurrentRowNum(curRow); + analysisContext + .readRowHolder(new ReadRowHolder(curRow, analysisContext.readSheetHolder().getGlobalConfiguration())); curCol = PositionUtils.getCol(currentCellIndex); // t="s" ,it's means String @@ -68,7 +70,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder } // cell is formula if (CELL_FORMULA_TAG.equals(name)) { - currentCellData.setReadIsFormula(Boolean.TRUE); + currentCellData.setFormula(Boolean.TRUE); } } @@ -78,7 +80,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder ensureSize(); // Have to go "sharedStrings.xml" and get it if (currentCellData.getType() == CellDataTypeEnum.STRING) { - currentCellData.setStringValue(analysisContext.currentWorkbookHolder().getReadCache() + currentCellData.setStringValue(analysisContext.readWorkbookHolder().getReadCache() .get(Integer.valueOf(currentCellData.getStringValue()))); } curRowContent[curCol] = currentCellData; @@ -108,7 +110,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder return; } if (CELL_FORMULA_TAG.equals(currentTag)) { - currentCellData.setReadFormula(currentCellValue); + currentCellData.setFormulaValue(currentCellValue); return; } CellDataTypeEnum oldType = currentCellData.getType(); diff --git a/src/main/java/com/alibaba/excel/cache/Ehcache.java b/src/main/java/com/alibaba/excel/cache/Ehcache.java index 375b499f..cd844eb4 100644 --- a/src/main/java/com/alibaba/excel/cache/Ehcache.java +++ b/src/main/java/com/alibaba/excel/cache/Ehcache.java @@ -31,8 +31,8 @@ public class Ehcache implements ReadCache { private static final Logger LOGGER = LoggerFactory.getLogger(Ehcache.class); - private static final int BATCH_COUNT = 500; - private static final int CHECK_INTERVAL = 1000; + private static final int BATCH_COUNT = 1000; + private static final int CHECK_INTERVAL = 500; private static final int MAX_CACHE_ACTIVATE = 10; private static final String CACHE = "cache"; @@ -43,6 +43,12 @@ public class Ehcache implements ReadCache { private static final String ESCAPED_KEY_VALUE_SEPARATOR = "&kv;"; private static final String ESCAPED_SPECIAL_SEPARATOR = "&s;"; + private static final int DEBUG_WRITE_SIZE = 100 * 10000; + private static final int DEBUG_CACHE_MISS_SIZE = 1000; + + /** + * Key index + */ private int index = 0; private StringBuilder data = new StringBuilder(); private CacheManager cacheManager; @@ -76,10 +82,10 @@ public class Ehcache implements ReadCache { @Override public void init(AnalysisContext analysisContext) { - File readTempFile = analysisContext.currentWorkbookHolder().getReadTempFile(); + File readTempFile = analysisContext.readWorkbookHolder().getTempFile(); if (readTempFile == null) { readTempFile = FileUtils.createCacheTmpFile(); - analysisContext.currentWorkbookHolder().setReadTempFile(readTempFile); + analysisContext.readWorkbookHolder().setTempFile(readTempFile); } File cacheFile = new File(readTempFile.getPath(), UUID.randomUUID().toString()); PersistentCacheManager persistentCacheManager = @@ -99,6 +105,11 @@ public class Ehcache implements ReadCache { data = new StringBuilder(); } index++; + if (LOGGER.isDebugEnabled()) { + if (index % DEBUG_WRITE_SIZE == 0) { + LOGGER.debug("Already put :{}", index); + } + } } private String escape(String str) { @@ -130,9 +141,9 @@ public class Ehcache implements ReadCache { int route = key / BATCH_COUNT; if (cacheMap.containsKey(route)) { lastCheckIntervalUsedSet.add(route); - countList.add(route); + String value = cacheMap.get(route).get(key); checkClear(); - return cacheMap.get(route).get(key); + return value; } Map tempCacheMap = new HashMap(BATCH_COUNT / 3 * 4 + 1); String batchData = cache.get(route); @@ -141,13 +152,17 @@ public class Ehcache implements ReadCache { String[] keyValue = dataString.split(KEY_VALUE_SEPARATOR); tempCacheMap.put(Integer.valueOf(keyValue[0]), unescape(keyValue[1])); } + countList.add(route); cacheMap.put(route, tempCacheMap); if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Cache misses count:{}", cacheMiss++); + if (cacheMiss++ % DEBUG_CACHE_MISS_SIZE == 0) { + LOGGER.debug("Cache misses count:{}", cacheMiss); + } } lastCheckIntervalUsedSet.add(route); + String value = tempCacheMap.get(key); checkClear(); - return tempCacheMap.get(key); + return value; } private void checkClear() { @@ -167,6 +182,9 @@ public class Ehcache implements ReadCache { } // Last 'CHECK_INTERVAL' not use iterator.remove(); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Cache remove because {} times unused.", CHECK_INTERVAL); + } Iterator countIterator = countList.iterator(); while (countIterator.hasNext()) { Integer route = countIterator.next(); diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContext.java b/src/main/java/com/alibaba/excel/context/AnalysisContext.java index e3a987bc..bcd71e63 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContext.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContext.java @@ -1,11 +1,16 @@ package com.alibaba.excel.context; +import java.io.InputStream; + import com.alibaba.excel.analysis.ExcelExecutor; +import com.alibaba.excel.event.AnalysisEventListener; +import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.holder.ReadHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; +import com.alibaba.excel.support.ExcelTypeEnum; /** * @@ -61,4 +66,67 @@ public interface AnalysisContext { * Custom attribute */ Object getCustom(); + + /** + * get current sheet + * + * @return current analysis sheet + * @deprecated please use {@link #readSheetHolder()} + */ + @Deprecated + Sheet getCurrentSheet(); + + /** + * + * get excel type + * + * @return excel type + * @deprecated please use {@link #readWorkbookHolder()} + */ + @Deprecated + ExcelTypeEnum getExcelType(); + + /** + * get in io + * + * @return file io + * @deprecated please use {@link #readWorkbookHolder()} + */ + @Deprecated + InputStream getInputStream(); + + /** + * get current row + * + * @return + * @deprecated please use {@link #readRowHolder()} + */ + @Deprecated + Integer getCurrentRowNum(); + + /** + * get total row ,Data may be inaccurate + * + * @return + * @deprecated please use {@link #readRowHolder()} + */ + @Deprecated + Integer getTotalCount(); + + /** + * get current result + * + * @return get current result + * @deprecated please use {@link #readRowHolder()} + */ + @Deprecated + Object getCurrentRowAnalysisResult(); + + /** + * Interrupt execution + * + * @deprecated please use {@link AnalysisEventListener#hasNext(AnalysisContext)} + */ + @Deprecated + void interrupt(); } diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index 8769d116..b464b5f2 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -1,16 +1,20 @@ package com.alibaba.excel.context; +import java.io.InputStream; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.excel.analysis.ExcelExecutor; import com.alibaba.excel.exception.ExcelAnalysisException; +import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.holder.ReadHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; +import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.util.StringUtils; /** @@ -119,4 +123,44 @@ public class AnalysisContextImpl implements AnalysisContext { public Object getCustom() { return readWorkbookHolder.getCustomObject(); } + + @Override + public Sheet getCurrentSheet() { + Sheet sheet = new Sheet(readSheetHolder.getSheetNo() + 1); + sheet.setSheetName(readSheetHolder.getSheetName()); + sheet.setHead(readSheetHolder.getHead()); + sheet.setClazz(readSheetHolder.getClazz()); + sheet.setHeadLineMun(readSheetHolder.getHeadRowNumber()); + return sheet; + } + + @Override + public ExcelTypeEnum getExcelType() { + return readWorkbookHolder.getExcelType(); + } + + @Override + public InputStream getInputStream() { + return readWorkbookHolder.getInputStream(); + } + + @Override + public Integer getCurrentRowNum() { + return readRowHolder.getRowIndex(); + } + + @Override + public Integer getTotalCount() { + return readSheetHolder.getTotal(); + } + + @Override + public Object getCurrentRowAnalysisResult() { + return readRowHolder.getCurrentRowAnalysisResult(); + } + + @Override + public void interrupt() { + throw new ExcelAnalysisException("interrupt error"); + } } diff --git a/src/main/java/com/alibaba/excel/context/WriteContext.java b/src/main/java/com/alibaba/excel/context/WriteContext.java index 01139fd3..86802779 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContext.java +++ b/src/main/java/com/alibaba/excel/context/WriteContext.java @@ -1,5 +1,10 @@ package com.alibaba.excel.context; +import java.io.OutputStream; + +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.holder.WriteHolder; @@ -49,7 +54,8 @@ public interface WriteContext { WriteTableHolder writeTableHolder(); /** - * Configuration of currently operated cell. May be 'writeSheetHolder' or 'writeTableHolder' or 'writeWorkbookHolder' + * Configuration of currently operated cell. May be 'writeSheetHolder' or 'writeTableHolder' or + * 'writeWorkbookHolder' * * @return */ @@ -59,4 +65,37 @@ public interface WriteContext { * close */ void finish(); + + /** + * + * @return + * @deprecated please us e{@link #writeSheetHolder()} + */ + @Deprecated + Sheet getCurrentSheet(); + + /** + * + * @return + * @deprecated please us e{@link #writeSheetHolder()} + */ + @Deprecated + boolean needHead(); + + /** + * + * @return + * @deprecated please us e{@link #writeWorkbookHolder()} ()} + */ + @Deprecated + OutputStream getOutputStream(); + + /** + * + * @return + * @deprecated please us e{@link #writeWorkbookHolder()} ()} + */ + @Deprecated + Workbook getWorkbook(); + } diff --git a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java index d098443f..db7c0803 100644 --- a/src/main/java/com/alibaba/excel/context/WriteContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/WriteContextImpl.java @@ -1,12 +1,14 @@ package com.alibaba.excel.context; import java.io.IOException; +import java.io.OutputStream; import java.util.List; import java.util.Map; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -337,6 +339,9 @@ public class WriteContextImpl implements WriteContext { @Override public void finish() { + if (writeWorkbookHolder == null) { + return; + } try { writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); writeWorkbookHolder.getWorkbook().close(); @@ -355,4 +360,24 @@ public class WriteContextImpl implements WriteContext { LOGGER.debug("Finished write."); } } + + @Override + public Sheet getCurrentSheet() { + return writeSheetHolder.getSheet(); + } + + @Override + public boolean needHead() { + return writeSheetHolder.needHead(); + } + + @Override + public OutputStream getOutputStream() { + return writeWorkbookHolder.getOutputStream(); + } + + @Override + public Workbook getWorkbook() { + return writeWorkbookHolder.getWorkbook(); + } } diff --git a/src/main/java/com/alibaba/excel/converters/ConverterKey.java b/src/main/java/com/alibaba/excel/converters/ConverterKey.java deleted file mode 100644 index cbbfedc6..00000000 --- a/src/main/java/com/alibaba/excel/converters/ConverterKey.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.alibaba.excel.converters; - -import com.alibaba.excel.enums.CellDataTypeEnum; - -/** - * Converter unique key - * - * @author zhuangjiaju - */ -public class ConverterKey { - - private Class javaTypeKey; - private CellDataTypeEnum excelTypeKey; - - public ConverterKey(Class javaTypeKey, CellDataTypeEnum excelTypeKey) { - if (javaTypeKey == null || excelTypeKey == null) { - throw new IllegalArgumentException("All parameters can not be null"); - } - this.javaTypeKey = javaTypeKey; - this.excelTypeKey = excelTypeKey; - } - - public static ConverterKey buildConverterKey(Class javaTypeKey, CellDataTypeEnum excelTypeKey) { - return new ConverterKey(javaTypeKey, excelTypeKey); - } - - public static ConverterKey buildConverterKey(Converter converter) { - return buildConverterKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ConverterKey that = (ConverterKey)o; - if (javaTypeKey != null ? !javaTypeKey.equals(that.javaTypeKey) : that.javaTypeKey != null) { - return false; - } - return excelTypeKey == that.excelTypeKey; - } - - @Override - public int hashCode() { - int result = javaTypeKey != null ? javaTypeKey.hashCode() : 0; - result = 31 * result + (excelTypeKey != null ? excelTypeKey.hashCode() : 0); - return result; - } -} diff --git a/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java new file mode 100644 index 00000000..03ceb250 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java @@ -0,0 +1,18 @@ +package com.alibaba.excel.converters; + +import com.alibaba.excel.enums.CellDataTypeEnum; + +/** + * Converter unique key + * + * @author zhuangjiaju + */ +public class ConverterKeyBuild { + public static String buildKey(Class clazz) { + return clazz.getName(); + } + + public static String buildKey(Class clazz, CellDataTypeEnum cellDataTypeEnum) { + return clazz.getName() + "-" + cellDataTypeEnum.toString(); + } +} diff --git a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index 722ee9ba..8c6748e3 100644 --- a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java @@ -39,8 +39,8 @@ public class DefaultConverterLoader { * * @return */ - public static Map loadDefaultWriteConverter() { - Map converterMap = new HashMap(); + public static Map loadDefaultWriteConverter() { + Map converterMap = new HashMap(); putWriteConverter(converterMap, new BigDecimalNumberConverter()); putWriteConverter(converterMap, new BooleanBooleanConverter()); putWriteConverter(converterMap, new ByteNumberConverter()); @@ -54,8 +54,8 @@ public class DefaultConverterLoader { return converterMap; } - private static void putWriteConverter(Map converterMap, Converter converter) { - converterMap.put(converter.supportJavaTypeKey(), converter); + private static void putWriteConverter(Map converterMap, Converter converter) { + converterMap.put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter); } /** @@ -63,8 +63,8 @@ public class DefaultConverterLoader { * * @return */ - public static Map loadDefaultReadConverter() { - Map converterMap = new HashMap(); + public static Map loadDefaultReadConverter() { + Map converterMap = new HashMap(); putReadConverter(converterMap, new BigDecimalBooleanConverter()); putReadConverter(converterMap, new BigDecimalNumberConverter()); putReadConverter(converterMap, new BigDecimalStringConverter()); @@ -107,7 +107,8 @@ public class DefaultConverterLoader { return converterMap; } - private static void putReadConverter(Map converterMap, Converter converter) { - converterMap.put(ConverterKey.buildConverterKey(converter), converter); + private static void putReadConverter(Map converterMap, Converter converter) { + converterMap.put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()), + converter); } } diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java index 15dc5553..7d2edef5 100644 --- a/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java +++ b/src/main/java/com/alibaba/excel/metadata/AbstractHolder.java @@ -1,6 +1,9 @@ package com.alibaba.excel.metadata; import java.util.List; +import java.util.Map; + +import com.alibaba.excel.converters.Converter; /** * Write/read holder @@ -25,10 +28,24 @@ public abstract class AbstractHolder implements Holder { */ private GlobalConfiguration globalConfiguration; + /** + *
  • Read key: + *
  • Write key: + */ + private Map converterMap; + public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) { this.newInitialization = Boolean.TRUE; - this.head = basicParameter.getHead(); - this.clazz = basicParameter.getClazz(); + if (basicParameter.getHead() == null && prentAbstractHolder != null) { + this.head = prentAbstractHolder.getHead(); + } else { + this.head = basicParameter.getHead(); + } + if (basicParameter.getClazz() == null && prentAbstractHolder != null) { + this.clazz = prentAbstractHolder.getClazz(); + } else { + this.clazz = basicParameter.getClazz(); + } this.globalConfiguration = new GlobalConfiguration(); if (basicParameter.getAutoTrim() == null) { if (prentAbstractHolder == null) { @@ -73,6 +90,19 @@ public abstract class AbstractHolder implements Holder { this.globalConfiguration = globalConfiguration; } + public Map getConverterMap() { + return converterMap; + } + + public void setConverterMap(Map converterMap) { + this.converterMap = converterMap; + } + + @Override + public Map converterMap() { + return getConverterMap(); + } + @Override public GlobalConfiguration globalConfiguration() { return getGlobalConfiguration(); diff --git a/src/main/java/com/alibaba/excel/metadata/Head.java b/src/main/java/com/alibaba/excel/metadata/Head.java index 7f50c361..af3269e3 100644 --- a/src/main/java/com/alibaba/excel/metadata/Head.java +++ b/src/main/java/com/alibaba/excel/metadata/Head.java @@ -42,15 +42,17 @@ public class Head { this.fieldName = fieldName; headNameList = new ArrayList(); headNameList.add(headName); + this.forceIndex = Boolean.FALSE; } - public Head(Integer columnIndex, String fieldName, List headNameList) { + public Head(Integer columnIndex, String fieldName, List headNameList, Boolean forceIndex) { this.columnIndex = columnIndex; this.fieldName = fieldName; if (headNameList == null) { headNameList = new ArrayList(); } this.headNameList = headNameList; + this.forceIndex = forceIndex; } public Integer getColumnIndex() { diff --git a/src/main/java/com/alibaba/excel/metadata/Holder.java b/src/main/java/com/alibaba/excel/metadata/Holder.java index 9aef31aa..15da0dca 100644 --- a/src/main/java/com/alibaba/excel/metadata/Holder.java +++ b/src/main/java/com/alibaba/excel/metadata/Holder.java @@ -1,5 +1,8 @@ package com.alibaba.excel.metadata; +import java.util.Map; + +import com.alibaba.excel.converters.Converter; import com.alibaba.excel.enums.HolderEnum; /** @@ -31,4 +34,11 @@ public interface Holder { * @return */ GlobalConfiguration globalConfiguration(); + + /** + * What converter does the currently operated cell need to execute + * + * @return + */ + Map converterMap(); } diff --git a/src/main/java/com/alibaba/excel/metadata/TableStyle.java b/src/main/java/com/alibaba/excel/metadata/TableStyle.java index dd512844..ef3b9eb3 100644 --- a/src/main/java/com/alibaba/excel/metadata/TableStyle.java +++ b/src/main/java/com/alibaba/excel/metadata/TableStyle.java @@ -2,6 +2,8 @@ package com.alibaba.excel.metadata; import org.apache.poi.ss.usermodel.IndexedColors; +import com.alibaba.excel.write.style.RowCellStyleStrategy; + /** * @author jipengfei * @deprecated please use {@link RowCellStyleStrategy} 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 d11ffdd0..f7af03b6 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -57,7 +57,7 @@ public class ExcelHeadProperty { headRowNumber = 0; if (head != null && !head.isEmpty()) { for (int i = 0; i < head.size(); i++) { - headMap.put(i, new Head(i, null, head.get(i))); + headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE)); contentPropertyMap.put(i, null); } headKind = HeadKindEnum.STRING; @@ -135,21 +135,21 @@ public class ExcelHeadProperty { int index = 0; for (Field field : defaultFieldList) { while (customFiledMap.containsKey(index)) { - initOneColumnProperty(index, customFiledMap.get(index)); + initOneColumnProperty(index, customFiledMap.get(index), Boolean.TRUE); customFiledMap.remove(index); index++; } - initOneColumnProperty(index, field); + initOneColumnProperty(index, field, Boolean.FALSE); index++; } for (Map.Entry entry : customFiledMap.entrySet()) { - initOneColumnProperty(index, entry.getValue()); + initOneColumnProperty(index, entry.getValue(), Boolean.FALSE); index++; } headKind = HeadKindEnum.CLASS; } - private void initOneColumnProperty(int index, Field field) { + private void initOneColumnProperty(int index, Field field, Boolean forceIndex) { ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); List tmpHeadList = new ArrayList(); if (excelProperty != null) { @@ -160,7 +160,7 @@ public class ExcelHeadProperty { if (tmpHeadList.isEmpty() || StringUtils.isEmpty(tmpHeadList.get(0))) { tmpHeadList.add(field.getName()); } - Head head = new Head(index, field.getName(), tmpHeadList); + Head head = new Head(index, field.getName(), tmpHeadList, forceIndex); ExcelContentProperty excelContentProperty = new ExcelContentProperty(); excelContentProperty.setHead(head); excelContentProperty.setField(field); diff --git a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java index e94548a7..c97395bd 100644 --- a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java +++ b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java @@ -6,6 +6,7 @@ import java.util.List; import com.alibaba.excel.ExcelReader; import com.alibaba.excel.converters.Converter; import com.alibaba.excel.event.SyncReadListener; +import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.metadata.ReadSheet; @@ -166,7 +167,7 @@ public class ExcelReaderSheetBuilder { */ public void finish() { if (excelReader == null) { - throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); + throw new ExcelAnalysisException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); } excelReader.finish(); } @@ -178,7 +179,7 @@ public class ExcelReaderSheetBuilder { */ public List doReadSync() { if (excelReader == null) { - throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); + throw new ExcelAnalysisException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); } SyncReadListener syncReadListener = new SyncReadListener(); registerReadListener(syncReadListener); 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 ce0d70ed..0603cafb 100644 --- a/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java +++ b/src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java @@ -7,7 +7,7 @@ import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.converters.ConverterKey; +import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener; @@ -67,7 +67,7 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener continue; } ExcelContentProperty excelContentProperty = contentPropertyMap.get(index); - Object value = convertValue(cellData, excelContentProperty.getField().getClass(), excelContentProperty, + Object value = convertValue(cellData, excelContentProperty.getField().getType(), excelContentProperty, currentReadHolder.converterMap()); if (value != null) { map.put(excelContentProperty.getField().getName(), value); @@ -78,8 +78,8 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener } private Object convertValue(CellData cellData, Class clazz, ExcelContentProperty contentProperty, - Map readConverterMap) { - Converter converter = readConverterMap.get(ConverterKey.buildConverterKey(clazz, cellData.getType())); + Map converterMap) { + Converter converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType())); if (converter == null) { throw new ExcelDataConvertException( "Converter not found, convert " + cellData.getType() + " to " + clazz.getName()); diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java index 7b902d73..257882a7 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java @@ -7,7 +7,8 @@ import java.util.Map; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.converters.ConverterKey; +import com.alibaba.excel.converters.ConverterKeyBuild; +import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.event.AnalysisEventListener; @@ -47,10 +48,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH * Read listener */ private List readListenerList; - /** - * Converter for workbook - */ - private Map converterMap; public AbstractReadHolder(ReadBasicParameter readBasicParameter, AbstractReadHolder parentAbstractReadHolder, Boolean convertAllFiled) { @@ -89,14 +86,16 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH } if (parentAbstractReadHolder == null) { - this.converterMap = new HashMap(); + setConverterMap(DefaultConverterLoader.loadDefaultReadConverter()); } else { - this.converterMap = new HashMap(parentAbstractReadHolder.getConverterMap()); + setConverterMap(new HashMap(parentAbstractReadHolder.getConverterMap())); } if (readBasicParameter.getCustomConverterList() != null && !readBasicParameter.getCustomConverterList().isEmpty()) { for (Converter converter : readBasicParameter.getCustomConverterList()) { - converterMap.put(ConverterKey.buildConverterKey(converter), converter); + getConverterMap().put( + ConverterKeyBuild.buildKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()), + converter); } } } @@ -110,13 +109,14 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) { List cellDataList = (List)event.getAnalysisResult(); ReadRowHolder readRowHolder = analysisContext.readRowHolder(); + readRowHolder.setCurrentRowAnalysisResult(cellDataList); if (readRowHolder.getRowIndex() >= analysisContext.readSheetHolder().getHeadRowNumber()) { - for (ReadListener readListener : readListenerList) { + for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) { try { readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext); } catch (Exception e) { - for (ReadListener readListenerException : readListenerList) { + for (ReadListener readListenerException : analysisContext.currentReadHolder().readListenerList()) { try { readListenerException.onException(e, analysisContext); } catch (Exception exception) { @@ -183,7 +183,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH List list = new ArrayList(); for (CellData cellData : data) { Converter converter = - readHolder.converterMap().get(ConverterKey.buildConverterKey(String.class, cellData.getType())); + readHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType())); if (converter == null) { throw new ExcelDataConvertException( "Converter not found, convert " + cellData.getType() + " to String"); @@ -205,14 +205,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH this.readListenerList = readListenerList; } - public Map getConverterMap() { - return converterMap; - } - - public void setConverterMap(Map converterMap) { - this.converterMap = converterMap; - } - public ExcelReadHeadProperty getExcelReadHeadProperty() { return excelReadHeadProperty; } @@ -234,11 +226,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH return getReadListenerList(); } - @Override - public Map converterMap() { - return getConverterMap(); - } - @Override public ExcelReadHeadProperty excelReadHeadProperty() { return getExcelReadHeadProperty(); diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java index 02bf22d3..d235afb5 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java @@ -1,10 +1,7 @@ package com.alibaba.excel.read.metadata.holder; import java.util.List; -import java.util.Map; -import com.alibaba.excel.converters.Converter; -import com.alibaba.excel.converters.ConverterKey; import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; @@ -23,13 +20,6 @@ public interface ReadHolder extends Holder { */ List readListenerList(); - /** - * What converter does the currently operated cell need to execute - * - * @return - */ - Map converterMap(); - /** * What 'ExcelReadHeadProperty' does the currently operated cell need to execute * diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java index a424b275..467c5a1f 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java @@ -1,27 +1,31 @@ package com.alibaba.excel.read.metadata.holder; -import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.metadata.GlobalConfiguration; -import com.alibaba.excel.metadata.Holder; /** * sheet holder * * @author zhuangjiaju */ -public class ReadRowHolder implements Holder { +public class ReadRowHolder { /** - * Some global variables + * Returns row index of a row in the sheet that contains this cell.Start form 0. */ - private GlobalConfiguration globalConfiguration; + private int rowIndex; + /** * The result of the previous listener */ private Object currentRowAnalysisResult; /** - * Returns row index of a row in the sheet that contains this cell.Start form 0. + * Some global variables */ - private int rowIndex; + private GlobalConfiguration globalConfiguration; + + public ReadRowHolder(int rowIndex, GlobalConfiguration globalConfiguration) { + this.rowIndex = rowIndex; + this.globalConfiguration = globalConfiguration; + } public GlobalConfiguration getGlobalConfiguration() { return globalConfiguration; @@ -46,19 +50,4 @@ public class ReadRowHolder implements Holder { public void setRowIndex(int rowIndex) { this.rowIndex = rowIndex; } - - @Override - public HolderEnum holderType() { - return HolderEnum.ROW; - } - - @Override - public boolean isNew() { - return true; - } - - @Override - public GlobalConfiguration globalConfiguration() { - return getGlobalConfiguration(); - } } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java index 8167ff52..236e42fb 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java @@ -32,7 +32,7 @@ public class ReadSheetHolder extends AbstractReadHolder { private Integer total; public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { - super(readSheet, null, readWorkbookHolder.getReadWorkbook().getConvertAllFiled()); + super(readSheet, readWorkbookHolder, readWorkbookHolder.getReadWorkbook().getConvertAllFiled()); this.readSheet = readSheet; this.parentReadWorkbookHolder = readWorkbookHolder; this.sheetNo = readSheet.getSheetNo(); diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java index 9f4f5127..0962edf7 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java @@ -74,11 +74,11 @@ public class ReadWorkbookHolder extends AbstractReadHolder { public ReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook, null, readWorkbook.getConvertAllFiled()); this.readWorkbook = readWorkbook; + this.inputStream = readWorkbook.getInputStream(); + this.file = readWorkbook.getFile(); if (file == null && inputStream == null) { throw new ExcelAnalysisException("File and inputStream must be a non-null."); } - this.inputStream = readWorkbook.getInputStream(); - this.file = readWorkbook.getFile(); if (readWorkbook.getMandatoryUseInputStream() == null) { this.mandatoryUseInputStream = Boolean.FALSE; } else { diff --git a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index 1f9815f9..5f5318f0 100644 --- a/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java @@ -13,7 +13,9 @@ import org.apache.poi.ss.util.CellRangeAddress; import com.alibaba.excel.context.WriteContext; import com.alibaba.excel.context.WriteContextImpl; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.exception.ExcelDataConvertException; +import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.property.ExcelContentProperty; @@ -38,9 +40,17 @@ public class ExcelBuilderImpl implements ExcelBuilder { private WriteContext context; public ExcelBuilderImpl(WriteWorkbook writeWorkbook) { - // Create temporary cache directory at initialization time to avoid POI concurrent write bugs - FileUtils.createPoiFilesDirectory(); - context = new WriteContextImpl(writeWorkbook); + try { + // Create temporary cache directory at initialization time to avoid POI concurrent write bugs + FileUtils.createPoiFilesDirectory(); + context = new WriteContextImpl(writeWorkbook); + } catch (RuntimeException e) { + finish(); + throw e; + } catch (Throwable e) { + finish(); + throw new ExcelGenerateException(e); + } } private void doAddContent(List data) { @@ -65,9 +75,18 @@ public class ExcelBuilderImpl implements ExcelBuilder { @Override public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) { - context.currentSheet(writeSheet); - context.currentTable(writeTable); - doAddContent(data); + try { + + context.currentSheet(writeSheet); + context.currentTable(writeTable); + doAddContent(data); + } catch (RuntimeException e) { + finish(); + throw e; + } catch (Throwable e) { + finish(); + throw new ExcelGenerateException(e); + } } @Override @@ -235,7 +254,7 @@ public class ExcelBuilderImpl implements ExcelBuilder { if (value == null) { return; } - Converter converter = currentWriteHolder.converterMap().get(clazz); + Converter converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz)); if (converter == null) { throw new ExcelDataConvertException( "Can not find 'Converter' support class " + clazz.getSimpleName() + "."); diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java index 067c0ed7..df3c67bb 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java @@ -149,7 +149,7 @@ public class ExcelWriterBuilder { return file(new File(outputPathName)); } - public ExcelWriterBuilder outputFile(URI outputUri) { + public ExcelWriterBuilder file(URI outputUri) { return file(new File(outputUri)); } diff --git a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java index fbf04bea..93ec0998 100644 --- a/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java +++ b/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java @@ -135,6 +135,13 @@ public class ExcelWriterSheetBuilder { return new ExcelWriterSheetBuilder(excelWriter); } + public void finish() { + if (excelWriter == null) { + throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method"); + } + excelWriter.finish(); + } + public ExcelWriterTableBuilder table() { return table(null); } 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 05a1522b..7d982b70 100644 --- a/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java +++ b/src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java @@ -5,9 +5,9 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.metadata.Head; -import com.alibaba.excel.write.metadata.holder.SheetHolder; -import com.alibaba.excel.write.metadata.holder.TableHolder; import com.alibaba.excel.write.handler.CellWriteHandler; +import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; +import com.alibaba.excel.write.metadata.holder.WriteTableHolder; /** * Merge strategy @@ -15,18 +15,19 @@ import com.alibaba.excel.write.handler.CellWriteHandler; * @author zhuangjiaju */ public abstract class AbstractMergeStrategy implements CellWriteHandler { - @Override - public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, - int relativeRowIndex, boolean isHead) {} + public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, + Head head, int relativeRowIndex, boolean isHead) { + + } @Override - public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, - int relativeRowIndex, boolean isHead) { + public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, + Head head, int relativeRowIndex, boolean isHead) { if (isHead) { return; } - merge(sheetHolder.getSheet(), cell, head, relativeRowIndex); + merge(writeSheetHolder.getSheet(), cell, head, relativeRowIndex); } /** diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java index 9c739ddd..42ba2b6f 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java @@ -12,6 +12,7 @@ import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.converters.ConverterKeyBuild; import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.event.NotRepeatExecutor; @@ -60,10 +61,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ * Write handler for workbook */ private Map, List> writeHandlerMap; - /** - * Converter for workbook - */ - private Map converterMap; public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder, Boolean convertAllFiled) { @@ -122,14 +119,14 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ // Set converterMap if (parentAbstractWriteHolder == null) { - this.converterMap = DefaultConverterLoader.loadDefaultWriteConverter(); + setConverterMap(DefaultConverterLoader.loadDefaultWriteConverter()); } else { - this.converterMap = new HashMap(parentAbstractWriteHolder.getConverterMap()); + setConverterMap(new HashMap(parentAbstractWriteHolder.getConverterMap())); } if (writeBasicParameter.getCustomConverterList() != null && !writeBasicParameter.getCustomConverterList().isEmpty()) { for (Converter converter : writeBasicParameter.getCustomConverterList()) { - converterMap.put(converter.getClass(), converter); + getConverterMap().put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter); } } } @@ -368,14 +365,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ this.writeHandlerMap = writeHandlerMap; } - public Map getConverterMap() { - return converterMap; - } - - public void setConverterMap(Map converterMap) { - this.converterMap = converterMap; - } - public ExcelWriteHeadProperty getExcelWriteHeadProperty() { return excelWriteHeadProperty; } @@ -402,11 +391,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ return getWriteHandlerMap(); } - @Override - public Map converterMap() { - return getConverterMap(); - } - @Override public boolean needHead() { return getNeedHead(); diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java index 1cf3c074..eba71df0 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java @@ -3,7 +3,6 @@ package com.alibaba.excel.write.metadata.holder; import java.util.List; import java.util.Map; -import com.alibaba.excel.converters.Converter; import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.property.ExcelWriteHeadProperty; @@ -27,13 +26,6 @@ public interface WriteHolder extends Holder { */ Map, List> writeHandlerMap(); - /** - * What converter does the currently operated cell need to execute - * - * @return - */ - Map converterMap(); - /** * Whether a header is required for the currently operated cell * diff --git a/src/test/java/com/alibaba/easyexcel/test/ReadTest.java b/src/test/java/com/alibaba/easyexcel/test/ReadTest.java index 06a2c2f8..0e46892f 100644 --- a/src/test/java/com/alibaba/easyexcel/test/ReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/ReadTest.java @@ -3,10 +3,11 @@ package com.alibaba.easyexcel.test; import com.alibaba.easyexcel.test.listen.ExcelListener; import com.alibaba.easyexcel.test.model.ReadModel; import com.alibaba.easyexcel.test.model.ReadModel2; -import com.alibaba.easyexcel.test.util.FileUtil; +import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelReader; -import com.alibaba.excel.write.metadata.Sheet; +import com.alibaba.excel.metadata.Sheet; + import org.junit.Test; import java.io.IOException; @@ -23,7 +24,7 @@ public class ReadTest { */ @Test public void simpleReadListStringV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); List data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); inputStream.close(); print(data); @@ -37,7 +38,7 @@ public class ReadTest { */ @Test public void simpleReadJavaModelV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); inputStream.close(); print(data); @@ -50,7 +51,7 @@ public class ReadTest { */ @Test public void saxReadListStringV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); ExcelListener excelListener = new ExcelListener(); EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1), excelListener); inputStream.close(); @@ -63,7 +64,7 @@ public class ReadTest { */ @Test public void saxReadJavaModelV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); ExcelListener excelListener = new ExcelListener(); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); inputStream.close(); @@ -76,7 +77,7 @@ public class ReadTest { */ @Test public void saxReadSheetsV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx"); ExcelListener excelListener = new ExcelListener(); ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); List sheets = excelReader.getSheets(); @@ -107,7 +108,7 @@ public class ReadTest { */ @Test public void simpleReadListStringV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); List data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); inputStream.close(); print(data); @@ -120,7 +121,7 @@ public class ReadTest { */ @Test public void simpleReadJavaModelV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); inputStream.close(); print(data); @@ -133,7 +134,7 @@ public class ReadTest { */ @Test public void saxReadListStringV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); ExcelListener excelListener = new ExcelListener(); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1), excelListener); inputStream.close(); @@ -146,7 +147,7 @@ public class ReadTest { */ @Test public void saxReadJavaModelV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); ExcelListener excelListener = new ExcelListener(); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); inputStream.close(); @@ -159,7 +160,7 @@ public class ReadTest { */ @Test public void saxReadSheetsV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls"); ExcelListener excelListener = new ExcelListener(); ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); List sheets = excelReader.getSheets(); diff --git a/src/test/java/com/alibaba/easyexcel/test/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/WriteTest.java index ca9d42e1..902e956b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/WriteTest.java @@ -2,11 +2,11 @@ package com.alibaba.easyexcel.test; import com.alibaba.easyexcel.test.listen.AfterExcelWriteHandlerImpl; import com.alibaba.easyexcel.test.model.WriteModel; -import com.alibaba.easyexcel.test.util.FileUtil; +import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.write.metadata.Sheet; -import com.alibaba.excel.write.metadata.Table; +import com.alibaba.excel.metadata.Sheet; +import com.alibaba.excel.metadata.Table; import com.alibaba.excel.support.ExcelTypeEnum; import org.junit.Test; @@ -64,7 +64,7 @@ public class WriteTest { @Test public void writeV2007WithTemplate() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("temp.xlsx"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("temp.xlsx"); OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true); //写第一个sheet, sheet1 数据全是List 无模型映射关系 @@ -108,7 +108,7 @@ public class WriteTest { @Test public void writeV2007WithTemplateAndHandler() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("temp.xlsx"); + InputStream inputStream = TestFileUtil.getResourcesFileInputStream("temp.xlsx"); OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); ExcelWriter writer = EasyExcelFactory.getWriterWithTempAndHandler(inputStream,out,ExcelTypeEnum.XLSX,true, new AfterExcelWriteHandlerImpl()); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java new file mode 100644 index 00000000..16a806f9 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java @@ -0,0 +1,60 @@ +package com.alibaba.easyexcel.test.core.large; + +import lombok.Data; + +/** + * @author zhuangjiaju + */ +@Data +public class LargeData { + + private String str1; + + private String str2; + + private String str3; + + private String str4; + + private String str5; + + private String str6; + + private String str7; + + private String str8; + + private String str9; + + private String str10; + + private String str11; + + private String str12; + + private String str13; + + private String str14; + + private String str15; + + private String str16; + + private String str17; + + private String str18; + + private String str19; + + private String str20; + + private String str21; + + private String str22; + + private String str23; + + private String str24; + + private String str25; +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java new file mode 100644 index 00000000..4ab38cb0 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java @@ -0,0 +1,34 @@ +package com.alibaba.easyexcel.test.core.large; + +import org.junit.Assert; +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 LargeDataListener extends AnalysisEventListener { + private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataListener.class); + private int count = 0; + + @Override + public void invoke(LargeData data, AnalysisContext context) { + if (count == 0) { + LOGGER.info("First row:{}", JSON.toJSONString(data)); + } + count++; + if (count % 100000 == 0) { + LOGGER.info("Already read:{}", count); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext context) { + LOGGER.info("Large row count:{}", count); + Assert.assertEquals(count, 464509); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java new file mode 100644 index 00000000..173eec75 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -0,0 +1,27 @@ +package com.alibaba.easyexcel.test.core.large; + +import java.io.File; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcelFactory; + +/** + * Large data test + * + * @author zhuangjiaju + */ +public class LargeDataTest { + private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataTest.class); + + @Test + public void read() { + long start = System.currentTimeMillis(); + EasyExcelFactory.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class, + new LargeDataListener()).headRowNumber(2).sheet().doRead().finish(); + LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java b/src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java new file mode 100644 index 00000000..4d0af588 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java @@ -0,0 +1,36 @@ +package com.alibaba.easyexcel.test.core.nohead; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import com.alibaba.easyexcel.test.core.order.OrderData; + +/** + * Order data test + * + * @author zhuangjiaju + */ +public class NoHeadData07Test { + + @Test + public void simple() { + // ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(TestFileUtil.createNewWriteFile("order07.xlsx")) + // .head(OrderData.class).build(); + // Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); + // writer.write(createData(10000 * 100), sheet); + // writer.finish(); + } + + private List createData(int count) { + List list = new ArrayList(); + for (int i = 0; i < count; i++) { + OrderData orderData = new OrderData(); + orderData.setIndex1("排序1:" + i); + orderData.setIndex10("排序10:" + i); + list.add(orderData); + } + return list; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java b/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java similarity index 92% rename from src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java rename to src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java index 29e814b8..657c74ba 100644 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java @@ -1,4 +1,4 @@ -package com.alibaba.easyexcel.test.wirte.order; +package com.alibaba.easyexcel.test.core.order; import com.alibaba.excel.annotation.ExcelProperty; diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java b/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java similarity index 50% rename from src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java rename to src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java index 105e0c6c..134242a7 100644 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java @@ -1,15 +1,10 @@ -package com.alibaba.easyexcel.test.wirte.order; +package com.alibaba.easyexcel.test.core.order; import java.util.ArrayList; import java.util.List; import org.junit.Test; -import com.alibaba.easyexcel.test.util.FileUtil; -import com.alibaba.excel.EasyExcelFactory; -import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.write.metadata.Sheet; - /** * Order data test * @@ -19,11 +14,11 @@ public class OrderData07Test { @Test public void simple() { - ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(FileUtil.createNewWriteFile("order07.xlsx")) - .head(OrderData.class).build(); - Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); - writer.write(createData(10000 * 100), sheet); - writer.finish(); + // ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(TestFileUtil.createNewWriteFile("order07.xlsx")) + // .head(OrderData.class).build(); + // Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); + // writer.write(createData(10000 * 100), sheet); + // writer.finish(); } private List createData(int count) { diff --git a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java similarity index 84% rename from src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData.java rename to src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java index 30cb2536..8e9303c8 100644 --- a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java @@ -1,4 +1,4 @@ -package com.alibaba.easyexcel.test.read.simple; +package com.alibaba.easyexcel.test.core.simple; import com.alibaba.excel.annotation.ExcelProperty; diff --git a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java similarity index 53% rename from src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleDataListener.java rename to src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java index 0e9a5be0..0d022cc0 100644 --- a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java @@ -1,8 +1,9 @@ -package com.alibaba.easyexcel.test.read.simple; +package com.alibaba.easyexcel.test.core.simple; import java.util.ArrayList; import java.util.List; +import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,19 +14,18 @@ import com.alibaba.fastjson.JSON; /** * @author zhuangjiaju */ -public class SimpleDataListener extends AnalysisEventListener { +public class SimpleDataListener extends AnalysisEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class); - - List list = new ArrayList(); + List list = new ArrayList(); @Override - public void invoke(Object object, AnalysisContext context) { - list.add(object); - LOGGER.info("data:{}", JSON.toJSONString(object)); + public void invoke(SimpleData data, AnalysisContext context) { + list.add(data); } @Override public void doAfterAllAnalysed(AnalysisContext context) { - assert list.size() == 10; + Assert.assertEquals(list.size(), 10); + LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java new file mode 100644 index 00000000..565d3b19 --- /dev/null +++ b/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java @@ -0,0 +1,22 @@ +package com.alibaba.easyexcel.test.core.simple; + +import java.io.File; + +import org.junit.Test; + +import com.alibaba.easyexcel.test.util.TestFileUtil; +import com.alibaba.excel.EasyExcelFactory; + +/** + * Simple data test + * + * @author zhuangjiaju + */ +public class SimpleDataTest { + + @Test + public void read() { + EasyExcelFactory.read(TestFileUtil.getPath() + "simple" + File.separator + "simple07Test.xlsx", + SimpleData.class, new SimpleDataListener()).sheet().doRead().finish(); + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/ehcache/EncacheTest.java b/src/test/java/com/alibaba/easyexcel/test/ehcache/EncacheTest.java deleted file mode 100644 index 09657112..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/ehcache/EncacheTest.java +++ /dev/null @@ -1,40 +0,0 @@ -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 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); - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java b/src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java deleted file mode 100644 index 812b1950..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.alibaba.easyexcel.test.read; - -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -import org.junit.Test; - -import com.alibaba.easyexcel.test.listen.ExcelListener; -import com.alibaba.easyexcel.test.model.ReadModel; -import com.alibaba.easyexcel.test.model.ReadModel2; -import com.alibaba.easyexcel.test.util.FileUtil; -import com.alibaba.excel.EasyExcelFactory; -import com.alibaba.excel.ExcelReader; -import com.alibaba.excel.write.metadata.Sheet; - -public class ReadTest { - - - /** - * 07版本excel读数据量少于1千行数据,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void simpleReadListStringV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); - List data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); - inputStream.close(); - print(data); - } - - - /** - * 07版本excel读数据量少于1千行数据自动转成javamodel,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void simpleReadJavaModelV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); - List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); - inputStream.close(); - print(data); - } - - /** - * 07版本excel读数据量大于1千行,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void saxReadListStringV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); - ExcelListener excelListener = new ExcelListener(); - EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1), excelListener); - inputStream.close(); - - } - /** - * 07版本excel读数据量大于1千行,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void saxReadJavaModelV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); - ExcelListener excelListener = new ExcelListener(); - EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); - inputStream.close(); - } - - /** - * 07版本excel读取sheet - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void saxReadSheetsV2007() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); - ExcelListener excelListener = new ExcelListener(); - ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); - List sheets = excelReader.getSheets(); - System.out.println("llll****"+sheets); - System.out.println(); - for (Sheet sheet:sheets) { - if(sheet.getSheetNo() ==1) { - excelReader.read(sheet); - }else if(sheet.getSheetNo() ==2){ - sheet.setHeadLineMun(1); - sheet.setClazz(ReadModel.class); - excelReader.read(sheet); - }else if(sheet.getSheetNo() ==3){ - sheet.setHeadLineMun(1); - sheet.setClazz(ReadModel2.class); - excelReader.read(sheet); - } - } - inputStream.close(); - } - - - - /** - * 03版本excel读数据量少于1千行数据,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void simpleReadListStringV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); - List data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); - inputStream.close(); - print(data); - } - - /** - * 03版本excel读数据量少于1千行数据转成javamodel,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void simpleReadJavaModelV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); - List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); - inputStream.close(); - print(data); - } - - /** - * 03版本excel读数据量大于1千行数据,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void saxReadListStringV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); - ExcelListener excelListener = new ExcelListener(); - EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1), excelListener); - inputStream.close(); - } - - /** - * 03版本excel读数据量大于1千行数据转成javamodel,内部采用回调方法. - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void saxReadJavaModelV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); - ExcelListener excelListener = new ExcelListener(); - EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); - inputStream.close(); - } - - /** - * 00版本excel读取sheet - * - * @throws IOException 简单抛出异常,真实环境需要catch异常,同时在finally中关闭流 - */ - @Test - public void saxReadSheetsV2003() throws IOException { - InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); - ExcelListener excelListener = new ExcelListener(); - ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, excelListener); - List sheets = excelReader.getSheets(); - System.out.println(); - for (Sheet sheet : sheets) { - if (sheet.getSheetNo() == 1) { - excelReader.read(sheet); - } else { - sheet.setHeadLineMun(2); - sheet.setClazz(ReadModel.class); - excelReader.read(sheet); - } - } - inputStream.close(); - } - - - public void print(List datas){ - int i=0; - for (Object ob:datas) { - System.out.println(i++); - System.out.println(ob); - } - } - -} diff --git a/src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java b/src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java deleted file mode 100644 index bef9801f..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.alibaba.easyexcel.test.read.large; - -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; - -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.context.AnalysisContext; -import com.alibaba.excel.event.AnalysisEventListener; -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 { - EasyExcelFactory.read().registerReadListener(listener).file(in).sheet().sheetNo(sheetNo).doRead().finish(); - -// -// public static List read(InputStream in, Sheet sheet) { -// final List rows = new ArrayList(); -// new ExcelReader(in, null, new AnalysisEventListener() { -// @Override -// public void invoke(Object object, AnalysisContext context) { -// rows.add(object); -// } -// -// @Override -// public void doAfterAllAnalysed(AnalysisContext context) {} -// }, false).read(sheet); -// return rows; -// } - // LOGGER.info("start"); - // long start = System.currentTimeMillis(); - // // InputStream inputStream = FileUtil.readFile("large/large07.xlsx"); - // ExcelReader excelReader = EasyExcelFactory.getReader(null, new LargeDataListener()); - // excelReader.read(new Sheet(1, 1)); - // // inputStream.close(); - // LOGGER.info("time:{}", System.currentTimeMillis() - start); - } - - @Test - public void hello() throws Exception { - LOGGER.info("start"); - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/read/large/LargeDataListener.java b/src/test/java/com/alibaba/easyexcel/test/read/large/LargeDataListener.java deleted file mode 100644 index e54f8757..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/read/large/LargeDataListener.java +++ /dev/null @@ -1,29 +0,0 @@ -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 { - 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) {} - -} diff --git a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java b/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java deleted file mode 100644 index 47365cda..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java +++ /dev/null @@ -1,26 +0,0 @@ -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.write.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(); - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java b/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java similarity index 75% rename from src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java rename to src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java index 69e6b1ec..ecfe5674 100644 --- a/src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java +++ b/src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java @@ -3,18 +3,14 @@ package com.alibaba.easyexcel.test.util; import java.io.File; import java.io.InputStream; -public class FileUtil { +public class TestFileUtil { public static InputStream getResourcesFileInputStream(String fileName) { return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName); } - public static InputStream readFile(String fileName) { - return Thread.currentThread().getContextClassLoader().getResourceAsStream("read/" + fileName); - } - public static String getPath() { - return FileUtil.class.getResource("/").getPath(); + return TestFileUtil.class.getResource("/").getPath(); } public static File createNewFile(String pathName) { diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/wirte/WriteTest.java deleted file mode 100644 index e8267d73..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/WriteTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.alibaba.easyexcel.test.wirte; - -import java.io.IOException; - -import org.junit.Test; - -public class WriteTest { - - @Test - public void writeV2007() throws IOException { - - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java deleted file mode 100644 index c5da2829..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.alibaba.easyexcel.test.wirte.nohead; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.Test; - -import com.alibaba.easyexcel.test.util.FileUtil; -import com.alibaba.easyexcel.test.wirte.order.OrderData; -import com.alibaba.excel.EasyExcelFactory; -import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.write.metadata.Sheet; - -/** - * Order data test - * - * @author zhuangjiaju - */ -public class NoHeadData07Test { - - @Test - public void simple() { - ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(FileUtil.createNewWriteFile("order07.xlsx")) - .head(OrderData.class).build(); - Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); - writer.write(createData(10000 * 100), sheet); - writer.finish(); - } - - private List createData(int count) { - List list = new ArrayList(); - for (int i = 0; i < count; i++) { - OrderData orderData = new OrderData(); - orderData.setIndex1("排序1:" + i); - orderData.setIndex10("排序10:" + i); - list.add(orderData); - } - return list; - } -} diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData.java b/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData.java deleted file mode 100644 index de6e9092..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.alibaba.easyexcel.test.wirte.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; -} diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData03Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData03Test.java deleted file mode 100644 index 95d639e0..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData03Test.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.alibaba.easyexcel.test.wirte.simple; - -import org.junit.Test; - -/** - * Simple data test - * - * @author zhuangjiaju - */ -public class SimpleData03Test { - - @Test - public void simple07() { - - } - -} diff --git a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java b/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java deleted file mode 100644 index 98171db1..00000000 --- a/src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.alibaba.easyexcel.test.wirte.simple; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.Test; - -import com.alibaba.easyexcel.test.util.FileUtil; -import com.alibaba.excel.EasyExcelFactory; -import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.write.metadata.Sheet; - -/** - * Simple data test - * - * @author zhuangjiaju - */ -public class SimpleData07Test { - - @Test - public void simple() { - ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(FileUtil.createNewWriteFile("simple07.xlsx")) - .head(SimpleData.class).build(); - Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("simple").build(); - writer.write(createData(10), sheet); - writer.finish(); - } - - @Test - public void repeatWrite() { - ExcelWriter writer = EasyExcelFactory.writerBuilder() - .outputFile(FileUtil.createNewWriteFile("repeatWrite07.xlsx")).head(SimpleData.class).build(); - Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("simple").build(); - writer.write(createData(10), sheet); - writer.write(createData(10), sheet); - writer.finish(); - } - - private List createData(int count) { - List list = new ArrayList(); - for (int i = 0; i < count; i++) { - SimpleData simpleData = new SimpleData(); - simpleData.setString1("一号字" + i); - simpleData.setString2("二号字" + i); - list.add(simpleData); - } - return list; - } -} diff --git a/src/test/resources/read/large/large07.xlsx b/src/test/resources/large/large07.xlsx similarity index 100% rename from src/test/resources/read/large/large07.xlsx rename to src/test/resources/large/large07.xlsx diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml index b7641287..602049a9 100644 --- a/src/test/resources/logback.xml +++ b/src/test/resources/logback.xml @@ -7,7 +7,7 @@ ${LOG_PATTERN} - + diff --git a/src/test/resources/read/simple/simple07.xlsx b/src/test/resources/simple/simple07Test.xlsx similarity index 100% rename from src/test/resources/read/simple/simple07.xlsx rename to src/test/resources/simple/simple07Test.xlsx