mirror of https://github.com/alibaba/easyexcel
zhuangjiaju
5 years ago
22 changed files with 396 additions and 46 deletions
@ -0,0 +1,17 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
/** |
||||
* The types of write last row |
||||
* |
||||
* @author zhuangjiaju |
||||
**/ |
||||
public enum WriteLastRowType { |
||||
/** |
||||
* Tables are created without templates ,And any data has been written; |
||||
*/ |
||||
EMPTY, |
||||
/** |
||||
* It's supposed to have data in it.Tables are created with templates ,or any data has been written; |
||||
*/ |
||||
HAVE_DATA,; |
||||
} |
@ -0,0 +1,33 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
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 ComplexDataListener extends AnalysisEventListener<ComplexHeadData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ComplexHeadData.class); |
||||
List<ComplexHeadData> list = new ArrayList<ComplexHeadData>(); |
||||
|
||||
@Override |
||||
public void invoke(ComplexHeadData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 1); |
||||
ComplexHeadData data = list.get(0); |
||||
Assert.assertEquals(data.getString4(), "字符串4"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class ComplexHeadData { |
||||
@ExcelProperty({"顶格", "顶格", "两格"}) |
||||
private String string0; |
||||
@ExcelProperty({"顶格", "顶格", "两格"}) |
||||
private String string1; |
||||
@ExcelProperty({"顶格", "四联", "四联"}) |
||||
private String string2; |
||||
@ExcelProperty({"顶格", "四联", "四联"}) |
||||
private String string3; |
||||
@ExcelProperty({"顶格"}) |
||||
private String string4; |
||||
} |
@ -0,0 +1,54 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ComplexHeadDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.createNewFile("complexHead07.xlsx"); |
||||
file03 = TestFileUtil.createNewFile("complexHead03.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite07() { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void T02ReadAndWrite03() { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
private void readAndWrite(File file) { |
||||
EasyExcelFactory.write(file, ComplexHeadData.class).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file, ComplexHeadData.class, new ComplexDataListener()).sheet().doRead().finish(); |
||||
} |
||||
|
||||
private List<ComplexHeadData> data() { |
||||
List<ComplexHeadData> list = new ArrayList<ComplexHeadData>(); |
||||
ComplexHeadData data = new ComplexHeadData(); |
||||
data.setString0("字符串0"); |
||||
data.setString1("字符串1"); |
||||
data.setString2("字符串2"); |
||||
data.setString3("字符串3"); |
||||
data.setString4("字符串4"); |
||||
list.add(data); |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,38 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
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.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ListHeadDataListener extends AnalysisEventListener<Map<Integer, String>> { |
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(NoHeadData.class); |
||||
List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>(); |
||||
|
||||
@Override |
||||
public void invoke(Map<Integer, String> data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 1); |
||||
Map<Integer, String> data = list.get(0); |
||||
Assert.assertEquals(data.get(0), "字符串0"); |
||||
Assert.assertEquals(data.get(1), "1.0"); |
||||
Assert.assertEquals(data.get(2), "2020-01-01 01:01:01"); |
||||
Assert.assertEquals(data.get(3), "额外数据"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,69 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
import java.io.File; |
||||
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.EasyExcelFactory; |
||||
import com.alibaba.excel.util.DateUtils; |
||||
|
||||
/** |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class ListHeadDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.createNewFile("listHead07.xlsx"); |
||||
file03 = TestFileUtil.createNewFile("listHead03.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).head(head()).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file).registerReadListener(new ListHeadDataListener()).sheet().doRead().finish(); |
||||
} |
||||
|
||||
private List<List<String>> head() { |
||||
List<List<String>> list = new ArrayList<List<String>>(); |
||||
List<String> head0 = new ArrayList<String>(); |
||||
head0.add("字符串"); |
||||
List<String> head1 = new ArrayList<String>(); |
||||
head1.add("数字"); |
||||
List<String> head2 = new ArrayList<String>(); |
||||
head2.add("日期"); |
||||
list.add(head0); |
||||
list.add(head1); |
||||
list.add(head2); |
||||
return list; |
||||
} |
||||
|
||||
private List<List<Object>> data() throws ParseException { |
||||
List<List<Object>> list = new ArrayList<List<Object>>(); |
||||
List<Object> data0 = new ArrayList<Object>(); |
||||
data0.add("字符串0"); |
||||
data0.add(1); |
||||
data0.add(DateUtils.parseDate("2020-01-01 01:01:01")); |
||||
data0.add("额外数据"); |
||||
list.add(data0); |
||||
return list; |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author zhuangjiaju |
||||
*/ |
||||
@Data |
||||
public class NoHeadData { |
||||
@ExcelProperty("字符串") |
||||
private String string; |
||||
} |
@ -1,20 +0,0 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
/** |
||||
* Order data test |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class NoHeadData07Test { |
||||
|
||||
@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();
|
||||
} |
||||
} |
@ -0,0 +1,33 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
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 NoHeadDataListener extends AnalysisEventListener<NoHeadData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(NoHeadData.class); |
||||
List<NoHeadData> list = new ArrayList<NoHeadData>(); |
||||
|
||||
@Override |
||||
public void invoke(NoHeadData data, AnalysisContext context) { |
||||
list.add(data); |
||||
} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) { |
||||
Assert.assertEquals(list.size(), 1); |
||||
NoHeadData data = list.get(0); |
||||
Assert.assertEquals(data.getString(), "字符串0"); |
||||
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0))); |
||||
} |
||||
} |
@ -0,0 +1,51 @@
|
||||
package com.alibaba.easyexcel.test.core.head; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* |
||||
* @author zhuangjiaju |
||||
*/ |
||||
public class NoHeadDataTest { |
||||
|
||||
private static File file07; |
||||
private static File file03; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file07 = TestFileUtil.createNewFile("noHead07.xlsx"); |
||||
file03 = TestFileUtil.createNewFile("noHead03.xls"); |
||||
} |
||||
|
||||
@Test |
||||
public void T01ReadAndWrite07() { |
||||
readAndWrite(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void T02ReadAndWrite03() { |
||||
readAndWrite(file03); |
||||
} |
||||
|
||||
private void readAndWrite(File file) { |
||||
EasyExcelFactory.write(file, NoHeadData.class).needHead(Boolean.FALSE).sheet().doWrite(data()).finish(); |
||||
EasyExcelFactory.read(file, NoHeadData.class, new NoHeadDataListener()).headRowNumber(0).sheet().doRead() |
||||
.finish(); |
||||
} |
||||
|
||||
private List<NoHeadData> data() { |
||||
List<NoHeadData> list = new ArrayList<NoHeadData>(); |
||||
NoHeadData data = new NoHeadData(); |
||||
data.setString("字符串0"); |
||||
list.add(data); |
||||
return list; |
||||
} |
||||
} |
Loading…
Reference in new issue