Browse Source

优化读写逻辑

pull/507/head
zhuangjiaju 5 years ago
parent
commit
99f041e9cd
  1. 15
      README.md
  2. 8
      src/main/java/com/alibaba/excel/EasyExcel.java
  3. 18
      src/main/java/com/alibaba/excel/EasyExcelFactory.java
  4. 14
      src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java
  5. 9
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  6. 9
      src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java
  7. 8
      src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java
  8. 8
      src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataTest.java
  9. 4
      src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityDataTest.java
  10. 18
      src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityParameterDataTest.java
  11. 11
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
  12. 8
      src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java
  13. 6
      src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java
  14. 6
      src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java
  15. 7
      src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java
  16. 6
      src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
  17. 58
      src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java
  18. 20
      src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java
  19. 12
      src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java
  20. 16
      src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java
  21. 18
      src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java
  22. 31
      src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java
  23. 49
      src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java
  24. 59
      src/test/java/com/alibaba/easyexcel/test/demo/read/CustomStringStringConverter.java
  25. 97
      src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
  26. 5
      src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java
  27. 17
      src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  28. BIN
      src/test/resources/demo/demo.xlsx

15
README.md

@ -45,8 +45,8 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
@Test @Test
public void simpleRead() { public void simpleRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 然后千万别忘记 finish // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcelFactory.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead().finish(); EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
} }
``` ```
@ -60,10 +60,10 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
*/ */
@Test @Test
public void simpleWrite() { public void simpleWrite() {
String fileName = TestFileUtil.getPath() + "write.xlsx"; String fileName = TestFileUtil.getPath() + "write" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后千万别忘记 finish // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可 // 如果这里想使用03 则 传入excelType参数即可
EasyExcelFactory.write(fileName, DemoData.class).sheet("模板").doWrite(data()).finish(); EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());
} }
``` ```
@ -81,7 +81,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
response.setContentType("application/vnd.ms-excel"); response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
EasyExcelFactory.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()).finish(); EasyExcel.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());
} }
/** /**
@ -93,8 +93,7 @@ DEMO代码地址:[https://github.com/alibaba/easyexcel/blob/master/src/test/ja
@PostMapping("upload") @PostMapping("upload")
@ResponseBody @ResponseBody
public String upload(MultipartFile file) throws IOException { public String upload(MultipartFile file) throws IOException {
EasyExcelFactory.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead() EasyExcel.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead();
.finish();
return "success"; return "success";
} }
``` ```

8
src/main/java/com/alibaba/excel/EasyExcel.java

@ -0,0 +1,8 @@
package com.alibaba.excel;
/**
* This is actually {@link EasyExcelFactory}, and it's nice to have a short name
*
* @author jipengfei
*/
public class EasyExcel extends EasyExcelFactory {}

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

@ -47,7 +47,7 @@ public class EasyExcelFactory {
* @param sheet * @param sheet
* read sheet. * read sheet.
* @return analysis result. * @return analysis result.
* @deprecated please use 'EasyExcelFactory.read(in).sheet(sheetNo).doReadSync();' * @deprecated please use 'EasyExcel.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) {
@ -73,7 +73,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(in,head,listener).sheet(sheetNo).doRead().finish();' * @deprecated please use 'EasyExcel.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) {
@ -88,7 +88,7 @@ public class EasyExcelFactory {
* @param listener * @param listener
* Callback method after each row is parsed. * Callback method after each row is parsed.
* @return ExcelReader. * @return ExcelReader.
* @deprecated please use {@link EasyExcelFactory#read()} build 'ExcelReader' * @deprecated please use {@link EasyExcel#read()} build 'ExcelReader'
*/ */
@Deprecated @Deprecated
public static ExcelReader getReader(InputStream in, AnalysisEventListener listener) { public static ExcelReader getReader(InputStream in, AnalysisEventListener listener) {
@ -101,7 +101,7 @@ public class EasyExcelFactory {
* @param outputStream * @param outputStream
* the java OutputStream you wish to write the value to. * the java OutputStream you wish to write the value to.
* @return new ExcelWriter. * @return new ExcelWriter.
* @deprecated please use {@link EasyExcelFactory#write()} * @deprecated please use {@link EasyExcel#write()}
*/ */
@Deprecated @Deprecated
public static ExcelWriter getWriter(OutputStream outputStream) { public static ExcelWriter getWriter(OutputStream outputStream) {
@ -118,7 +118,7 @@ public class EasyExcelFactory {
* @param needHead * @param needHead
* Do you need to write the header to the file? * Do you need to write the header to the file?
* @return new ExcelWriter * @return new ExcelWriter
* @deprecated please use {@link EasyExcelFactory#write()} * @deprecated please use {@link EasyExcel#write()}
*/ */
@Deprecated @Deprecated
public static ExcelWriter getWriter(OutputStream outputStream, ExcelTypeEnum typeEnum, boolean needHead) { public static ExcelWriter getWriter(OutputStream outputStream, ExcelTypeEnum typeEnum, boolean needHead) {
@ -137,7 +137,7 @@ public class EasyExcelFactory {
* @param typeEnum * @param typeEnum
* 03 or 07 * 03 or 07
* @return new ExcelWriter * @return new ExcelWriter
* @deprecated please use {@link EasyExcelFactory#write()} * @deprecated please use {@link EasyExcel#write()}
*/ */
@Deprecated @Deprecated
public static ExcelWriter getWriterWithTemp(InputStream temp, OutputStream outputStream, ExcelTypeEnum typeEnum, public static ExcelWriter getWriterWithTemp(InputStream temp, OutputStream outputStream, ExcelTypeEnum typeEnum,
@ -160,7 +160,7 @@ public class EasyExcelFactory {
* @param handler * @param handler
* User-defined callback * User-defined callback
* @return new ExcelWriter * @return new ExcelWriter
* @deprecated please use {@link EasyExcelFactory#write()} * @deprecated please use {@link EasyExcel#write()}
*/ */
@Deprecated @Deprecated
public static ExcelWriter getWriterWithTempAndHandler(InputStream temp, OutputStream outputStream, public static ExcelWriter getWriterWithTempAndHandler(InputStream temp, OutputStream outputStream,
@ -300,7 +300,7 @@ public class EasyExcelFactory {
if (sheetName != null) { if (sheetName != null) {
excelWriterSheetBuilder.sheetName(sheetName); excelWriterSheetBuilder.sheetName(sheetName);
} }
return new ExcelWriterSheetBuilder(); return excelWriterSheetBuilder;
} }
/** /**
@ -502,6 +502,6 @@ public class EasyExcelFactory {
if (sheetName != null) { if (sheetName != null) {
excelReaderSheetBuilder.sheetName(sheetName); excelReaderSheetBuilder.sheetName(sheetName);
} }
return new ExcelReaderSheetBuilder(); return excelReaderSheetBuilder;
} }
} }

14
src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java

@ -152,23 +152,11 @@ public class ExcelReaderSheetBuilder {
* *
* @return * @return
*/ */
public ExcelReaderSheetBuilder doRead() { public void doRead() {
if (excelReader == null) { if (excelReader == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method"); throw new ExcelGenerateException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
} }
excelReader.read(build()); excelReader.read(build());
return new ExcelReaderSheetBuilder(excelReader);
}
/**
* Sax read
*
* @return
*/
public void finish() {
if (excelReader == null) {
throw new ExcelAnalysisException("Must use 'EasyExcelFactory.read().sheet()' to call this method");
}
excelReader.finish(); excelReader.finish();
} }

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

@ -127,18 +127,11 @@ public class ExcelWriterSheetBuilder {
return writeSheet; return writeSheet;
} }
public ExcelWriterSheetBuilder doWrite(List data) { public void doWrite(List data) {
if (excelWriter == null) { if (excelWriter == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method"); throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method");
} }
excelWriter.write(data, build()); excelWriter.write(data, build());
return new ExcelWriterSheetBuilder(excelWriter);
}
public void finish() {
if (excelWriter == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method");
}
excelWriter.finish(); excelWriter.finish();
} }

9
src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java

@ -121,18 +121,11 @@ public class ExcelWriterTableBuilder {
return writeTable; return writeTable;
} }
public ExcelWriterTableBuilder doWrite(List data) { public void doWrite(List data) {
if (excelWriter == null) { if (excelWriter == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet().table()' to call this method"); throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet().table()' to call this method");
} }
excelWriter.write(data, writeSheet, build()); excelWriter.write(data, writeSheet, build());
return new ExcelWriterTableBuilder(excelWriter, writeSheet);
}
public void finish() {
if (excelWriter == null) {
throw new ExcelGenerateException("Must use 'EasyExcelFactory.write().sheet()' to call this method");
}
excelWriter.finish(); excelWriter.finish();
} }

8
src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataTest.java

@ -8,7 +8,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
/** /**
@ -37,9 +37,9 @@ public class AnnotationDataTest {
} }
private void readAndWrite(File file) throws Exception { private void readAndWrite(File file) throws Exception {
EasyExcelFactory.write().file(file).head(AnnotationData.class).sheet().doWrite(data()).finish(); EasyExcel.write().file(file).head(AnnotationData.class).sheet().doWrite(data());
EasyExcelFactory.read().file(file).head(AnnotationData.class).registerReadListener(new AnnotationDataListener()) EasyExcel.read().file(file).head(AnnotationData.class).registerReadListener(new AnnotationDataListener())
.sheet().doRead().finish(); .sheet().doRead();
} }
private List<AnnotationData> data() throws Exception { private List<AnnotationData> data() throws Exception {

8
src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataTest.java

@ -8,7 +8,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
/** /**
* Annotation data test * Annotation data test
@ -37,9 +37,9 @@ public class AnnotationIndexAndNameDataTest {
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
EasyExcelFactory.write(file, AnnotationIndexAndNameData.class).sheet().doWrite(data()).finish(); EasyExcel.write(file, AnnotationIndexAndNameData.class).sheet().doWrite(data());
EasyExcelFactory.read(file, AnnotationIndexAndNameData.class, new AnnotationIndexAndNameDataListener()).sheet() EasyExcel.read(file, AnnotationIndexAndNameData.class, new AnnotationIndexAndNameDataListener()).sheet()
.doRead().finish(); .doRead();
} }
private List<AnnotationIndexAndNameData> data() { private List<AnnotationIndexAndNameData> data() {

4
src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityDataTest.java

@ -16,7 +16,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Font; import com.alibaba.excel.metadata.Font;
@ -91,7 +91,7 @@ public class CompatibilityDataTest {
out.close(); out.close();
InputStream inputStream = new FileInputStream(file); InputStream inputStream = new FileInputStream(file);
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(1, 1)); List<Object> data = EasyExcel.read(inputStream, new Sheet(1, 1));
Assert.assertEquals(data.size(), 1); Assert.assertEquals(data.size(), 1);
List<String> dataList = (List<String>)data.get(0); List<String> dataList = (List<String>)data.get(0);
Assert.assertEquals(dataList.get(0), "字符串00"); Assert.assertEquals(dataList.get(0), "字符串00");

18
src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityParameterDataTest.java

@ -12,7 +12,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
@ -42,7 +42,7 @@ public class CompatibilityParameterDataTest {
private void readAndWrite1(File file) throws Exception { private void readAndWrite1(File file) throws Exception {
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
ExcelWriter writer = EasyExcelFactory.getWriter(out); ExcelWriter writer = EasyExcel.getWriter(out);
Sheet sheet1 = new Sheet(1, 0); Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("第一个sheet"); sheet1.setSheetName("第一个sheet");
writer.write0(data(), sheet1); writer.write0(data(), sheet1);
@ -50,13 +50,13 @@ public class CompatibilityParameterDataTest {
out.close(); out.close();
InputStream inputStream = new FileInputStream(file); InputStream inputStream = new FileInputStream(file);
EasyExcelFactory.readBySax(inputStream, new Sheet(1, 0), new CompatibilityDataListener()); EasyExcel.readBySax(inputStream, new Sheet(1, 0), new CompatibilityDataListener());
inputStream.close(); inputStream.close();
} }
private void readAndWrite2(File file) throws Exception { private void readAndWrite2(File file) throws Exception {
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
ExcelWriter writer = EasyExcelFactory.getWriter(out, null, false); ExcelWriter writer = EasyExcel.getWriter(out, null, false);
Sheet sheet1 = new Sheet(1, 0); Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("第一个sheet"); sheet1.setSheetName("第一个sheet");
writer.write0(data(), sheet1); writer.write0(data(), sheet1);
@ -64,7 +64,7 @@ public class CompatibilityParameterDataTest {
out.close(); out.close();
InputStream inputStream = new FileInputStream(file); InputStream inputStream = new FileInputStream(file);
EasyExcelFactory.readBySax(inputStream, new Sheet(1, 0), new CompatibilityDataListener()); EasyExcel.readBySax(inputStream, new Sheet(1, 0), new CompatibilityDataListener());
inputStream.close(); inputStream.close();
} }
@ -101,7 +101,7 @@ public class CompatibilityParameterDataTest {
private void readAndWrite5(File file) throws Exception { private void readAndWrite5(File file) throws Exception {
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(null, out, null, false); ExcelWriter writer = EasyExcel.getWriterWithTemp(null, out, null, false);
Sheet sheet1 = new Sheet(1, 0); Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("第一个sheet"); sheet1.setSheetName("第一个sheet");
writer.write0(data(), sheet1, null); writer.write0(data(), sheet1, null);
@ -109,14 +109,14 @@ public class CompatibilityParameterDataTest {
out.close(); out.close();
InputStream inputStream = new FileInputStream(file); InputStream inputStream = new FileInputStream(file);
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, new CompatibilityDataListener()); ExcelReader excelReader = EasyExcel.getReader(inputStream, new CompatibilityDataListener());
excelReader.read(new Sheet(1, 0)); excelReader.read(new Sheet(1, 0));
inputStream.close(); inputStream.close();
} }
private void readAndWrite6(File file) throws Exception { private void readAndWrite6(File file) throws Exception {
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
ExcelWriter writer = EasyExcelFactory.getWriterWithTempAndHandler(null, out, null, false, null); ExcelWriter writer = EasyExcel.getWriterWithTempAndHandler(null, out, null, false, null);
Sheet sheet1 = new Sheet(1, 0); Sheet sheet1 = new Sheet(1, 0);
sheet1.setSheetName("第一个sheet"); sheet1.setSheetName("第一个sheet");
writer.write0(data(), sheet1, null); writer.write0(data(), sheet1, null);
@ -124,7 +124,7 @@ public class CompatibilityParameterDataTest {
out.close(); out.close();
InputStream inputStream = new FileInputStream(file); InputStream inputStream = new FileInputStream(file);
ExcelReader excelReader = EasyExcelFactory.getReader(inputStream, new CompatibilityDataListener()); ExcelReader excelReader = EasyExcel.getReader(inputStream, new CompatibilityDataListener());
excelReader.read(new Sheet(1, 0)); excelReader.read(new Sheet(1, 0));
inputStream.close(); inputStream.close();
} }

11
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java

@ -11,7 +11,8 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
@ -42,8 +43,8 @@ public class ConverterDataTest {
} }
private void readAndWrite(File file) throws Exception { private void readAndWrite(File file) throws Exception {
EasyExcelFactory.write(file, ConverterData.class).sheet().doWrite(data()).finish(); EasyExcel.write(file, ConverterData.class).sheet().doWrite(data());
EasyExcelFactory.read(file, ConverterData.class, new ConverterDataListener()).sheet().doRead().finish(); EasyExcel.read(file, ConverterData.class, new ConverterDataListener()).sheet().doRead();
} }
@Test @Test
@ -57,9 +58,9 @@ public class ConverterDataTest {
} }
private void readAllConverter(String fileName) { private void readAllConverter(String fileName) {
EasyExcelFactory EasyExcel
.read(TestFileUtil.readFile(fileName), ReadAllConverterData.class, new ReadAllConverterDataListener()) .read(TestFileUtil.readFile(fileName), ReadAllConverterData.class, new ReadAllConverterDataListener())
.sheet().doRead().finish(); .sheet().doRead();
} }
private List<ConverterData> data() throws Exception { private List<ConverterData> data() throws Exception {

8
src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataTest.java

@ -12,7 +12,7 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
/** /**
* *
@ -41,9 +41,9 @@ public class ExceptionDataTest {
} }
private void readAndWrite(File file) throws Exception { private void readAndWrite(File file) throws Exception {
EasyExcelFactory.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data()).finish(); EasyExcel.write(new FileOutputStream(file), ExceptionData.class).sheet().doWrite(data());
EasyExcelFactory.read(new FileInputStream(file), ExceptionData.class, new ExceptionDataListener()).sheet() EasyExcel.read(new FileInputStream(file), ExceptionData.class, new ExceptionDataListener()).sheet()
.doRead().finish(); .doRead();
} }
private List<ExceptionData> data() { private List<ExceptionData> data() {

6
src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java

@ -8,7 +8,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
/** /**
* *
@ -36,8 +36,8 @@ public class ComplexHeadDataTest {
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
EasyExcelFactory.write(file, ComplexHeadData.class).sheet().doWrite(data()).finish(); EasyExcel.write(file, ComplexHeadData.class).sheet().doWrite(data());
EasyExcelFactory.read(file, ComplexHeadData.class, new ComplexDataListener()).sheet().doRead().finish(); EasyExcel.read(file, ComplexHeadData.class, new ComplexDataListener()).sheet().doRead();
} }
private List<ComplexHeadData> data() { private List<ComplexHeadData> data() {

6
src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java

@ -9,7 +9,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
/** /**
@ -38,8 +38,8 @@ public class ListHeadDataTest {
} }
private void readAndWrite(File file) throws Exception { private void readAndWrite(File file) throws Exception {
EasyExcelFactory.write(file).head(head()).sheet().doWrite(data()).finish(); EasyExcel.write(file).head(head()).sheet().doWrite(data());
EasyExcelFactory.read(file).registerReadListener(new ListHeadDataListener()).sheet().doRead().finish(); EasyExcel.read(file).registerReadListener(new ListHeadDataListener()).sheet().doRead();
} }
private List<List<String>> head() { private List<List<String>> head() {

7
src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java

@ -8,7 +8,7 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
/** /**
* *
@ -36,9 +36,8 @@ public class NoHeadDataTest {
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
EasyExcelFactory.write(file, NoHeadData.class).needHead(Boolean.FALSE).sheet().doWrite(data()).finish(); EasyExcel.write(file, NoHeadData.class).needHead(Boolean.FALSE).sheet().doWrite(data());
EasyExcelFactory.read(file, NoHeadData.class, new NoHeadDataListener()).headRowNumber(0).sheet().doRead() EasyExcel.read(file, NoHeadData.class, new NoHeadDataListener()).headRowNumber(0).sheet().doRead();
.finish();
} }
private List<NoHeadData> data() { private List<NoHeadData> data() {

6
src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java

@ -7,7 +7,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
/** /**
* *
@ -19,8 +19,8 @@ public class LargeDataTest {
@Test @Test
public void read() { public void read() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
EasyExcelFactory.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class, EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class,
new LargeDataListener()).headRowNumber(2).sheet().doRead().finish(); new LargeDataListener()).headRowNumber(2).sheet().doRead();
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
} }
} }

58
src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataTest.java

@ -11,7 +11,7 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.cache.MapCache; import com.alibaba.excel.cache.MapCache;
@ -46,81 +46,79 @@ public class ParameterDataTest {
} }
private void readAndWrite1() { private void readAndWrite1() {
EasyExcelFactory.write(file.getPath()).head(ParameterData.class).sheet().doWrite(data()).finish(); EasyExcel.write(file.getPath()).head(ParameterData.class).sheet().doWrite(data());
EasyExcelFactory.read(file.getPath()).head(ParameterData.class) EasyExcel.read(file.getPath()).head(ParameterData.class)
.registerReadListener(new ParameterDataListener()).sheet().doRead().finish(); .registerReadListener(new ParameterDataListener()).sheet().doRead();
} }
private void readAndWrite2() { private void readAndWrite2() {
EasyExcelFactory.write(file.getPath(), ParameterData.class).sheet().doWrite(data()).finish(); EasyExcel.write(file.getPath(), ParameterData.class).sheet().doWrite(data());
EasyExcelFactory.read(file.getPath(), ParameterData.class, new ParameterDataListener()).sheet().doRead() EasyExcel.read(file.getPath(), ParameterData.class, new ParameterDataListener()).sheet().doRead();
.finish();
} }
private void readAndWrite3() throws Exception { private void readAndWrite3() throws Exception {
EasyExcelFactory.write(new FileOutputStream(file)).head(ParameterData.class).sheet().doWrite(data()).finish(); EasyExcel.write(new FileOutputStream(file)).head(ParameterData.class).sheet().doWrite(data());
EasyExcelFactory.read(file.getPath()).head(ParameterData.class) EasyExcel.read(file.getPath()).head(ParameterData.class)
.registerReadListener(new ParameterDataListener()).sheet().doRead().finish(); .registerReadListener(new ParameterDataListener()).sheet().doRead();
} }
private void readAndWrite4() throws Exception { private void readAndWrite4() throws Exception {
EasyExcelFactory.write(new FileOutputStream(file), ParameterData.class).sheet().doWrite(data()).finish(); EasyExcel.write(new FileOutputStream(file), ParameterData.class).sheet().doWrite(data());
EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).sheet().doRead() EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class).sheet().doRead();
.finish();
} }
private void readAndWrite5() throws Exception { private void readAndWrite5() throws Exception {
ExcelWriter excelWriter = EasyExcelFactory.write(new FileOutputStream(file)).head(ParameterData.class) ExcelWriter excelWriter = EasyExcel.write(new FileOutputStream(file)).head(ParameterData.class)
.relativeHeadRowIndex(0).build(); .relativeHeadRowIndex(0).build();
WriteSheet writeSheet = EasyExcelFactory.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build(); WriteSheet writeSheet = EasyExcel.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build();
WriteTable writeTable = EasyExcelFactory.writerTable(0).relativeHeadRowIndex(0).needHead(Boolean.TRUE).build(); WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).needHead(Boolean.TRUE).build();
excelWriter.write(data(), writeSheet, writeTable); excelWriter.write(data(), writeSheet, writeTable);
excelWriter.finish(); excelWriter.finish();
ExcelReader excelReader = ExcelReader excelReader =
EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class)
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); .mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
ReadSheet readSheet = EasyExcelFactory.readSheet().head(ParameterData.class).use1904windowing(Boolean.FALSE) ReadSheet readSheet = EasyExcel.readSheet().head(ParameterData.class).use1904windowing(Boolean.FALSE)
.headRowNumber(1).sheetNo(0).sheetName("0").build(); .headRowNumber(1).sheetNo(0).sheetName("0").build();
excelReader.read(readSheet); excelReader.read(readSheet);
excelReader.finish(); excelReader.finish();
excelReader = EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) excelReader = EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class)
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); .mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
excelReader.read(); excelReader.read();
excelReader.finish(); excelReader.finish();
} }
private void readAndWrite6() throws Exception { private void readAndWrite6() throws Exception {
ExcelWriter excelWriter = EasyExcelFactory.write(new FileOutputStream(file)).head(ParameterData.class) ExcelWriter excelWriter = EasyExcel.write(new FileOutputStream(file)).head(ParameterData.class)
.relativeHeadRowIndex(0).build(); .relativeHeadRowIndex(0).build();
WriteSheet writeSheet = EasyExcelFactory.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build(); WriteSheet writeSheet = EasyExcel.writerSheet(0).relativeHeadRowIndex(0).needHead(Boolean.FALSE).build();
WriteTable writeTable = EasyExcelFactory.writerTable(0).registerConverter(new StringStringConverter()) WriteTable writeTable = EasyExcel.writerTable(0).registerConverter(new StringStringConverter())
.relativeHeadRowIndex(0).needHead(Boolean.TRUE).build(); .relativeHeadRowIndex(0).needHead(Boolean.TRUE).build();
excelWriter.write(data(), writeSheet, writeTable); excelWriter.write(data(), writeSheet, writeTable);
excelWriter.finish(); excelWriter.finish();
ExcelReader excelReader = ExcelReader excelReader =
EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class)
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); .mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
ReadSheet readSheet = EasyExcelFactory.readSheet("0").head(ParameterData.class).use1904windowing(Boolean.FALSE) ReadSheet readSheet = EasyExcel.readSheet("0").head(ParameterData.class).use1904windowing(Boolean.FALSE)
.headRowNumber(1).sheetNo(0).build(); .headRowNumber(1).sheetNo(0).build();
excelReader.read(readSheet); excelReader.read(readSheet);
excelReader.finish(); excelReader.finish();
excelReader = EasyExcelFactory.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class) excelReader = EasyExcel.read(file.getPath(), new ParameterDataListener()).head(ParameterData.class)
.mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build(); .mandatoryUseInputStream(Boolean.FALSE).autoCloseStream(Boolean.TRUE).readCache(new MapCache()).build();
excelReader.read(); excelReader.read();
excelReader.finish(); excelReader.finish();
} }
private void readAndWrite7() throws Exception { private void readAndWrite7() {
EasyExcelFactory.write(file, ParameterData.class).registerConverter(new StringStringConverter()).sheet() EasyExcel.write(file, ParameterData.class).registerConverter(new StringStringConverter()).sheet()
.registerConverter(new StringStringConverter()).needHead(Boolean.FALSE).table(0).needHead(Boolean.TRUE) .registerConverter(new StringStringConverter()).needHead(Boolean.FALSE).table(0).needHead(Boolean.TRUE)
.doWrite(data()).finish(); .doWrite(data());
EasyExcelFactory.read(file.getPath()).head(ParameterData.class) EasyExcel.read(file.getPath()).head(ParameterData.class)
.registerReadListener(new ParameterDataListener()).sheet().registerConverter(new StringStringConverter()) .registerReadListener(new ParameterDataListener()).sheet().registerConverter(new StringStringConverter())
.doRead().finish(); .doRead();
} }
private List<ParameterData> data() { private List<ParameterData> data() {

20
src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java

@ -10,7 +10,7 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
@ -48,12 +48,12 @@ public class RepetitionDataTest {
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
ExcelWriter excelWriter = EasyExcelFactory.write(file, RepetitionData.class).build(); ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build();
WriteSheet writeSheet = EasyExcelFactory.writerSheet(0).build(); WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
excelWriter.write(data(), writeSheet).write(data(), writeSheet).finish(); excelWriter.write(data(), writeSheet).write(data(), writeSheet).finish();
ExcelReader excelReader = ExcelReader excelReader =
EasyExcelFactory.read(file, RepetitionData.class, new RepetitionDataListener()).build(); EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build();
ReadSheet readSheet = EasyExcelFactory.readSheet(0).build(); ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet).finish(); excelReader.read(readSheet).finish();
} }
@ -68,13 +68,13 @@ public class RepetitionDataTest {
} }
private void readAndWriteTable(File file) { private void readAndWriteTable(File file) {
ExcelWriter excelWriter = EasyExcelFactory.write(file, RepetitionData.class).build(); ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build();
WriteSheet writeSheet = EasyExcelFactory.writerSheet(0).build(); WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
WriteTable writeTable = EasyExcelFactory.writerTable(0).relativeHeadRowIndex(0).build(); WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).build();
excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable).finish(); excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable).finish();
ExcelReader excelReader = ExcelReader excelReader =
EasyExcelFactory.read(file, RepetitionData.class, new RepetitionDataListener()).build(); EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build();
ReadSheet readSheet = EasyExcelFactory.readSheet(0).headRowNumber(2).build(); ReadSheet readSheet = EasyExcel.readSheet(0).headRowNumber(2).build();
excelReader.read(readSheet).finish(); excelReader.read(readSheet).finish();
} }

12
src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java

@ -11,7 +11,7 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
/** /**
* *
@ -40,8 +40,8 @@ public class SimpleDataTest {
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
EasyExcelFactory.write(file, SimpleData.class).sheet().doWrite(data()).finish(); EasyExcel.write(file, SimpleData.class).sheet().doWrite(data());
EasyExcelFactory.read(file, SimpleData.class, new SimpleDataListener()).sheet().doRead().finish(); EasyExcel.read(file, SimpleData.class, new SimpleDataListener()).sheet().doRead();
} }
@Test @Test
@ -56,13 +56,13 @@ public class SimpleDataTest {
@Test @Test
public void T05SheetNameRead07() { public void T05SheetNameRead07() {
EasyExcelFactory.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class, EasyExcel.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class,
new SimpleDataSheetNameListener()).sheet("simple").doRead().finish(); new SimpleDataSheetNameListener()).sheet("simple").doRead();
} }
private void synchronousRead(File file) { private void synchronousRead(File file) {
// Synchronous read file // Synchronous read file
List<Object> list = EasyExcelFactory.read(file).head(SimpleData.class).sheet().doReadSync(); List<Object> list = EasyExcel.read(file).head(SimpleData.class).sheet().doReadSync();
Assert.assertEquals(list.size(), 10); Assert.assertEquals(list.size(), 10);
Assert.assertTrue(list.get(0) instanceof SimpleData); Assert.assertTrue(list.get(0) instanceof SimpleData);
Assert.assertEquals(((SimpleData)list.get(0)).getName(), "姓名0"); Assert.assertEquals(((SimpleData)list.get(0)).getName(), "姓名0");

16
src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataTest.java

@ -16,7 +16,7 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.LoopMergeStrategy; import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy; import com.alibaba.excel.write.merge.OnceAbsoluteMergeStrategy;
@ -107,14 +107,14 @@ public class StyleDataTest {
return writeCellStyle; return writeCellStyle;
} }
}; };
EasyExcelFactory.write(file07, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet() EasyExcel.write(file07, StyleData.class).registerWriteHandler(verticalCellStyleStrategy).sheet()
.doWrite(data()).finish(); .doWrite(data());
} }
@Test @Test
public void T04LoopMergeStrategy() { public void T04LoopMergeStrategy() {
EasyExcelFactory.write(file07, StyleData.class).sheet().registerWriteHandler(new LoopMergeStrategy(2, 1)) EasyExcel.write(file07, StyleData.class).sheet().registerWriteHandler(new LoopMergeStrategy(2, 1))
.doWrite(data10()).finish(); .doWrite(data10());
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
@ -137,10 +137,10 @@ public class StyleDataTest {
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle); new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1); OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
EasyExcelFactory.write(file, StyleData.class).registerWriteHandler(simpleColumnWidthStyleStrategy) EasyExcel.write(file, StyleData.class).registerWriteHandler(simpleColumnWidthStyleStrategy)
.registerWriteHandler(simpleRowHeightStyleStrategy).registerWriteHandler(horizontalCellStyleStrategy) .registerWriteHandler(simpleRowHeightStyleStrategy).registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(onceAbsoluteMergeStrategy).sheet().doWrite(data()).finish(); .registerWriteHandler(onceAbsoluteMergeStrategy).sheet().doWrite(data());
EasyExcelFactory.read(file, StyleData.class, new StyleDataListener()).sheet().doRead().finish(); EasyExcel.read(file, StyleData.class, new StyleDataListener()).sheet().doRead();
} }
private List<StyleData> data() { private List<StyleData> data() {

18
src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataTest.java

@ -10,7 +10,7 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcel;
/** /**
* *
@ -39,19 +39,17 @@ public class TemplateDataTest {
} }
private void readAndWrite07(File file) { private void readAndWrite07(File file) {
EasyExcelFactory.write(file, TemplateData.class) EasyExcel.write(file, TemplateData.class)
.withTemplate(TestFileUtil.readFile("template" + File.separator + "template07.xlsx")).sheet() .withTemplate(TestFileUtil.readFile("template" + File.separator + "template07.xlsx")).sheet()
.doWrite(data()).finish(); .doWrite(data());
EasyExcelFactory.read(file, TemplateData.class, new TemplateDataListener()).headRowNumber(2).sheet().doRead() EasyExcel.read(file, TemplateData.class, new TemplateDataListener()).headRowNumber(2).sheet().doRead();
.finish();
} }
private void readAndWrite03(File file) { private void readAndWrite03(File file) {
EasyExcelFactory.write(file, TemplateData.class) EasyExcel.write(file, TemplateData.class)
.withTemplate(TestFileUtil.readFile("template" + File.separator + "template03.xls")).sheet().doWrite(data()) .withTemplate(TestFileUtil.readFile("template" + File.separator + "template03.xls")).sheet()
.finish(); .doWrite(data());
EasyExcelFactory.read(file, TemplateData.class, new TemplateDataListener()).headRowNumber(2).sheet().doRead() EasyExcel.read(file, TemplateData.class, new TemplateDataListener()).headRowNumber(2).sheet().doRead();
.finish();
} }
private List<TemplateData> data() { private List<TemplateData> data() {

31
src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterData.java

@ -0,0 +1,31 @@
package com.alibaba.easyexcel.test.demo.read;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import lombok.Data;
/**
* 基础数据类.这里的排序和excel里面的排序一致
*
* @author Jiaju Zhuang
**/
@Data
public class ConverterData {
/**
* 我自定义 转换器不管数据库传过来什么 我给他加上自定义
*/
@ExcelProperty(converter = CustomStringStringConverter.class)
private String string;
/**
* 这里用string 去接日期才能格式化我想接收年月日格式
*/
@DateTimeFormat("yyyy年MM月dd日HH时mm分ss秒")
private String date;
/**
* 我想接收百分比的数字
*/
@NumberFormat("#.##%")
private String doubleData;
}

49
src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java

@ -0,0 +1,49 @@
package com.alibaba.easyexcel.test.demo.read;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
/**
* 模板的读取类
*
* @author Jiaju Zhuang
*/
public class ConverterDataListener extends AnalysisEventListener<ConverterData> {
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class);
/**
* 每隔5条存储数据库实际使用中可以3000条然后清理list 方便内存回收
*/
private static final int BATCH_COUNT = 5;
List<ConverterData> list = new ArrayList<ConverterData>();
@Override
public void invoke(ConverterData data, AnalysisContext context) {
LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
list.add(data);
if (list.size() >= BATCH_COUNT) {
saveData();
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
saveData();
LOGGER.info("所有数据解析完成!");
}
/**
* 加上存储数据库
*/
private void saveData() {
LOGGER.info("{}条数据,开始存储数据库!", list.size());
LOGGER.info("存储数据库成功!");
}
}

59
src/test/java/com/alibaba/easyexcel/test/demo/read/CustomStringStringConverter.java

@ -0,0 +1,59 @@
package com.alibaba.easyexcel.test.demo.read;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
/**
* String and string converter
*
* @author Jiaju Zhuang
*/
public class CustomStringStringConverter implements Converter<String> {
@Override
public Class supportJavaTypeKey() {
return String.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
/**
* 这里读的时候会调用
*
* @param cellData
* NotNull
* @param contentProperty
* Nullable
* @param globalConfiguration
* NotNull
* @return
*/
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return "自定义:" + cellData.getStringValue();
}
/**
* 这里是写的时候会调用 不用管
*
* @param value
* NotNull
* @param contentProperty
* Nullable
* @param globalConfiguration
* NotNull
* @return
*/
@Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value);
}
}

97
src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java

@ -1,14 +1,21 @@
package com.alibaba.easyexcel.test.demo.read; package com.alibaba.easyexcel.test.demo.read;
import java.io.File; import java.io.File;
import java.util.List;
import java.util.Map;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.util.TestFileUtil; 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.annotation.ExcelProperty;
import com.alibaba.excel.converters.DefaultConverterLoader;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.fastjson.JSON;
/** /**
* 读的常见写法 * 读的常见写法
@ -17,6 +24,8 @@ import com.alibaba.excel.read.metadata.ReadSheet;
*/ */
@Ignore @Ignore
public class ReadTest { public class ReadTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ReadTest.class);
/** /**
* 最简单的读 * 最简单的读
* <li>1. 创建excel对应的实体对象 参照{@link DemoData} * <li>1. 创建excel对应的实体对象 参照{@link DemoData}
@ -27,14 +36,15 @@ public class ReadTest {
public void simpleRead() { public void simpleRead() {
// 写法1: // 写法1:
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 然后千万别忘记 finish // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
EasyExcelFactory.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead().finish(); EasyExcelFactory.read(fileName, DemoData.class, new DemoDataListener()).sheet().doRead();
// 写法2: // 写法2:
fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
ExcelReader excelReader = EasyExcelFactory.read(fileName, DemoData.class, new DemoDataListener()).build(); ExcelReader excelReader = EasyExcelFactory.read(fileName, DemoData.class, new DemoDataListener()).build();
ReadSheet readSheet = EasyExcelFactory.readSheet(0).build(); ReadSheet readSheet = EasyExcelFactory.readSheet(0).build();
excelReader.read(readSheet); excelReader.read(readSheet);
// 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
excelReader.finish(); excelReader.finish();
} }
@ -45,7 +55,86 @@ public class ReadTest {
@Test @Test
public void indexOrNameRead() { public void indexOrNameRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里默认读取第一个sheet 然后千万别忘记 finish // 这里默认读取第一个sheet
EasyExcelFactory.read(fileName, IndexOrNameData.class, new IndexOrNameDataListener()).sheet().doRead().finish(); EasyExcelFactory.read(fileName, IndexOrNameData.class, new IndexOrNameDataListener()).sheet().doRead();
}
/**
* 读多个sheet,这里注意一个sheet不能读取多次一定要多次需要重新读取文件
* <li>1. 创建excel对应的实体对象 参照{@link DemoData}
* <li>2. 由于默认异步读取excel所以需要创建excel一行一行的回调监听器参照{@link DemoDataListener}
* <li>3. 直接读即可
*/
@Test
public void repeatedRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
ExcelReader excelReader = EasyExcelFactory.read(fileName, DemoData.class, new DemoDataListener()).build();
ReadSheet readSheet1 = EasyExcelFactory.readSheet(0).build();
ReadSheet readSheet2 = EasyExcelFactory.readSheet(1).build();
excelReader.read(readSheet1);
excelReader.read(readSheet2);
// 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
excelReader.finish();
}
/**
* 日期数字或者自定义格式转换
* <p>
* 默认读的转换器{@link DefaultConverterLoader#loadDefaultReadConverter()}
* <li>1. 创建excel对应的实体对象 参照{@link ConverterData}.里面可以使用注解.
* <li>2. 由于默认异步读取excel所以需要创建excel一行一行的回调监听器参照{@link ConverterDataListener}
* <li>3. 直接读即可
*/
@Test
public void converterRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 然后千万别忘记 finish
EasyExcelFactory.read(fileName, ConverterData.class, new ConverterDataListener())
// 这里注意 我们也可以registerConverter来指定自定义转换器, 但是这个转换变成全局了, 所有java为string,excel为string的都会用这个转换器。
// 如果就想单个字段使用请使用@ExcelProperty 指定converter
// .registerConverter(new CustomStringStringConverter())
// 读取sheet
.sheet().doRead();
}
/**
* 多行头.
*
* <li>1. 创建excel对应的实体对象 参照{@link DemoData}
* <li>2. 由于默认异步读取excel所以需要创建excel一行一行的回调监听器参照{@link DemoDataListener}
* <li>3. 设置headRowNumber参数然后读 这里要注意headRowNumber如果不指定 会根据你传入的class的{@link ExcelProperty#value()}里面的表头的数量来决定行数
* 如果不传入class则默认为1.当然你指定了headRowNumber不管是否传入class都是以你传入的为准
*/
@Test
public void complexHeaderRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 然后千万别忘记 finish
EasyExcelFactory.read(fileName, DemoData.class, new DemoDataListener()).sheet()
// 这里可以设置1,因为头就是一行。如果多行头,可以设置其他值。不传入也可以,因为默认会根据DemoData 来解析,他没有指定头,也就是默认1行
.headRowNumber(1).doRead();
} }
/**
* 同步的返回不推荐使用如果数据量大会把数据放到内存里面
*/
@Test
public void synchronousRead() {
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 同步读取会自动finish
List<Object> list = EasyExcelFactory.read(fileName).head(DemoData.class).sheet().doReadSync();
for (Object obj : list) {
DemoData data = (DemoData)obj;
LOGGER.info("读取到数据:{}", JSON.toJSONString(data));
}
// 这里 也可以不指定class,返回一个list,然后读取第一个sheet 同步读取会自动finish
list = EasyExcelFactory.read(fileName).sheet().doReadSync();
for (Object obj : list) {
// 返回每条数据的键值对 表示所在的列 和所在列的值
Map<Integer, String> data = (Map<Integer, String>)obj;
LOGGER.info("读取到数据:{}", JSON.toJSONString(data));
}
}
} }

5
src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java

@ -33,7 +33,7 @@ public class WebTest {
response.setContentType("application/vnd.ms-excel"); response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); response.setHeader("Content-disposition", "attachment;filename=demo.xlsx");
EasyExcelFactory.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data()).finish(); EasyExcelFactory.write(response.getOutputStream(), DownloadData.class).sheet("模板").doWrite(data());
} }
/** /**
@ -45,8 +45,7 @@ public class WebTest {
@PostMapping("upload") @PostMapping("upload")
@ResponseBody @ResponseBody
public String upload(MultipartFile file) throws IOException { public String upload(MultipartFile file) throws IOException {
EasyExcelFactory.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead() EasyExcelFactory.read(file.getInputStream(), UploadData.class, new UploadDataListener()).sheet().doRead();
.finish();
return "success"; return "success";
} }

17
src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -9,6 +9,8 @@ import org.junit.Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; 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.write.metadata.WriteSheet;
/** /**
* 写的常见写法 * 写的常见写法
@ -24,12 +26,23 @@ public class WriteTest {
*/ */
@Test @Test
public void simpleWrite() { public void simpleWrite() {
// 写法1
String fileName = TestFileUtil.getPath() + "write" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "write" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后千万别忘记 finish // 这里 需要指定写用哪个class去读,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 如果这里想使用03 则 传入excelType参数即可 // 如果这里想使用03 则 传入excelType参数即可
EasyExcelFactory.write(fileName, DemoData.class).sheet("模板").doWrite(data()).finish(); EasyExcelFactory.write(fileName, DemoData.class).sheet("模板").doWrite(data());
// 写法2
fileName = TestFileUtil.getPath() + "write" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去读
ExcelWriter excelWriter = EasyExcelFactory.write(fileName, DemoData.class).build();
WriteSheet writeSheet = EasyExcelFactory.writerSheet("模板").build();
excelWriter.write(data(), writeSheet);
/// 千万别忘记finish 会帮忙关闭流
excelWriter.finish();
} }
private List<DemoData> data() { private List<DemoData> data() {
List<DemoData> list = new ArrayList<DemoData>(); List<DemoData> list = new ArrayList<DemoData>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {

BIN
src/test/resources/demo/demo.xlsx

Binary file not shown.
Loading…
Cancel
Save