forked from github/easyexcel
zhuangjiaju
5 years ago
48 changed files with 644 additions and 223 deletions
@ -1,16 +1,20 @@
|
||||
package com.alibaba.excel.read.listener.event; |
||||
|
||||
import java.util.List; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* |
||||
* |
||||
* Event |
||||
* |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public interface AnalysisFinishEvent { |
||||
/** |
||||
* Get result |
||||
* |
||||
* |
||||
* @return |
||||
*/ |
||||
Object getAnalysisResult(); |
||||
List<CellData> getAnalysisResult(); |
||||
} |
||||
|
@ -0,0 +1,16 @@
|
||||
package com.alibaba.easyexcel.test.core.annotation; |
||||
|
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class AnnotationData { |
||||
@ExcelProperty("日期") |
||||
private Date name; |
||||
} |
@ -0,0 +1,74 @@
|
||||
package com.alibaba.easyexcel.test.core.annotation; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
import com.alibaba.easyexcel.test.core.simple.SimpleData; |
||||
import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; |
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
|
||||
/** |
||||
* Annotation data test |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class AnnotationDataTest { |
||||
|
||||
private File file07 = TestFileUtil.createNewFile("simple07.xlsx"); |
||||
private File file03 = TestFileUtil.createNewFile("simple03.xls"); |
||||
|
||||
|
||||
|
||||
@Test |
||||
public void readAndWrite07() { |
||||
EasyExcelFactory.write(file07, com.alibaba.easyexcel.test.core.simple.SimpleData.class).sheet().doWrite(data()) |
||||
.finish(); |
||||
EasyExcelFactory.read(file07, com.alibaba.easyexcel.test.core.simple.SimpleData.class, |
||||
new com.alibaba.easyexcel.test.core.simple.SimpleDataListener()).sheet().doRead().finish(); |
||||
} |
||||
|
||||
@Test |
||||
public void synchronousRead07() { |
||||
// Synchronous read file
|
||||
List<Object> list = EasyExcelFactory.read(file07).head(com.alibaba.easyexcel.test.core.simple.SimpleData.class) |
||||
.sheet().doReadSync(); |
||||
Assert.assertEquals(list.size(), 10); |
||||
Assert.assertTrue(list.get(0) instanceof com.alibaba.easyexcel.test.core.simple.SimpleData); |
||||
Assert.assertEquals(((com.alibaba.easyexcel.test.core.simple.SimpleData)list.get(0)).getName(), "姓名0"); |
||||
} |
||||
|
||||
@Test |
||||
public void readAndWrite03() { |
||||
EasyExcelFactory.write(file03, com.alibaba.easyexcel.test.core.simple.SimpleData.class).sheet().doWrite(data()) |
||||
.finish(); |
||||
EasyExcelFactory.read(file03, com.alibaba.easyexcel.test.core.simple.SimpleData.class, new SimpleDataListener()) |
||||
.sheet().doRead().finish(); |
||||
} |
||||
|
||||
@Test |
||||
|
||||
public void synchronousRead03() { |
||||
// Synchronous read file
|
||||
List<Object> list = EasyExcelFactory.read(file03).head(com.alibaba.easyexcel.test.core.simple.SimpleData.class) |
||||
.sheet().doReadSync(); |
||||
Assert.assertEquals(list.size(), 10); |
||||
Assert.assertTrue(list.get(0) instanceof com.alibaba.easyexcel.test.core.simple.SimpleData); |
||||
Assert.assertEquals(((com.alibaba.easyexcel.test.core.simple.SimpleData)list.get(0)).getName(), "姓名0"); |
||||
} |
||||
|
||||
private List<com.alibaba.easyexcel.test.core.simple.SimpleData> data() { |
||||
List<com.alibaba.easyexcel.test.core.simple.SimpleData> list = |
||||
new ArrayList<com.alibaba.easyexcel.test.core.simple.SimpleData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
com.alibaba.easyexcel.test.core.simple.SimpleData simpleData = new SimpleData(); |
||||
simpleData.setName("姓名" + i); |
||||
list.add(simpleData); |
||||
} |
||||
return list; |
||||
} |
||||
} |
@ -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.easyexcel.test.core.simple.SimpleData; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class SimpleDataListener extends AnalysisEventListener<SimpleData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleDataListener.class); |
||||
List<SimpleData> list = new ArrayList<SimpleData>(); |
||||
|
||||
@Override |
||||
public void invoke(SimpleData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 10); |
||||
Assert.assertEquals(list.get(0).getName(), "姓名0"); |
||||
Assert.assertEquals((int)(context.readSheetHolder().getSheetNo()), 0); |
||||
Assert.assertEquals( |
||||
context.readSheetHolder().getExcelReadHeadProperty().getHeadMap().get(0).getHeadNameList().get(0), "姓名"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.alibaba.easyexcel.test.core.converter; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class ConverterData { |
||||
@ExcelProperty("日期") |
||||
private Date date; |
||||
@ExcelProperty("布尔") |
||||
private Boolean booleanData; |
||||
@ExcelProperty("大数") |
||||
private BigDecimal bigDecimal; |
||||
@ExcelProperty("长整型") |
||||
private Long longData; |
||||
@ExcelProperty("整型") |
||||
private Integer integerData; |
||||
@ExcelProperty("短整型") |
||||
private Short shortData; |
||||
@ExcelProperty("字节型") |
||||
private Byte byteData; |
||||
@ExcelProperty("双精度浮点型") |
||||
private Double doubleData; |
||||
@ExcelProperty("浮点型") |
||||
private Float floatData; |
||||
@ExcelProperty("字符串") |
||||
private String string; |
||||
} |
@ -0,0 +1,50 @@
|
||||
package com.alibaba.easyexcel.test.core.converter; |
||||
|
||||
import java.math.BigDecimal; |
||||
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 ConverterDataListener extends AnalysisEventListener<ConverterData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class); |
||||
List<ConverterData> list = new ArrayList<ConverterData>(); |
||||
|
||||
@Override |
||||
public void invoke(ConverterData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 1); |
||||
ConverterData 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.getBooleanData(), Boolean.TRUE); |
||||
Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); |
||||
Assert.assertEquals((long)data.getLongData(), 1L); |
||||
Assert.assertEquals((long)data.getIntegerData(), 1L); |
||||
Assert.assertEquals((long)data.getShortData(), 1L); |
||||
Assert.assertEquals((long)data.getByteData(), 1L); |
||||
Assert.assertEquals(data.getDoubleData(), 1.0, 0.0); |
||||
Assert.assertEquals(data.getFloatData(), (float)1.0, 0.0); |
||||
Assert.assertEquals(data.getString(), "测试"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,58 @@
|
||||
package com.alibaba.easyexcel.test.core.converter; |
||||
|
||||
import java.io.File; |
||||
import java.math.BigDecimal; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
|
||||
/** |
||||
* Annotation data test |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class ConverterDataTest { |
||||
|
||||
private File file07 = TestFileUtil.createNewFile("converter07.xlsx"); |
||||
private File file03 = TestFileUtil.createNewFile("converter03.xls"); |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite07() throws Exception { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void T02ReadAndWrite03() throws Exception { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
private void readAndWrite(File file) throws Exception { |
||||
EasyExcelFactory.write(file, ConverterData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file, ConverterData.class, new ConverterDataListener()).sheet().doRead().finish(); |
||||
} |
||||
|
||||
private List<ConverterData> data() throws Exception { |
||||
List<ConverterData> list = new ArrayList<ConverterData>(); |
||||
ConverterData converterData = new ConverterData(); |
||||
converterData.setDate(DateUtils.parseDate("2020-01-01 01:01:01")); |
||||
converterData.setBooleanData(Boolean.TRUE); |
||||
converterData.setBigDecimal(BigDecimal.ONE); |
||||
converterData.setLongData(1L); |
||||
converterData.setIntegerData(1); |
||||
converterData.setShortData((short)1); |
||||
converterData.setByteData((byte)1); |
||||
converterData.setDoubleData(1.0); |
||||
converterData.setFloatData((float)1.0); |
||||
converterData.setString("测试"); |
||||
list.add(converterData); |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,53 @@
|
||||
package com.alibaba.easyexcel.test.core.converter; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.Date; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class ReadConverterData { |
||||
@ExcelProperty("日期") |
||||
private Date date; |
||||
@ExcelProperty("日期字符串") |
||||
private String dateString; |
||||
@ExcelProperty("布尔") |
||||
private Boolean booleanData; |
||||
@ExcelProperty("布尔字符串") |
||||
private String booleanString; |
||||
@ExcelProperty("大数") |
||||
private BigDecimal bigDecimal; |
||||
@ExcelProperty("大数字符串") |
||||
private String bigDecimalString; |
||||
@ExcelProperty("长整型") |
||||
private Long longData; |
||||
@ExcelProperty("长整型字符串") |
||||
private String longString; |
||||
@ExcelProperty("整型") |
||||
private Integer integerData; |
||||
@ExcelProperty("整型字符串") |
||||
private String integerString; |
||||
@ExcelProperty("短整型") |
||||
private Short shortData; |
||||
@ExcelProperty("短整型字符串") |
||||
private String shortString; |
||||
@ExcelProperty("字节型") |
||||
private Byte byteData; |
||||
@ExcelProperty("字节型字符串") |
||||
private String byteString; |
||||
@ExcelProperty("双精度浮点型") |
||||
private Double doulbleData; |
||||
@ExcelProperty("双精度浮点型字符串") |
||||
private String doulbleString; |
||||
@ExcelProperty("浮点型") |
||||
private Float FloatData; |
||||
@ExcelProperty("浮点型字符串") |
||||
private String FloatString; |
||||
@ExcelProperty("字符串") |
||||
private String string; |
||||
} |
@ -1,22 +1,70 @@
|
||||
package com.alibaba.easyexcel.test.core.simple; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcelFactory; |
||||
|
||||
/** |
||||
* Simple data test |
||||
* |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class SimpleDataTest { |
||||
|
||||
private File file07 = TestFileUtil.createNewFile("simple07.xlsx"); |
||||
private File file03 = TestFileUtil.createNewFile("simple03.xls"); |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite() { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void T02ReadAndWrite03() { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
private void readAndWrite(File file) { |
||||
EasyExcelFactory.write(file, SimpleData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file, SimpleData.class, new SimpleDataListener()).sheet().doRead().finish(); |
||||
} |
||||
|
||||
@Test |
||||
public void read() { |
||||
EasyExcelFactory.read(TestFileUtil.getPath() + "simple" + File.separator + "simple07Test.xlsx", |
||||
SimpleData.class, new SimpleDataListener()).sheet().doRead().finish(); |
||||
public void T03SynchronousRead07() { |
||||
synchronousRead(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void T04SynchronousRead03() { |
||||
synchronousRead(file03); |
||||
} |
||||
|
||||
|
||||
|
||||
private void synchronousRead(File file) { |
||||
// Synchronous read file
|
||||
List<Object> list = EasyExcelFactory.read(file).head(SimpleData.class).sheet().doReadSync(); |
||||
Assert.assertEquals(list.size(), 10); |
||||
Assert.assertTrue(list.get(0) instanceof SimpleData); |
||||
Assert.assertEquals(((SimpleData)list.get(0)).getName(), "姓名0"); |
||||
} |
||||
|
||||
private List<SimpleData> data() { |
||||
List<SimpleData> list = new ArrayList<SimpleData>(); |
||||
for (int i = 0; i < 10; i++) { |
||||
SimpleData simpleData = new SimpleData(); |
||||
simpleData.setName("姓名" + i); |
||||
list.add(simpleData); |
||||
} |
||||
return list; |
||||
} |
||||
} |
||||
|
Binary file not shown.
Loading…
Reference in new issue