forked from github/easyexcel
Jiaju Zhuang
5 years ago
14 changed files with 585 additions and 122 deletions
@ -0,0 +1,20 @@
|
||||
package com.alibaba.easyexcel.test.core.excludeorinclude; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class ExcludeOrIncludeData { |
||||
@ExcelProperty(order = 1) |
||||
private String column1; |
||||
@ExcelProperty(order = 2) |
||||
private String column2; |
||||
@ExcelProperty(order = 3) |
||||
private String column3; |
||||
@ExcelProperty(order = 4) |
||||
private String column4; |
||||
} |
@ -0,0 +1,161 @@
|
||||
package com.alibaba.easyexcel.test.core.excludeorinclude; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.BeforeClass; |
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import com.alibaba.easyexcel.test.core.sort.SortData; |
||||
import com.alibaba.easyexcel.test.core.sort.SortDataListener; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class ExcludeOrIncludeDataTest { |
||||
|
||||
private static File excludeIndex07; |
||||
private static File excludeIndex03; |
||||
private static File excludeFiledName07; |
||||
private static File excludeFiledName03; |
||||
private static File includeIndex07; |
||||
private static File includeIndex03; |
||||
private static File includeFiledName07; |
||||
private static File includeFiledName03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
excludeIndex07 = TestFileUtil.createNewFile("excludeIndex.xlsx"); |
||||
excludeIndex03 = TestFileUtil.createNewFile("excludeIndex.xls"); |
||||
excludeFiledName07 = TestFileUtil.createNewFile("excludeFiledName.xlsx"); |
||||
excludeFiledName03 = TestFileUtil.createNewFile("excludeFiledName.xls"); |
||||
includeIndex07 = TestFileUtil.createNewFile("includeIndex.xlsx"); |
||||
includeIndex03 = TestFileUtil.createNewFile("includeIndex.xls"); |
||||
includeFiledName07 = TestFileUtil.createNewFile("includeFiledName.xlsx"); |
||||
includeFiledName03 = TestFileUtil.createNewFile("includeFiledName.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void t01ExcludeIndex07() { |
||||
excludeIndex(excludeIndex07); |
||||
} |
||||
|
||||
@Test |
||||
public void t02ExcludeIndex07() { |
||||
excludeIndex(excludeIndex03); |
||||
} |
||||
|
||||
@Test |
||||
public void t03ExcludeFiledName07() { |
||||
excludeFiledName(excludeFiledName07); |
||||
} |
||||
|
||||
@Test |
||||
public void t04ExcludeFiledName07() { |
||||
excludeFiledName(excludeFiledName03); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void t05IncludeIndex07() { |
||||
includeIndex(includeIndex07); |
||||
} |
||||
|
||||
@Test |
||||
public void t06IncludeIndex07() { |
||||
includeIndex(includeIndex03); |
||||
} |
||||
|
||||
@Test |
||||
public void t07IncludeFiledName07() { |
||||
includeFiledName(includeFiledName07); |
||||
} |
||||
|
||||
@Test |
||||
public void t08IncludeFiledName07() { |
||||
includeFiledName(includeFiledName03); |
||||
} |
||||
|
||||
|
||||
private void excludeIndex(File file) { |
||||
Set<Integer> excludeColumnIndexes = new HashSet<Integer>(); |
||||
excludeColumnIndexes.add(0); |
||||
excludeColumnIndexes.add(3); |
||||
EasyExcel.write(file, ExcludeOrIncludeData.class).excludeColumnIndexes(excludeColumnIndexes).sheet() |
||||
.doWrite(data()); |
||||
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); |
||||
Assert.assertEquals(1, dataMap.size()); |
||||
Map<Integer, String> record = dataMap.get(0); |
||||
Assert.assertEquals(2, record.size()); |
||||
Assert.assertEquals("column2", record.get(0)); |
||||
Assert.assertEquals("column3", record.get(1)); |
||||
|
||||
} |
||||
|
||||
private void excludeFiledName(File file) { |
||||
Set<String> excludeColumnFiledNames = new HashSet<String>(); |
||||
excludeColumnFiledNames.add("column1"); |
||||
excludeColumnFiledNames.add("column3"); |
||||
excludeColumnFiledNames.add("column4"); |
||||
EasyExcel.write(file, ExcludeOrIncludeData.class).excludeColumnFiledNames(excludeColumnFiledNames).sheet() |
||||
.doWrite(data()); |
||||
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); |
||||
Assert.assertEquals(1, dataMap.size()); |
||||
Map<Integer, String> record = dataMap.get(0); |
||||
Assert.assertEquals(1, record.size()); |
||||
Assert.assertEquals("column2", record.get(0)); |
||||
|
||||
} |
||||
|
||||
private void includeIndex(File file) { |
||||
Set<Integer> includeColumnIndexes = new HashSet<Integer>(); |
||||
includeColumnIndexes.add(1); |
||||
includeColumnIndexes.add(2); |
||||
EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnIndexes(includeColumnIndexes).sheet() |
||||
.doWrite(data()); |
||||
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); |
||||
Assert.assertEquals(1, dataMap.size()); |
||||
Map<Integer, String> record = dataMap.get(0); |
||||
Assert.assertEquals(2, record.size()); |
||||
Assert.assertEquals("column2", record.get(0)); |
||||
Assert.assertEquals("column3", record.get(1)); |
||||
|
||||
} |
||||
|
||||
private void includeFiledName(File file) { |
||||
Set<String> includeColumnFiledNames = new HashSet<String>(); |
||||
includeColumnFiledNames.add("column2"); |
||||
includeColumnFiledNames.add("column3"); |
||||
EasyExcel.write(file, ExcludeOrIncludeData.class).includeColumnFiledNames(includeColumnFiledNames).sheet() |
||||
.doWrite(data()); |
||||
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); |
||||
Assert.assertEquals(1, dataMap.size()); |
||||
Map<Integer, String> record = dataMap.get(0); |
||||
Assert.assertEquals(2, record.size()); |
||||
Assert.assertEquals("column2", record.get(0)); |
||||
Assert.assertEquals("column3", record.get(1)); |
||||
} |
||||
|
||||
|
||||
private List<ExcludeOrIncludeData> data() { |
||||
List<ExcludeOrIncludeData> list = new ArrayList<ExcludeOrIncludeData>(); |
||||
ExcludeOrIncludeData excludeOrIncludeData = new ExcludeOrIncludeData(); |
||||
excludeOrIncludeData.setColumn1("column1"); |
||||
excludeOrIncludeData.setColumn2("column2"); |
||||
excludeOrIncludeData.setColumn3("column3"); |
||||
excludeOrIncludeData.setColumn4("column4"); |
||||
list.add(excludeOrIncludeData); |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package com.alibaba.easyexcel.test.core.sort; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class SortData { |
||||
|
||||
private String column5; |
||||
private String column6; |
||||
@ExcelProperty(order = 99) |
||||
private String column3; |
||||
@ExcelProperty(order = 100) |
||||
private String column4; |
||||
@ExcelProperty(value = "column2", index = 1) |
||||
private String column2; |
||||
@ExcelProperty(value = "column1", index = 0) |
||||
private String column1; |
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.easyexcel.test.core.sort; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Assert; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.core.simple.SimpleData; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class SortDataListener extends AnalysisEventListener<SortData> { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SortDataListener.class); |
||||
List<SortData> list = new ArrayList<SortData>(); |
||||
|
||||
|
||||
@Override |
||||
public void invoke(SortData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 1); |
||||
SortData sortData = list.get(0); |
||||
Assert.assertEquals("column1", sortData.getColumn1()); |
||||
Assert.assertEquals("column2", sortData.getColumn2()); |
||||
Assert.assertEquals("column3", sortData.getColumn3()); |
||||
Assert.assertEquals("column4", sortData.getColumn4()); |
||||
Assert.assertEquals("column5", sortData.getColumn5()); |
||||
Assert.assertEquals("column6", sortData.getColumn6()); |
||||
} |
||||
} |
@ -0,0 +1,111 @@
|
||||
package com.alibaba.easyexcel.test.core.sort; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.BeforeClass; |
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class SortDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
private static File sortNoHead07; |
||||
private static File sortNoHead03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.createNewFile("sort.xlsx"); |
||||
file03 = TestFileUtil.createNewFile("sort.xls"); |
||||
sortNoHead07 = TestFileUtil.createNewFile("sortNoHead.xlsx"); |
||||
sortNoHead03 = TestFileUtil.createNewFile("sortNoHead.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void t01ReadAndWrite07() { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void t02ReadAndWrite03() { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
@Test |
||||
public void t03ReadAndWriteNoHead07() { |
||||
readAndWriteNoHead(sortNoHead07); |
||||
} |
||||
|
||||
@Test |
||||
public void t04ReadAndWriteNoHead03() { |
||||
readAndWriteNoHead(sortNoHead03); |
||||
} |
||||
|
||||
|
||||
private void readAndWrite(File file) { |
||||
EasyExcel.write(file, SortData.class).sheet().doWrite(data()); |
||||
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); |
||||
Assert.assertEquals(1, dataMap.size()); |
||||
Map<Integer, String> record = dataMap.get(0); |
||||
Assert.assertEquals("column1", record.get(0)); |
||||
Assert.assertEquals("column2", record.get(1)); |
||||
Assert.assertEquals("column3", record.get(2)); |
||||
Assert.assertEquals("column4", record.get(3)); |
||||
Assert.assertEquals("column5", record.get(4)); |
||||
Assert.assertEquals("column6", record.get(5)); |
||||
|
||||
EasyExcel.read(file, SortData.class, new SortDataListener()).sheet().doRead(); |
||||
} |
||||
|
||||
private void readAndWriteNoHead(File file) { |
||||
EasyExcel.write(file).head(head()).sheet().doWrite(data()); |
||||
List<Map<Integer, String>> dataMap = EasyExcel.read(file).sheet().doReadSync(); |
||||
Assert.assertEquals(1, dataMap.size()); |
||||
Map<Integer, String> record = dataMap.get(0); |
||||
Assert.assertEquals("column1", record.get(0)); |
||||
Assert.assertEquals("column2", record.get(1)); |
||||
Assert.assertEquals("column3", record.get(2)); |
||||
Assert.assertEquals("column4", record.get(3)); |
||||
Assert.assertEquals("column5", record.get(4)); |
||||
Assert.assertEquals("column6", record.get(5)); |
||||
EasyExcel.read(file, SortData.class, new SortDataListener()).sheet().doRead(); |
||||
} |
||||
|
||||
private List<List<String>> head() { |
||||
List<List<String>> head = new ArrayList<List<String>>(); |
||||
head.add(Collections.singletonList("column1")); |
||||
head.add(Collections.singletonList("column2")); |
||||
head.add(Collections.singletonList("column3")); |
||||
head.add(Collections.singletonList("column4")); |
||||
head.add(Collections.singletonList("column5")); |
||||
head.add(Collections.singletonList("column6")); |
||||
return head; |
||||
} |
||||
|
||||
|
||||
private List<SortData> data() { |
||||
List<SortData> list = new ArrayList<SortData>(); |
||||
SortData sortData = new SortData(); |
||||
sortData.setColumn1("column1"); |
||||
sortData.setColumn2("column2"); |
||||
sortData.setColumn3("column3"); |
||||
sortData.setColumn4("column4"); |
||||
sortData.setColumn5("column5"); |
||||
sortData.setColumn6("column6"); |
||||
list.add(sortData); |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.alibaba.easyexcel.test.temp.large; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.apache.poi.ss.formula.functions.Index; |
||||
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 NoModelLargeDataListener extends AnalysisEventListener<Map<Integer, String>> { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(NoModelLargeDataListener.class); |
||||
private int count = 0; |
||||
|
||||
@Override |
||||
public void invoke(Map<Integer, String> 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); |
||||
} |
||||
} |
Loading…
Reference in new issue