forked from github/easyexcel
zhuangjiaju
5 years ago
31 changed files with 502 additions and 240 deletions
@ -0,0 +1,41 @@
|
||||
package com.alibaba.easyexcel.test.core.annotation; |
||||
|
||||
import java.text.ParseException; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
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.exception.ExcelCommonException; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class AnnotationDataListener extends AnalysisEventListener<AnnotationData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationDataListener.class); |
||||
List<AnnotationData> list = new ArrayList<AnnotationData>(); |
||||
|
||||
@Override |
||||
public void invoke(AnnotationData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 1); |
||||
AnnotationData data = list.get(0); |
||||
try { |
||||
Assert.assertEquals(data.getDate(), DateUtils.parseDate("2020-01-01 01:01:01")); |
||||
} catch (ParseException e) { |
||||
throw new ExcelCommonException("Test Exception", e); |
||||
} |
||||
Assert.assertEquals(data.getNumber(), 99.99, 0.00); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,20 @@
|
||||
package com.alibaba.easyexcel.test.core.annotation; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class AnnotationIndexAndNameData { |
||||
@ExcelProperty(value = "第四个", index = 4) |
||||
private String index4; |
||||
@ExcelProperty(value = "第二个") |
||||
private String index2; |
||||
@ExcelProperty(index = 0) |
||||
private String index0; |
||||
@ExcelProperty(value = "第一个", index = 1) |
||||
private String index1; |
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.alibaba.easyexcel.test.core.annotation; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
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 AnnotationIndexAndNameDataListener extends AnalysisEventListener<AnnotationIndexAndNameData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationIndexAndNameDataListener.class); |
||||
List<AnnotationIndexAndNameData> list = new ArrayList<AnnotationIndexAndNameData>(); |
||||
|
||||
@Override |
||||
public void invoke(AnnotationIndexAndNameData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 1); |
||||
AnnotationIndexAndNameData data = list.get(0); |
||||
Assert.assertEquals(data.getIndex0(), "第0个"); |
||||
Assert.assertEquals(data.getIndex1(), "第1个"); |
||||
Assert.assertEquals(data.getIndex2(), "第2个"); |
||||
Assert.assertEquals(data.getIndex4(), "第4个"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,55 @@
|
||||
package com.alibaba.easyexcel.test.core.annotation; |
||||
|
||||
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.EasyExcelFactory; |
||||
|
||||
/** |
||||
* Annotation data test |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class AnnotationIndexAndNameDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.createNewFile("annotationIndexAndName07.xlsx"); |
||||
file03 = TestFileUtil.createNewFile("annotationIndexAndName03.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite07() { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void T02ReadAndWrite03() { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
private void readAndWrite(File file) { |
||||
EasyExcelFactory.write(file, AnnotationIndexAndNameData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file, AnnotationIndexAndNameData.class, new AnnotationIndexAndNameDataListener()).sheet() |
||||
.doRead().finish(); |
||||
} |
||||
|
||||
private List<AnnotationIndexAndNameData> data() { |
||||
List<AnnotationIndexAndNameData> list = new ArrayList<AnnotationIndexAndNameData>(); |
||||
AnnotationIndexAndNameData data = new AnnotationIndexAndNameData(); |
||||
data.setIndex0("第0个"); |
||||
data.setIndex1("第1个"); |
||||
data.setIndex2("第2个"); |
||||
data.setIndex4("第4个"); |
||||
list.add(data); |
||||
return list; |
||||
} |
||||
} |
@ -1,29 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.core.order; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class OrderData { |
||||
@ExcelProperty(value = "第一个", index = 1) |
||||
private String index1; |
||||
@ExcelProperty(value = "第10个", index = 10) |
||||
private String index10; |
||||
|
||||
public String getIndex1() { |
||||
return index1; |
||||
} |
||||
|
||||
public void setIndex1(String index1) { |
||||
this.index1 = index1; |
||||
} |
||||
|
||||
public String getIndex10() { |
||||
return index10; |
||||
} |
||||
|
||||
public void setIndex10(String index10) { |
||||
this.index10 = index10; |
||||
} |
||||
} |
@ -1,34 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.core.order; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
/** |
||||
* Order data test |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class OrderData07Test { |
||||
|
||||
@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; |
||||
} |
||||
} |
Binary file not shown.
Loading…
Reference in new issue