From c1396b278aae7db933f1210aa78a1a82eb38b60e Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Mon, 13 Sep 2021 18:55:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90large=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/core/head/ComplexHeadDataTest.java | 34 ++++++++++++----- .../test/core/head/ListHeadDataTest.java | 17 +++++++-- .../test/core/head/NoHeadDataTest.java | 17 +++++++-- .../test/core/large/LargeDataListener.java | 17 ++++++--- .../test/core/large/LargeDataTest.java | 38 +++++++++++++++---- 5 files changed, 93 insertions(+), 30 deletions(-) diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java index 25f7fed5..07340c3b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexHeadDataTest.java @@ -4,29 +4,35 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + /** - * * @author Jiaju Zhuang */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ComplexHeadDataTest { private static File file07; private static File file03; + private static File fileCsv; private static File file07AutomaticMergeHead; private static File file03AutomaticMergeHead; + private static File fileCsvAutomaticMergeHead; @BeforeClass public static void init() { file07 = TestFileUtil.createNewFile("complexHead07.xlsx"); file03 = TestFileUtil.createNewFile("complexHead03.xls"); + fileCsv = TestFileUtil.createNewFile("complexHeadCsv.csv"); file07AutomaticMergeHead = TestFileUtil.createNewFile("complexHeadAutomaticMergeHead07.xlsx"); file03AutomaticMergeHead = TestFileUtil.createNewFile("complexHeadAutomaticMergeHead03.xls"); + fileCsvAutomaticMergeHead = TestFileUtil.createNewFile("complexHeadAutomaticMergeHeadCsv.csv"); } @Test @@ -39,6 +45,11 @@ public class ComplexHeadDataTest { readAndWrite(file03); } + @Test + public void t03ReadAndWriteCsv() { + readAndWrite(fileCsv); + } + private void readAndWrite(File file) { EasyExcel.write(file, ComplexHeadData.class).sheet().doWrite(data()); EasyExcel.read(file, ComplexHeadData.class, new ComplexDataListener()) @@ -46,16 +57,21 @@ public class ComplexHeadDataTest { } @Test - public void t03ReadAndWriteAutomaticMergeHead07() { - readAndWriteAutomaticMergeHead07(file07AutomaticMergeHead); + public void t11ReadAndWriteAutomaticMergeHead07() { + readAndWriteAutomaticMergeHead(file07AutomaticMergeHead); + } + + @Test + public void t12ReadAndWriteAutomaticMergeHead03() { + readAndWriteAutomaticMergeHead(file03AutomaticMergeHead); } @Test - public void t04ReadAndWriteAutomaticMergeHead0703() { - readAndWriteAutomaticMergeHead07(file03AutomaticMergeHead); + public void t13ReadAndWriteAutomaticMergeHeadCsv() { + readAndWriteAutomaticMergeHead(fileCsvAutomaticMergeHead); } - private void readAndWriteAutomaticMergeHead07(File file) { + private void readAndWriteAutomaticMergeHead(File file) { EasyExcel.write(file, ComplexHeadData.class).automaticMergeHead(Boolean.FALSE).sheet().doWrite(data()); EasyExcel.read(file, ComplexHeadData.class, new ComplexDataListener()).sheet().doRead(); } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java index 6e63a0b3..073c0e9c 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataTest.java @@ -5,26 +5,32 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.util.DateUtils; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + /** * * @author Jiaju Zhuang */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ListHeadDataTest { private static File file07; private static File file03; + private static File fileCsv; + @BeforeClass public static void init() { file07 = TestFileUtil.createNewFile("listHead07.xlsx"); file03 = TestFileUtil.createNewFile("listHead03.xls"); + fileCsv = TestFileUtil.createNewFile("listHeadCsv.csv"); } @Test @@ -37,6 +43,11 @@ public class ListHeadDataTest { readAndWrite(file03); } + @Test + public void t03ReadAndWriteCsv() throws Exception { + readAndWrite(fileCsv); + } + private void readAndWrite(File file) throws Exception { EasyExcel.write(file).head(head()).sheet().doWrite(data()); EasyExcel.read(file).registerReadListener(new ListHeadDataListener()).sheet().doRead(); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java index a62adbb8..b08e4630 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataTest.java @@ -4,25 +4,29 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; - import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; + /** - * * @author Jiaju Zhuang */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class NoHeadDataTest { private static File file07; private static File file03; + private static File fileCsv; @BeforeClass public static void init() { file07 = TestFileUtil.createNewFile("noHead07.xlsx"); file03 = TestFileUtil.createNewFile("noHead03.xls"); + fileCsv = TestFileUtil.createNewFile("noHeadCsv.csv"); } @Test @@ -35,6 +39,11 @@ public class NoHeadDataTest { readAndWrite(file03); } + @Test + public void t03ReadAndWriteCsv() { + readAndWrite(fileCsv); + } + private void readAndWrite(File file) { EasyExcel.write(file, NoHeadData.class).needHead(Boolean.FALSE).sheet().doWrite(data()); EasyExcel.read(file, NoHeadData.class, new NoHeadDataListener()).headRowNumber(0).sheet().doRead(); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java index 23a8843c..dafe25fa 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java @@ -1,13 +1,14 @@ 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.excel.support.ExcelTypeEnum; import com.alibaba.fastjson.JSON; +import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author Jiaju Zhuang */ @@ -22,13 +23,17 @@ public class LargeDataListener extends AnalysisEventListener { } count++; if (count % 100000 == 0) { - LOGGER.info("Already read:{}", count); + LOGGER.info("Already read:{},{}", count, JSON.toJSONString(data)); } } @Override public void doAfterAllAnalysed(AnalysisContext context) { LOGGER.info("Large row count:{}", count); - Assert.assertEquals(count, 464509); + if (context.readWorkbookHolder().getExcelType() != ExcelTypeEnum.CSV) { + Assert.assertEquals(count, 464509); + } else { + Assert.assertEquals(count, 499999); + } } } diff --git a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index fa32433a..957894e2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -4,31 +4,34 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import org.junit.BeforeClass; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.alibaba.easyexcel.test.demo.write.DemoData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.write.metadata.WriteSheet; +import org.junit.BeforeClass; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** - * * @author Jiaju Zhuang */ +@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class LargeDataTest { private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataTest.class); private static File fileFill07; private static File template07; + private static File fileCsv; private int i = 0; @BeforeClass public static void init() { fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx"); + fileCsv = TestFileUtil.createNewFile("largefileCsv.csv"); } @Test @@ -50,8 +53,27 @@ public class LargeDataTest { excelWriter.finish(); } + @Test + public void t03ReadAndWriteCsv() { + // write + long start = System.currentTimeMillis(); + ExcelWriter excelWriter = EasyExcel.write(fileCsv).build(); + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + for (int j = 0; j < 100; j++) { + excelWriter.write(data(), writeSheet); + LOGGER.info("{} write success.", j); + } + excelWriter.finish(); + LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start); + + // read + start = System.currentTimeMillis(); + EasyExcel.read(fileCsv, LargeData.class, new LargeDataListener()).sheet().doRead(); + LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start); + } + private List data() { - List list = new ArrayList(); + List list = new ArrayList<>(); int size = i + 5000; for (; i < size; i++) { LargeData largeData = new LargeData();