Browse Source

优化读写的写法,方便读写

pull/507/head
zhuangjiaju 6 years ago
parent
commit
ce03b82c71
  1. 406
      src/main/java/com/alibaba/excel/EasyExcelFactory.java
  2. 39
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  3. 12
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  4. 3
      src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java
  5. 10
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  6. 34
      src/main/java/com/alibaba/excel/cache/Ehcache.java
  7. 68
      src/main/java/com/alibaba/excel/context/AnalysisContext.java
  8. 44
      src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  9. 41
      src/main/java/com/alibaba/excel/context/WriteContext.java
  10. 25
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  11. 52
      src/main/java/com/alibaba/excel/converters/ConverterKey.java
  12. 18
      src/main/java/com/alibaba/excel/converters/ConverterKeyBuild.java
  13. 17
      src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java
  14. 30
      src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
  15. 4
      src/main/java/com/alibaba/excel/metadata/Head.java
  16. 10
      src/main/java/com/alibaba/excel/metadata/Holder.java
  17. 2
      src/main/java/com/alibaba/excel/metadata/TableStyle.java
  18. 12
      src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
  19. 5
      src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java
  20. 8
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  21. 35
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  22. 10
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java
  23. 33
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java
  24. 2
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java
  25. 4
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java
  26. 21
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  27. 2
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  28. 7
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  29. 17
      src/main/java/com/alibaba/excel/write/merge/AbstractMergeStrategy.java
  30. 24
      src/main/java/com/alibaba/excel/write/metadata/holder/AbstractWriteHolder.java
  31. 8
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java
  32. 25
      src/test/java/com/alibaba/easyexcel/test/ReadTest.java
  33. 10
      src/test/java/com/alibaba/easyexcel/test/WriteTest.java
  34. 60
      src/test/java/com/alibaba/easyexcel/test/core/large/LargeData.java
  35. 34
      src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java
  36. 27
      src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
  37. 36
      src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java
  38. 2
      src/test/java/com/alibaba/easyexcel/test/core/order/OrderData.java
  39. 17
      src/test/java/com/alibaba/easyexcel/test/core/order/OrderData07Test.java
  40. 2
      src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleData.java
  41. 16
      src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java
  42. 22
      src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java
  43. 40
      src/test/java/com/alibaba/easyexcel/test/ehcache/EncacheTest.java
  44. 189
      src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java
  45. 57
      src/test/java/com/alibaba/easyexcel/test/read/large/LargeData07Test.java
  46. 29
      src/test/java/com/alibaba/easyexcel/test/read/large/LargeDataListener.java
  47. 26
      src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java
  48. 8
      src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java
  49. 13
      src/test/java/com/alibaba/easyexcel/test/wirte/WriteTest.java
  50. 40
      src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java
  51. 16
      src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData.java
  52. 17
      src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData03Test.java
  53. 49
      src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java
  54. 0
      src/test/resources/large/large07.xlsx
  55. 2
      src/test/resources/logback.xml
  56. 0
      src/test/resources/simple/simple07Test.xlsx

406
src/main/java/com/alibaba/excel/EasyExcelFactory.java

@ -1,7 +1,9 @@
package com.alibaba.excel; package com.alibaba.excel;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -11,6 +13,7 @@ import com.alibaba.excel.event.WriteHandler;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.builder.ExcelReaderBuilder; import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder; import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder; import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder; import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
@ -19,6 +22,20 @@ import com.alibaba.excel.write.builder.ExcelWriterTableBuilder;
/** /**
* Reader and writer factory class * Reader and writer factory class
* *
* <h1>Quick start</h1>
* <h2>Read</h2>
* <h3>Sample1</h3>
*
* <h3>Sample2</h3>
*
* <h2>Write</h2>
*
* <h3>Sample1</h3>
*
* <h3>Sample2</h3>
*
*
*
* @author jipengfei * @author jipengfei
*/ */
public class EasyExcelFactory { public class EasyExcelFactory {
@ -31,7 +48,7 @@ public class EasyExcelFactory {
* @param sheet * @param sheet
* read sheet. * read sheet.
* @return analysis result. * @return analysis result.
* @deprecated please use 'EasyExcelFactory.read().file(in).sheet(sheetNo).doReadSync();' * @deprecated please use 'EasyExcelFactory.read(in).sheet(sheetNo).doReadSync();'
*/ */
@Deprecated @Deprecated
public static List<Object> read(InputStream in, Sheet sheet) { public static List<Object> read(InputStream in, Sheet sheet) {
@ -57,7 +74,7 @@ public class EasyExcelFactory {
* read sheet. * read sheet.
* @param listener * @param listener
* Callback method after each row is parsed. * 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 @Deprecated
public static void readBySax(InputStream in, Sheet sheet, AnalysisEventListener listener) { 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(); .registerWriteHandler(handler).autoCloseStream(Boolean.FALSE).convertAllFiled(Boolean.FALSE).build();
} }
/**
* Build excel the write
*
* @return
*/
public static ExcelWriterBuilder write() { public static ExcelWriterBuilder write() {
return new ExcelWriterBuilder(); 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() { 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(); return new ExcelWriterSheetBuilder();
} }
/**
* Build excel the 'writerTable'
*
* @return
*/
public static ExcelWriterTableBuilder writerTable() { 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() { public static ExcelReaderBuilder read() {
return new ExcelReaderBuilder(); 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() { 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(); return new ExcelReaderSheetBuilder();
} }
} }

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

@ -1,5 +1,7 @@
package com.alibaba.excel.analysis; package com.alibaba.excel.analysis;
import java.io.IOException;
import com.alibaba.excel.analysis.v03.XlsSaxAnalyser; import com.alibaba.excel.analysis.v03.XlsSaxAnalyser;
import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser; import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser;
import com.alibaba.excel.context.AnalysisContext; 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.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; 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.support.ExcelTypeEnum;
import com.alibaba.excel.util.FileUtils;
/** /**
* @author jipengfei * @author jipengfei
@ -19,8 +23,16 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
private ExcelExecutor excelExecutor; private ExcelExecutor excelExecutor;
public ExcelAnalyserImpl(ReadWorkbook readWorkbook) { public ExcelAnalyserImpl(ReadWorkbook readWorkbook) {
try {
analysisContext = new AnalysisContextImpl(readWorkbook); analysisContext = new AnalysisContextImpl(readWorkbook);
choiceExcelExecutor(); choiceExcelExecutor();
} catch (RuntimeException e) {
finish();
throw e;
} catch (Throwable e) {
finish();
throw new ExcelAnalysisException(e);
}
} }
private void choiceExcelExecutor() { private void choiceExcelExecutor() {
@ -47,13 +59,38 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
@Override @Override
public void analysis(ReadSheet readSheet) { public void analysis(ReadSheet readSheet) {
try {
analysisContext.currentSheet(excelExecutor, readSheet); analysisContext.currentSheet(excelExecutor, readSheet);
excelExecutor.execute(); excelExecutor.execute();
analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext); analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext);
} catch (RuntimeException e) {
finish();
throw e;
} catch (Throwable e) {
finish();
throw new ExcelAnalysisException(e);
}
} }
@Override @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 @Override
public com.alibaba.excel.analysis.ExcelExecutor excelExecutor() { public com.alibaba.excel.analysis.ExcelExecutor excelExecutor() {

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

@ -112,12 +112,6 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
return sheetList; return sheetList;
} }
@Override
public void execute() {
parseXmlSource(sheetMap.get(analysisContext.readSheetHolder().getSheetNo()),
new XlsxRowHandler(analysisContext));
}
private void parseXmlSource(InputStream inputStream, ContentHandler handler) { private void parseXmlSource(InputStream inputStream, ContentHandler handler) {
InputSource inputSource = new InputSource(inputStream); InputSource inputSource = new InputSource(inputStream);
try { try {
@ -143,4 +137,10 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
} }
} }
@Override
public void execute() {
parseXmlSource(sheetMap.get(analysisContext.readSheetHolder().getSheetNo()),
new XlsxRowHandler(analysisContext));
}
} }

3
src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java

@ -15,6 +15,7 @@ public class CountRowCellHandler implements XlsxCellHandler {
public CountRowCellHandler(AnalysisContext analysisContext) { public CountRowCellHandler(AnalysisContext analysisContext) {
this.analysisContext = analysisContext; this.analysisContext = analysisContext;
} }
@Override @Override
public boolean support(String name) { public boolean support(String name) {
return DIMENSION.equals(name); return DIMENSION.equals(name);
@ -25,7 +26,7 @@ public class CountRowCellHandler implements XlsxCellHandler {
String d = attributes.getValue(DIMENSION_REF); String d = attributes.getValue(DIMENSION_REF);
String totalStr = d.substring(d.indexOf(":") + 1, d.length()); String totalStr = d.substring(d.indexOf(":") + 1, d.length());
String c = totalStr.toUpperCase().replaceAll("[A-Z]", ""); String c = totalStr.toUpperCase().replaceAll("[A-Z]", "");
analysisContext.setTotalCount(Integer.parseInt(c)); analysisContext.readSheetHolder().setTotal(Integer.parseInt(c));
} }
@Override @Override

10
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.context.AnalysisContext;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; 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.BooleanUtils;
import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
@ -55,7 +56,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
if (nextRow > curRow) { if (nextRow > curRow) {
curRow = nextRow; curRow = nextRow;
} }
analysisContext.setCurrentRowNum(curRow); analysisContext
.readRowHolder(new ReadRowHolder(curRow, analysisContext.readSheetHolder().getGlobalConfiguration()));
curCol = PositionUtils.getCol(currentCellIndex); curCol = PositionUtils.getCol(currentCellIndex);
// t="s" ,it's means String // t="s" ,it's means String
@ -68,7 +70,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
} }
// cell is formula // cell is formula
if (CELL_FORMULA_TAG.equals(name)) { 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(); ensureSize();
// Have to go "sharedStrings.xml" and get it // Have to go "sharedStrings.xml" and get it
if (currentCellData.getType() == CellDataTypeEnum.STRING) { if (currentCellData.getType() == CellDataTypeEnum.STRING) {
currentCellData.setStringValue(analysisContext.currentWorkbookHolder().getReadCache() currentCellData.setStringValue(analysisContext.readWorkbookHolder().getReadCache()
.get(Integer.valueOf(currentCellData.getStringValue()))); .get(Integer.valueOf(currentCellData.getStringValue())));
} }
curRowContent[curCol] = currentCellData; curRowContent[curCol] = currentCellData;
@ -108,7 +110,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
return; return;
} }
if (CELL_FORMULA_TAG.equals(currentTag)) { if (CELL_FORMULA_TAG.equals(currentTag)) {
currentCellData.setReadFormula(currentCellValue); currentCellData.setFormulaValue(currentCellValue);
return; return;
} }
CellDataTypeEnum oldType = currentCellData.getType(); CellDataTypeEnum oldType = currentCellData.getType();

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

@ -31,8 +31,8 @@ public class Ehcache implements ReadCache {
private static final Logger LOGGER = LoggerFactory.getLogger(Ehcache.class); private static final Logger LOGGER = LoggerFactory.getLogger(Ehcache.class);
private static final int BATCH_COUNT = 500; private static final int BATCH_COUNT = 1000;
private static final int CHECK_INTERVAL = 1000; private static final int CHECK_INTERVAL = 500;
private static final int MAX_CACHE_ACTIVATE = 10; private static final int MAX_CACHE_ACTIVATE = 10;
private static final String CACHE = "cache"; 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_KEY_VALUE_SEPARATOR = "&kv;";
private static final String ESCAPED_SPECIAL_SEPARATOR = "&s;"; 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 int index = 0;
private StringBuilder data = new StringBuilder(); private StringBuilder data = new StringBuilder();
private CacheManager cacheManager; private CacheManager cacheManager;
@ -76,10 +82,10 @@ public class Ehcache implements ReadCache {
@Override @Override
public void init(AnalysisContext analysisContext) { public void init(AnalysisContext analysisContext) {
File readTempFile = analysisContext.currentWorkbookHolder().getReadTempFile(); File readTempFile = analysisContext.readWorkbookHolder().getTempFile();
if (readTempFile == null) { if (readTempFile == null) {
readTempFile = FileUtils.createCacheTmpFile(); readTempFile = FileUtils.createCacheTmpFile();
analysisContext.currentWorkbookHolder().setReadTempFile(readTempFile); analysisContext.readWorkbookHolder().setTempFile(readTempFile);
} }
File cacheFile = new File(readTempFile.getPath(), UUID.randomUUID().toString()); File cacheFile = new File(readTempFile.getPath(), UUID.randomUUID().toString());
PersistentCacheManager persistentCacheManager = PersistentCacheManager persistentCacheManager =
@ -99,6 +105,11 @@ public class Ehcache implements ReadCache {
data = new StringBuilder(); data = new StringBuilder();
} }
index++; index++;
if (LOGGER.isDebugEnabled()) {
if (index % DEBUG_WRITE_SIZE == 0) {
LOGGER.debug("Already put :{}", index);
}
}
} }
private String escape(String str) { private String escape(String str) {
@ -130,9 +141,9 @@ public class Ehcache implements ReadCache {
int route = key / BATCH_COUNT; int route = key / BATCH_COUNT;
if (cacheMap.containsKey(route)) { if (cacheMap.containsKey(route)) {
lastCheckIntervalUsedSet.add(route); lastCheckIntervalUsedSet.add(route);
countList.add(route); String value = cacheMap.get(route).get(key);
checkClear(); checkClear();
return cacheMap.get(route).get(key); return value;
} }
Map<Integer, String> tempCacheMap = new HashMap<Integer, String>(BATCH_COUNT / 3 * 4 + 1); Map<Integer, String> tempCacheMap = new HashMap<Integer, String>(BATCH_COUNT / 3 * 4 + 1);
String batchData = cache.get(route); String batchData = cache.get(route);
@ -141,13 +152,17 @@ public class Ehcache implements ReadCache {
String[] keyValue = dataString.split(KEY_VALUE_SEPARATOR); String[] keyValue = dataString.split(KEY_VALUE_SEPARATOR);
tempCacheMap.put(Integer.valueOf(keyValue[0]), unescape(keyValue[1])); tempCacheMap.put(Integer.valueOf(keyValue[0]), unescape(keyValue[1]));
} }
countList.add(route);
cacheMap.put(route, tempCacheMap); cacheMap.put(route, tempCacheMap);
if (LOGGER.isDebugEnabled()) { 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); lastCheckIntervalUsedSet.add(route);
String value = tempCacheMap.get(key);
checkClear(); checkClear();
return tempCacheMap.get(key); return value;
} }
private void checkClear() { private void checkClear() {
@ -167,6 +182,9 @@ public class Ehcache implements ReadCache {
} }
// Last 'CHECK_INTERVAL' not use // Last 'CHECK_INTERVAL' not use
iterator.remove(); iterator.remove();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Cache remove because {} times unused.", CHECK_INTERVAL);
}
Iterator<Integer> countIterator = countList.iterator(); Iterator<Integer> countIterator = countList.iterator();
while (countIterator.hasNext()) { while (countIterator.hasNext()) {
Integer route = countIterator.next(); Integer route = countIterator.next();

68
src/main/java/com/alibaba/excel/context/AnalysisContext.java

@ -1,11 +1,16 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import java.io.InputStream;
import com.alibaba.excel.analysis.ExcelExecutor; 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.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadHolder; import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum;
/** /**
* *
@ -61,4 +66,67 @@ public interface AnalysisContext {
* Custom attribute * Custom attribute
*/ */
Object getCustom(); 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();
} }

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

@ -1,16 +1,20 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import java.io.InputStream;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.ExcelExecutor; import com.alibaba.excel.analysis.ExcelExecutor;
import com.alibaba.excel.exception.ExcelAnalysisException; 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.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadHolder; import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
/** /**
@ -119,4 +123,44 @@ public class AnalysisContextImpl implements AnalysisContext {
public Object getCustom() { public Object getCustom() {
return readWorkbookHolder.getCustomObject(); 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");
}
} }

41
src/main/java/com/alibaba/excel/context/WriteContext.java

@ -1,5 +1,10 @@
package com.alibaba.excel.context; 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.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.holder.WriteHolder; import com.alibaba.excel.write.metadata.holder.WriteHolder;
@ -49,7 +54,8 @@ public interface WriteContext {
WriteTableHolder writeTableHolder(); 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 * @return
*/ */
@ -59,4 +65,37 @@ public interface WriteContext {
* close * close
*/ */
void finish(); 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();
} }

25
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -1,12 +1,14 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -337,6 +339,9 @@ public class WriteContextImpl implements WriteContext {
@Override @Override
public void finish() { public void finish() {
if (writeWorkbookHolder == null) {
return;
}
try { try {
writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream()); writeWorkbookHolder.getWorkbook().write(writeWorkbookHolder.getOutputStream());
writeWorkbookHolder.getWorkbook().close(); writeWorkbookHolder.getWorkbook().close();
@ -355,4 +360,24 @@ public class WriteContextImpl implements WriteContext {
LOGGER.debug("Finished write."); 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();
}
} }

52
src/main/java/com/alibaba/excel/converters/ConverterKey.java

@ -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;
}
}

18
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();
}
}

17
src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java

@ -39,8 +39,8 @@ public class DefaultConverterLoader {
* *
* @return * @return
*/ */
public static Map<Class, Converter> loadDefaultWriteConverter() { public static Map<String, Converter> loadDefaultWriteConverter() {
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(); Map<String, Converter> converterMap = new HashMap<String, Converter>();
putWriteConverter(converterMap, new BigDecimalNumberConverter()); putWriteConverter(converterMap, new BigDecimalNumberConverter());
putWriteConverter(converterMap, new BooleanBooleanConverter()); putWriteConverter(converterMap, new BooleanBooleanConverter());
putWriteConverter(converterMap, new ByteNumberConverter()); putWriteConverter(converterMap, new ByteNumberConverter());
@ -54,8 +54,8 @@ public class DefaultConverterLoader {
return converterMap; return converterMap;
} }
private static void putWriteConverter(Map<Class, Converter> converterMap, Converter converter) { private static void putWriteConverter(Map<String, Converter> converterMap, Converter converter) {
converterMap.put(converter.supportJavaTypeKey(), converter); converterMap.put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter);
} }
/** /**
@ -63,8 +63,8 @@ public class DefaultConverterLoader {
* *
* @return * @return
*/ */
public static Map<ConverterKey, Converter> loadDefaultReadConverter() { public static Map<String, Converter> loadDefaultReadConverter() {
Map<ConverterKey, Converter> converterMap = new HashMap<ConverterKey, Converter>(); Map<String, Converter> converterMap = new HashMap<String, Converter>();
putReadConverter(converterMap, new BigDecimalBooleanConverter()); putReadConverter(converterMap, new BigDecimalBooleanConverter());
putReadConverter(converterMap, new BigDecimalNumberConverter()); putReadConverter(converterMap, new BigDecimalNumberConverter());
putReadConverter(converterMap, new BigDecimalStringConverter()); putReadConverter(converterMap, new BigDecimalStringConverter());
@ -107,7 +107,8 @@ public class DefaultConverterLoader {
return converterMap; return converterMap;
} }
private static void putReadConverter(Map<ConverterKey, Converter> converterMap, Converter converter) { private static void putReadConverter(Map<String, Converter> converterMap, Converter converter) {
converterMap.put(ConverterKey.buildConverterKey(converter), converter); converterMap.put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()),
converter);
} }
} }

30
src/main/java/com/alibaba/excel/metadata/AbstractHolder.java

@ -1,6 +1,9 @@
package com.alibaba.excel.metadata; package com.alibaba.excel.metadata;
import java.util.List; import java.util.List;
import java.util.Map;
import com.alibaba.excel.converters.Converter;
/** /**
* Write/read holder * Write/read holder
@ -25,10 +28,24 @@ public abstract class AbstractHolder implements Holder {
*/ */
private GlobalConfiguration globalConfiguration; private GlobalConfiguration globalConfiguration;
/**
* <li>Read key:
* <li>Write key:
*/
private Map<String, Converter> converterMap;
public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) { public AbstractHolder(BasicParameter basicParameter, AbstractHolder prentAbstractHolder) {
this.newInitialization = Boolean.TRUE; this.newInitialization = Boolean.TRUE;
if (basicParameter.getHead() == null && prentAbstractHolder != null) {
this.head = prentAbstractHolder.getHead();
} else {
this.head = basicParameter.getHead(); this.head = basicParameter.getHead();
}
if (basicParameter.getClazz() == null && prentAbstractHolder != null) {
this.clazz = prentAbstractHolder.getClazz();
} else {
this.clazz = basicParameter.getClazz(); this.clazz = basicParameter.getClazz();
}
this.globalConfiguration = new GlobalConfiguration(); this.globalConfiguration = new GlobalConfiguration();
if (basicParameter.getAutoTrim() == null) { if (basicParameter.getAutoTrim() == null) {
if (prentAbstractHolder == null) { if (prentAbstractHolder == null) {
@ -73,6 +90,19 @@ public abstract class AbstractHolder implements Holder {
this.globalConfiguration = globalConfiguration; this.globalConfiguration = globalConfiguration;
} }
public Map<String, Converter> getConverterMap() {
return converterMap;
}
public void setConverterMap(Map<String, Converter> converterMap) {
this.converterMap = converterMap;
}
@Override
public Map<String, Converter> converterMap() {
return getConverterMap();
}
@Override @Override
public GlobalConfiguration globalConfiguration() { public GlobalConfiguration globalConfiguration() {
return getGlobalConfiguration(); return getGlobalConfiguration();

4
src/main/java/com/alibaba/excel/metadata/Head.java

@ -42,15 +42,17 @@ public class Head {
this.fieldName = fieldName; this.fieldName = fieldName;
headNameList = new ArrayList<String>(); headNameList = new ArrayList<String>();
headNameList.add(headName); headNameList.add(headName);
this.forceIndex = Boolean.FALSE;
} }
public Head(Integer columnIndex, String fieldName, List<String> headNameList) { public Head(Integer columnIndex, String fieldName, List<String> headNameList, Boolean forceIndex) {
this.columnIndex = columnIndex; this.columnIndex = columnIndex;
this.fieldName = fieldName; this.fieldName = fieldName;
if (headNameList == null) { if (headNameList == null) {
headNameList = new ArrayList<String>(); headNameList = new ArrayList<String>();
} }
this.headNameList = headNameList; this.headNameList = headNameList;
this.forceIndex = forceIndex;
} }
public Integer getColumnIndex() { public Integer getColumnIndex() {

10
src/main/java/com/alibaba/excel/metadata/Holder.java

@ -1,5 +1,8 @@
package com.alibaba.excel.metadata; package com.alibaba.excel.metadata;
import java.util.Map;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.enums.HolderEnum;
/** /**
@ -31,4 +34,11 @@ public interface Holder {
* @return * @return
*/ */
GlobalConfiguration globalConfiguration(); GlobalConfiguration globalConfiguration();
/**
* What converter does the currently operated cell need to execute
*
* @return
*/
Map<String, Converter> converterMap();
} }

2
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 org.apache.poi.ss.usermodel.IndexedColors;
import com.alibaba.excel.write.style.RowCellStyleStrategy;
/** /**
* @author jipengfei * @author jipengfei
* @deprecated please use {@link RowCellStyleStrategy} * @deprecated please use {@link RowCellStyleStrategy}

12
src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java

@ -57,7 +57,7 @@ public class ExcelHeadProperty {
headRowNumber = 0; headRowNumber = 0;
if (head != null && !head.isEmpty()) { if (head != null && !head.isEmpty()) {
for (int i = 0; i < head.size(); i++) { 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); contentPropertyMap.put(i, null);
} }
headKind = HeadKindEnum.STRING; headKind = HeadKindEnum.STRING;
@ -135,21 +135,21 @@ public class ExcelHeadProperty {
int index = 0; int index = 0;
for (Field field : defaultFieldList) { for (Field field : defaultFieldList) {
while (customFiledMap.containsKey(index)) { while (customFiledMap.containsKey(index)) {
initOneColumnProperty(index, customFiledMap.get(index)); initOneColumnProperty(index, customFiledMap.get(index), Boolean.TRUE);
customFiledMap.remove(index); customFiledMap.remove(index);
index++; index++;
} }
initOneColumnProperty(index, field); initOneColumnProperty(index, field, Boolean.FALSE);
index++; index++;
} }
for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) { for (Map.Entry<Integer, Field> entry : customFiledMap.entrySet()) {
initOneColumnProperty(index, entry.getValue()); initOneColumnProperty(index, entry.getValue(), Boolean.FALSE);
index++; index++;
} }
headKind = HeadKindEnum.CLASS; 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); ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
List<String> tmpHeadList = new ArrayList<String>(); List<String> tmpHeadList = new ArrayList<String>();
if (excelProperty != null) { if (excelProperty != null) {
@ -160,7 +160,7 @@ public class ExcelHeadProperty {
if (tmpHeadList.isEmpty() || StringUtils.isEmpty(tmpHeadList.get(0))) { if (tmpHeadList.isEmpty() || StringUtils.isEmpty(tmpHeadList.get(0))) {
tmpHeadList.add(field.getName()); 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 excelContentProperty = new ExcelContentProperty();
excelContentProperty.setHead(head); excelContentProperty.setHead(head);
excelContentProperty.setField(field); excelContentProperty.setField(field);

5
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.ExcelReader;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.SyncReadListener; import com.alibaba.excel.event.SyncReadListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
@ -166,7 +167,7 @@ public class ExcelReaderSheetBuilder {
*/ */
public void finish() { public void finish() {
if (excelReader == null) { 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(); excelReader.finish();
} }
@ -178,7 +179,7 @@ public class ExcelReaderSheetBuilder {
*/ */
public List<Object> doReadSync() { public List<Object> doReadSync() {
if (excelReader == null) { 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(); SyncReadListener syncReadListener = new SyncReadListener();
registerReadListener(syncReadListener); registerReadListener(syncReadListener);

8
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.context.AnalysisContext;
import com.alibaba.excel.converters.Converter; 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.CellDataTypeEnum;
import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener; import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener;
@ -67,7 +67,7 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
continue; continue;
} }
ExcelContentProperty excelContentProperty = contentPropertyMap.get(index); ExcelContentProperty excelContentProperty = contentPropertyMap.get(index);
Object value = convertValue(cellData, excelContentProperty.getField().getClass(), excelContentProperty, Object value = convertValue(cellData, excelContentProperty.getField().getType(), excelContentProperty,
currentReadHolder.converterMap()); currentReadHolder.converterMap());
if (value != null) { if (value != null) {
map.put(excelContentProperty.getField().getName(), value); map.put(excelContentProperty.getField().getName(), value);
@ -78,8 +78,8 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
} }
private Object convertValue(CellData cellData, Class clazz, ExcelContentProperty contentProperty, private Object convertValue(CellData cellData, Class clazz, ExcelContentProperty contentProperty,
Map<ConverterKey, Converter> readConverterMap) { Map<String, Converter> converterMap) {
Converter converter = readConverterMap.get(ConverterKey.buildConverterKey(clazz, cellData.getType())); Converter converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType()));
if (converter == null) { if (converter == null) {
throw new ExcelDataConvertException( throw new ExcelDataConvertException(
"Converter not found, convert " + cellData.getType() + " to " + clazz.getName()); "Converter not found, convert " + cellData.getType() + " to " + clazz.getName());

35
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.context.AnalysisContext;
import com.alibaba.excel.converters.Converter; 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.HeadKindEnum;
import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
@ -47,10 +48,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
* Read listener * Read listener
*/ */
private List<ReadListener> readListenerList; private List<ReadListener> readListenerList;
/**
* Converter for workbook
*/
private Map<ConverterKey, Converter> converterMap;
public AbstractReadHolder(ReadBasicParameter readBasicParameter, AbstractReadHolder parentAbstractReadHolder, public AbstractReadHolder(ReadBasicParameter readBasicParameter, AbstractReadHolder parentAbstractReadHolder,
Boolean convertAllFiled) { Boolean convertAllFiled) {
@ -89,14 +86,16 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
} }
if (parentAbstractReadHolder == null) { if (parentAbstractReadHolder == null) {
this.converterMap = new HashMap<ConverterKey, Converter>(); setConverterMap(DefaultConverterLoader.loadDefaultReadConverter());
} else { } else {
this.converterMap = new HashMap<ConverterKey, Converter>(parentAbstractReadHolder.getConverterMap()); setConverterMap(new HashMap<String, Converter>(parentAbstractReadHolder.getConverterMap()));
} }
if (readBasicParameter.getCustomConverterList() != null if (readBasicParameter.getCustomConverterList() != null
&& !readBasicParameter.getCustomConverterList().isEmpty()) { && !readBasicParameter.getCustomConverterList().isEmpty()) {
for (Converter converter : readBasicParameter.getCustomConverterList()) { 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) { public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) {
List<CellData> cellDataList = (List<CellData>)event.getAnalysisResult(); List<CellData> cellDataList = (List<CellData>)event.getAnalysisResult();
ReadRowHolder readRowHolder = analysisContext.readRowHolder(); ReadRowHolder readRowHolder = analysisContext.readRowHolder();
readRowHolder.setCurrentRowAnalysisResult(cellDataList);
if (readRowHolder.getRowIndex() >= analysisContext.readSheetHolder().getHeadRowNumber()) { if (readRowHolder.getRowIndex() >= analysisContext.readSheetHolder().getHeadRowNumber()) {
for (ReadListener readListener : readListenerList) { for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
try { try {
readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext); readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext);
} catch (Exception e) { } catch (Exception e) {
for (ReadListener readListenerException : readListenerList) { for (ReadListener readListenerException : analysisContext.currentReadHolder().readListenerList()) {
try { try {
readListenerException.onException(e, analysisContext); readListenerException.onException(e, analysisContext);
} catch (Exception exception) { } catch (Exception exception) {
@ -183,7 +183,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
for (CellData cellData : data) { for (CellData cellData : data) {
Converter converter = Converter converter =
readHolder.converterMap().get(ConverterKey.buildConverterKey(String.class, cellData.getType())); readHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType()));
if (converter == null) { if (converter == null) {
throw new ExcelDataConvertException( throw new ExcelDataConvertException(
"Converter not found, convert " + cellData.getType() + " to String"); "Converter not found, convert " + cellData.getType() + " to String");
@ -205,14 +205,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
this.readListenerList = readListenerList; this.readListenerList = readListenerList;
} }
public Map<ConverterKey, Converter> getConverterMap() {
return converterMap;
}
public void setConverterMap(Map<ConverterKey, Converter> converterMap) {
this.converterMap = converterMap;
}
public ExcelReadHeadProperty getExcelReadHeadProperty() { public ExcelReadHeadProperty getExcelReadHeadProperty() {
return excelReadHeadProperty; return excelReadHeadProperty;
} }
@ -234,11 +226,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
return getReadListenerList(); return getReadListenerList();
} }
@Override
public Map<ConverterKey, Converter> converterMap() {
return getConverterMap();
}
@Override @Override
public ExcelReadHeadProperty excelReadHeadProperty() { public ExcelReadHeadProperty excelReadHeadProperty() {
return getExcelReadHeadProperty(); return getExcelReadHeadProperty();

10
src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java

@ -1,10 +1,7 @@
package com.alibaba.excel.read.metadata.holder; package com.alibaba.excel.read.metadata.holder;
import java.util.List; 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.metadata.Holder;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
@ -23,13 +20,6 @@ public interface ReadHolder extends Holder {
*/ */
List<ReadListener> readListenerList(); List<ReadListener> readListenerList();
/**
* What converter does the currently operated cell need to execute
*
* @return
*/
Map<ConverterKey, Converter> converterMap();
/** /**
* What 'ExcelReadHeadProperty' does the currently operated cell need to execute * What 'ExcelReadHeadProperty' does the currently operated cell need to execute
* *

33
src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java

@ -1,27 +1,31 @@
package com.alibaba.excel.read.metadata.holder; package com.alibaba.excel.read.metadata.holder;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.Holder;
/** /**
* sheet holder * sheet holder
* *
* @author zhuangjiaju * @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 * The result of the previous listener
*/ */
private Object currentRowAnalysisResult; 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() { public GlobalConfiguration getGlobalConfiguration() {
return globalConfiguration; return globalConfiguration;
@ -46,19 +50,4 @@ public class ReadRowHolder implements Holder {
public void setRowIndex(int rowIndex) { public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex; this.rowIndex = rowIndex;
} }
@Override
public HolderEnum holderType() {
return HolderEnum.ROW;
}
@Override
public boolean isNew() {
return true;
}
@Override
public GlobalConfiguration globalConfiguration() {
return getGlobalConfiguration();
}
} }

2
src/main/java/com/alibaba/excel/read/metadata/holder/ReadSheetHolder.java

@ -32,7 +32,7 @@ public class ReadSheetHolder extends AbstractReadHolder {
private Integer total; private Integer total;
public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { public ReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) {
super(readSheet, null, readWorkbookHolder.getReadWorkbook().getConvertAllFiled()); super(readSheet, readWorkbookHolder, readWorkbookHolder.getReadWorkbook().getConvertAllFiled());
this.readSheet = readSheet; this.readSheet = readSheet;
this.parentReadWorkbookHolder = readWorkbookHolder; this.parentReadWorkbookHolder = readWorkbookHolder;
this.sheetNo = readSheet.getSheetNo(); this.sheetNo = readSheet.getSheetNo();

4
src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java

@ -74,11 +74,11 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
public ReadWorkbookHolder(ReadWorkbook readWorkbook) { public ReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook, null, readWorkbook.getConvertAllFiled()); super(readWorkbook, null, readWorkbook.getConvertAllFiled());
this.readWorkbook = readWorkbook; this.readWorkbook = readWorkbook;
this.inputStream = readWorkbook.getInputStream();
this.file = readWorkbook.getFile();
if (file == null && inputStream == null) { if (file == null && inputStream == null) {
throw new ExcelAnalysisException("File and inputStream must be a non-null."); throw new ExcelAnalysisException("File and inputStream must be a non-null.");
} }
this.inputStream = readWorkbook.getInputStream();
this.file = readWorkbook.getFile();
if (readWorkbook.getMandatoryUseInputStream() == null) { if (readWorkbook.getMandatoryUseInputStream() == null) {
this.mandatoryUseInputStream = Boolean.FALSE; this.mandatoryUseInputStream = Boolean.FALSE;
} else { } else {

21
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.WriteContext;
import com.alibaba.excel.context.WriteContextImpl; import com.alibaba.excel.context.WriteContextImpl;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
@ -38,9 +40,17 @@ public class ExcelBuilderImpl implements ExcelBuilder {
private WriteContext context; private WriteContext context;
public ExcelBuilderImpl(WriteWorkbook writeWorkbook) { public ExcelBuilderImpl(WriteWorkbook writeWorkbook) {
try {
// Create temporary cache directory at initialization time to avoid POI concurrent write bugs // Create temporary cache directory at initialization time to avoid POI concurrent write bugs
FileUtils.createPoiFilesDirectory(); FileUtils.createPoiFilesDirectory();
context = new WriteContextImpl(writeWorkbook); context = new WriteContextImpl(writeWorkbook);
} catch (RuntimeException e) {
finish();
throw e;
} catch (Throwable e) {
finish();
throw new ExcelGenerateException(e);
}
} }
private void doAddContent(List data) { private void doAddContent(List data) {
@ -65,9 +75,18 @@ public class ExcelBuilderImpl implements ExcelBuilder {
@Override @Override
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) { public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) {
try {
context.currentSheet(writeSheet); context.currentSheet(writeSheet);
context.currentTable(writeTable); context.currentTable(writeTable);
doAddContent(data); doAddContent(data);
} catch (RuntimeException e) {
finish();
throw e;
} catch (Throwable e) {
finish();
throw new ExcelGenerateException(e);
}
} }
@Override @Override
@ -235,7 +254,7 @@ public class ExcelBuilderImpl implements ExcelBuilder {
if (value == null) { if (value == null) {
return; return;
} }
Converter converter = currentWriteHolder.converterMap().get(clazz); Converter converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz));
if (converter == null) { if (converter == null) {
throw new ExcelDataConvertException( throw new ExcelDataConvertException(
"Can not find 'Converter' support class " + clazz.getSimpleName() + "."); "Can not find 'Converter' support class " + clazz.getSimpleName() + ".");

2
src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java

@ -149,7 +149,7 @@ public class ExcelWriterBuilder {
return file(new File(outputPathName)); return file(new File(outputPathName));
} }
public ExcelWriterBuilder outputFile(URI outputUri) { public ExcelWriterBuilder file(URI outputUri) {
return file(new File(outputUri)); return file(new File(outputUri));
} }

7
src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java

@ -135,6 +135,13 @@ public class ExcelWriterSheetBuilder {
return new ExcelWriterSheetBuilder(excelWriter); 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() { public ExcelWriterTableBuilder table() {
return table(null); return table(null);
} }

17
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 org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.metadata.Head; 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.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
/** /**
* Merge strategy * Merge strategy
@ -15,18 +15,19 @@ import com.alibaba.excel.write.handler.CellWriteHandler;
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public abstract class AbstractMergeStrategy implements CellWriteHandler { public abstract class AbstractMergeStrategy implements CellWriteHandler {
@Override @Override
public void beforeCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Row row, Head head, public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
int relativeRowIndex, boolean isHead) {} Head head, int relativeRowIndex, boolean isHead) {
}
@Override @Override
public void afterCellCreate(SheetHolder sheetHolder, TableHolder tableHolder, Cell cell, Head head, public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,
int relativeRowIndex, boolean isHead) { Head head, int relativeRowIndex, boolean isHead) {
if (isHead) { if (isHead) {
return; return;
} }
merge(sheetHolder.getSheet(), cell, head, relativeRowIndex); merge(writeSheetHolder.getSheet(), cell, head, relativeRowIndex);
} }
/** /**

24
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 org.apache.poi.ss.usermodel.Sheet;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.DefaultConverterLoader; import com.alibaba.excel.converters.DefaultConverterLoader;
import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.event.NotRepeatExecutor; import com.alibaba.excel.event.NotRepeatExecutor;
@ -60,10 +61,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
* Write handler for workbook * Write handler for workbook
*/ */
private Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap; private Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap;
/**
* Converter for workbook
*/
private Map<Class, Converter> converterMap;
public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder, public AbstractWriteHolder(WriteBasicParameter writeBasicParameter, AbstractWriteHolder parentAbstractWriteHolder,
Boolean convertAllFiled) { Boolean convertAllFiled) {
@ -122,14 +119,14 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
// Set converterMap // Set converterMap
if (parentAbstractWriteHolder == null) { if (parentAbstractWriteHolder == null) {
this.converterMap = DefaultConverterLoader.loadDefaultWriteConverter(); setConverterMap(DefaultConverterLoader.loadDefaultWriteConverter());
} else { } else {
this.converterMap = new HashMap<Class, Converter>(parentAbstractWriteHolder.getConverterMap()); setConverterMap(new HashMap<String, Converter>(parentAbstractWriteHolder.getConverterMap()));
} }
if (writeBasicParameter.getCustomConverterList() != null if (writeBasicParameter.getCustomConverterList() != null
&& !writeBasicParameter.getCustomConverterList().isEmpty()) { && !writeBasicParameter.getCustomConverterList().isEmpty()) {
for (Converter converter : writeBasicParameter.getCustomConverterList()) { 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; this.writeHandlerMap = writeHandlerMap;
} }
public Map<Class, Converter> getConverterMap() {
return converterMap;
}
public void setConverterMap(Map<Class, Converter> converterMap) {
this.converterMap = converterMap;
}
public ExcelWriteHeadProperty getExcelWriteHeadProperty() { public ExcelWriteHeadProperty getExcelWriteHeadProperty() {
return excelWriteHeadProperty; return excelWriteHeadProperty;
} }
@ -402,11 +391,6 @@ public abstract class AbstractWriteHolder extends AbstractHolder implements Writ
return getWriteHandlerMap(); return getWriteHandlerMap();
} }
@Override
public Map<Class, Converter> converterMap() {
return getConverterMap();
}
@Override @Override
public boolean needHead() { public boolean needHead() {
return getNeedHead(); return getNeedHead();

8
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.List;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.metadata.Holder;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.property.ExcelWriteHeadProperty; import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
@ -27,13 +26,6 @@ public interface WriteHolder extends Holder {
*/ */
Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap(); Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap();
/**
* What converter does the currently operated cell need to execute
*
* @return
*/
Map<Class, Converter> converterMap();
/** /**
* Whether a header is required for the currently operated cell * Whether a header is required for the currently operated cell
* *

25
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.listen.ExcelListener;
import com.alibaba.easyexcel.test.model.ReadModel; import com.alibaba.easyexcel.test.model.ReadModel;
import com.alibaba.easyexcel.test.model.ReadModel2; 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.EasyExcelFactory;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.write.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -23,7 +24,7 @@ public class ReadTest {
*/ */
@Test @Test
public void simpleReadListStringV2007() throws IOException { public void simpleReadListStringV2007() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx");
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0));
inputStream.close(); inputStream.close();
print(data); print(data);
@ -37,7 +38,7 @@ public class ReadTest {
*/ */
@Test @Test
public void simpleReadJavaModelV2007() throws IOException { public void simpleReadJavaModelV2007() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx");
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class));
inputStream.close(); inputStream.close();
print(data); print(data);
@ -50,7 +51,7 @@ public class ReadTest {
*/ */
@Test @Test
public void saxReadListStringV2007() throws IOException { public void saxReadListStringV2007() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1), excelListener); EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1), excelListener);
inputStream.close(); inputStream.close();
@ -63,7 +64,7 @@ public class ReadTest {
*/ */
@Test @Test
public void saxReadJavaModelV2007() throws IOException { public void saxReadJavaModelV2007() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener);
inputStream.close(); inputStream.close();
@ -76,7 +77,7 @@ public class ReadTest {
*/ */
@Test @Test
public void saxReadSheetsV2007() throws IOException { public void saxReadSheetsV2007() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2007.xlsx");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener);
List<Sheet> sheets = excelReader.getSheets(); List<Sheet> sheets = excelReader.getSheets();
@ -107,7 +108,7 @@ public class ReadTest {
*/ */
@Test @Test
public void simpleReadListStringV2003() throws IOException { public void simpleReadListStringV2003() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls");
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0)); List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 0));
inputStream.close(); inputStream.close();
print(data); print(data);
@ -120,7 +121,7 @@ public class ReadTest {
*/ */
@Test @Test
public void simpleReadJavaModelV2003() throws IOException { public void simpleReadJavaModelV2003() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls");
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class));
inputStream.close(); inputStream.close();
print(data); print(data);
@ -133,7 +134,7 @@ public class ReadTest {
*/ */
@Test @Test
public void saxReadListStringV2003() throws IOException { public void saxReadListStringV2003() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1), excelListener); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1), excelListener);
inputStream.close(); inputStream.close();
@ -146,7 +147,7 @@ public class ReadTest {
*/ */
@Test @Test
public void saxReadJavaModelV2003() throws IOException { public void saxReadJavaModelV2003() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener);
inputStream.close(); inputStream.close();
@ -159,7 +160,7 @@ public class ReadTest {
*/ */
@Test @Test
public void saxReadSheetsV2003() throws IOException { public void saxReadSheetsV2003() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); InputStream inputStream = TestFileUtil.getResourcesFileInputStream("2003.xls");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener);
List<Sheet> sheets = excelReader.getSheets(); List<Sheet> sheets = excelReader.getSheets();

10
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.listen.AfterExcelWriteHandlerImpl;
import com.alibaba.easyexcel.test.model.WriteModel; 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.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.write.metadata.Table; import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import org.junit.Test; import org.junit.Test;
@ -64,7 +64,7 @@ public class WriteTest {
@Test @Test
public void writeV2007WithTemplate() throws IOException { 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"); OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx");
ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true); ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true);
//写第一个sheet, sheet1 数据全是List<String> 无模型映射关系 //写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
@ -108,7 +108,7 @@ public class WriteTest {
@Test @Test
public void writeV2007WithTemplateAndHandler() throws IOException { 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"); OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx");
ExcelWriter writer = EasyExcelFactory.getWriterWithTempAndHandler(inputStream,out,ExcelTypeEnum.XLSX,true, ExcelWriter writer = EasyExcelFactory.getWriterWithTempAndHandler(inputStream,out,ExcelTypeEnum.XLSX,true,
new AfterExcelWriteHandlerImpl()); new AfterExcelWriteHandlerImpl());

60
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;
}

34
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<LargeData> {
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);
}
}

27
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);
}
}

36
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<OrderData> createData(int count) {
List<OrderData> list = new ArrayList<OrderData>();
for (int i = 0; i < count; i++) {
OrderData orderData = new OrderData();
orderData.setIndex1("排序1:" + i);
orderData.setIndex10("排序10:" + i);
list.add(orderData);
}
return list;
}
}

2
src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData.java → 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; import com.alibaba.excel.annotation.ExcelProperty;

17
src/test/java/com/alibaba/easyexcel/test/wirte/order/OrderData07Test.java → 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.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Test; 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 * Order data test
* *
@ -19,11 +14,11 @@ public class OrderData07Test {
@Test @Test
public void simple() { public void simple() {
ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(FileUtil.createNewWriteFile("order07.xlsx")) // ExcelWriter writer = EasyExcelFactory.writerBuilder().outputFile(TestFileUtil.createNewWriteFile("order07.xlsx"))
.head(OrderData.class).build(); // .head(OrderData.class).build();
Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build(); // Sheet sheet = EasyExcelFactory.writerSheetBuilder().sheetNo(0).sheetName("order").build();
writer.write(createData(10000 * 100), sheet); // writer.write(createData(10000 * 100), sheet);
writer.finish(); // writer.finish();
} }
private List<OrderData> createData(int count) { private List<OrderData> createData(int count) {

2
src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData.java → 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; import com.alibaba.excel.annotation.ExcelProperty;

16
src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleDataListener.java → 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.ArrayList;
import java.util.List; import java.util.List;
import org.junit.Assert;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -13,19 +14,18 @@ import com.alibaba.fastjson.JSON;
/** /**
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class SimpleDataListener extends AnalysisEventListener<Object> { public class SimpleDataListener extends AnalysisEventListener<SimpleData> {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class); private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class);
List<SimpleData> list = new ArrayList<SimpleData>();
List<Object> list = new ArrayList<Object>();
@Override @Override
public void invoke(Object object, AnalysisContext context) { public void invoke(SimpleData data, AnalysisContext context) {
list.add(object); list.add(data);
LOGGER.info("data:{}", JSON.toJSONString(object));
} }
@Override @Override
public void doAfterAllAnalysed(AnalysisContext context) { public void doAfterAllAnalysed(AnalysisContext context) {
assert list.size() == 10; Assert.assertEquals(list.size(), 10);
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
} }
} }

22
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();
}
}

40
src/test/java/com/alibaba/easyexcel/test/ehcache/EncacheTest.java vendored

@ -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<Integer, String> cache = persistentCacheManager.getCache("cache", Integer.class, String.class);
cache.put(1, "测试1");
LOGGER.info("cache:{}", cache.get(1));
persistentCacheManager.close();
LOGGER.info("close");
POITempFile.delete(file);
}
}

189
src/test/java/com/alibaba/easyexcel/test/read/ReadTest.java

@ -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<Object> 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<Object> 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<Sheet> 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<Object> 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<Object> 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<Sheet> 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<Object> datas){
int i=0;
for (Object ob:datas) {
System.out.println(i++);
System.out.println(ob);
}
}
}

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

@ -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<Object> read(InputStream in, Sheet sheet) {
// final List<Object> rows = new ArrayList<Object>();
// new ExcelReader(in, null, new AnalysisEventListener<Object>() {
// @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");
}
}

29
src/test/java/com/alibaba/easyexcel/test/read/large/LargeDataListener.java

@ -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<Object> {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class);
private int count = 0;
@Override
public void invoke(Object object, AnalysisContext context) {
count++;
if (count % 100000 == 0) {
LOGGER.info("row:{} ,mem:{},data:{}", count, Runtime.getRuntime().totalMemory(), JSON.toJSONString(object));
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {}
}

26
src/test/java/com/alibaba/easyexcel/test/read/simple/SimpleData07Test.java

@ -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();
}
}

8
src/test/java/com/alibaba/easyexcel/test/util/FileUtil.java → 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.File;
import java.io.InputStream; import java.io.InputStream;
public class FileUtil { public class TestFileUtil {
public static InputStream getResourcesFileInputStream(String fileName) { public static InputStream getResourcesFileInputStream(String fileName) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName); return Thread.currentThread().getContextClassLoader().getResourceAsStream("" + fileName);
} }
public static InputStream readFile(String fileName) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream("read/" + fileName);
}
public static String getPath() { public static String getPath() {
return FileUtil.class.getResource("/").getPath(); return TestFileUtil.class.getResource("/").getPath();
} }
public static File createNewFile(String pathName) { public static File createNewFile(String pathName) {

13
src/test/java/com/alibaba/easyexcel/test/wirte/WriteTest.java

@ -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 {
}
}

40
src/test/java/com/alibaba/easyexcel/test/wirte/nohead/NoHeadData07Test.java

@ -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<OrderData> createData(int count) {
List<OrderData> list = new ArrayList<OrderData>();
for (int i = 0; i < count; i++) {
OrderData orderData = new OrderData();
orderData.setIndex1("排序1:" + i);
orderData.setIndex10("排序10:" + i);
list.add(orderData);
}
return list;
}
}

16
src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData.java

@ -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;
}

17
src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData03Test.java

@ -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() {
}
}

49
src/test/java/com/alibaba/easyexcel/test/wirte/simple/SimpleData07Test.java

@ -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<SimpleData> createData(int count) {
List<SimpleData> list = new ArrayList<SimpleData>();
for (int i = 0; i < count; i++) {
SimpleData simpleData = new SimpleData();
simpleData.setString1("一号字" + i);
simpleData.setString2("二号字" + i);
list.add(simpleData);
}
return list;
}
}

0
src/test/resources/read/large/large07.xlsx → src/test/resources/large/large07.xlsx

2
src/test/resources/logback.xml

@ -7,7 +7,7 @@
<pattern>${LOG_PATTERN}</pattern> <pattern>${LOG_PATTERN}</pattern>
</encoder> </encoder>
</appender> </appender>
<root level="INFO"> <root level="DEBUG">
<appender-ref ref="STDOUT"/> <appender-ref ref="STDOUT"/>
</root> </root>
</configuration> </configuration>

0
src/test/resources/read/simple/simple07.xlsx → src/test/resources/simple/simple07Test.xlsx

Loading…
Cancel
Save