Browse Source

增加父类支持

bugfix
jipengfei.jpf 6 years ago
parent
commit
99b7a251ec
  1. 11
      src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java
  2. 18
      src/test/java/com/alibaba/easyexcel/test/ReadTest.java
  3. 17
      src/test/java/com/alibaba/easyexcel/test/WriteTest.java
  4. 28
      src/test/java/com/alibaba/easyexcel/test/model/BaseReadModel.java
  5. 28
      src/test/java/com/alibaba/easyexcel/test/model/BaseWriteModel.java
  6. 24
      src/test/java/com/alibaba/easyexcel/test/model/ReadModel.java
  7. 2
      src/test/java/com/alibaba/easyexcel/test/model/ReadModel2.java
  8. 6
      src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java
  9. 8
      src/test/java/com/alibaba/easyexcel/test/util/DataUtil.java

11
src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java

@ -44,9 +44,16 @@ public class ExcelHeadProperty {
*/ */
private void initColumnProperties() { private void initColumnProperties() {
if (this.headClazz != null) { if (this.headClazz != null) {
Field[] fields = this.headClazz.getDeclaredFields(); List<Field> fieldList = new ArrayList<Field>();
Class tempClass = this.headClazz;
//当父类为null的时候说明到达了最上层的父类(Object类).
while (tempClass != null) {
fieldList.addAll(Arrays.asList(tempClass .getDeclaredFields()));
tempClass = tempClass.getSuperclass();
//得到父类,然后赋给自己
}
List<List<String>> headList = new ArrayList<List<String>>(); List<List<String>> headList = new ArrayList<List<String>>();
for (Field f : fields) { for (Field f : fieldList) {
initOneColumnProperty(f); initOneColumnProperty(f);
} }
//对列排序 //对列排序

18
src/test/java/com/alibaba/easyexcel/test/ReadTest.java

@ -1,8 +1,8 @@
package com.alibaba.easyexcel.test; package com.alibaba.easyexcel.test;
import com.alibaba.easyexcel.test.listen.ExcelListener; import com.alibaba.easyexcel.test.listen.ExcelListener;
import com.alibaba.easyexcel.test.model.JavaModel; import com.alibaba.easyexcel.test.model.ReadModel;
import com.alibaba.easyexcel.test.model.JavaModel2; import com.alibaba.easyexcel.test.model.ReadModel2;
import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader; import com.alibaba.excel.ExcelReader;
@ -38,7 +38,7 @@ public class ReadTest {
@Test @Test
public void simpleReadJavaModelV2007() throws IOException { public void simpleReadJavaModelV2007() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx");
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1,JavaModel.class)); List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class));
inputStream.close(); inputStream.close();
print(data); print(data);
} }
@ -65,7 +65,7 @@ public class ReadTest {
public void saxReadJavaModelV2007() throws IOException { public void saxReadJavaModelV2007() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1,JavaModel.class), excelListener); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener);
inputStream.close(); inputStream.close();
} }
@ -87,11 +87,11 @@ public class ReadTest {
excelReader.read(sheet); excelReader.read(sheet);
}else if(sheet.getSheetNo() ==2){ }else if(sheet.getSheetNo() ==2){
sheet.setHeadLineMun(1); sheet.setHeadLineMun(1);
sheet.setClazz(JavaModel.class); sheet.setClazz(ReadModel.class);
excelReader.read(sheet); excelReader.read(sheet);
}else if(sheet.getSheetNo() ==3){ }else if(sheet.getSheetNo() ==3){
sheet.setHeadLineMun(1); sheet.setHeadLineMun(1);
sheet.setClazz(JavaModel2.class); sheet.setClazz(ReadModel2.class);
excelReader.read(sheet); excelReader.read(sheet);
} }
} }
@ -121,7 +121,7 @@ public class ReadTest {
@Test @Test
public void simpleReadJavaModelV2003() throws IOException { public void simpleReadJavaModelV2003() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls");
List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, JavaModel.class)); List<Object> data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class));
inputStream.close(); inputStream.close();
print(data); print(data);
} }
@ -148,7 +148,7 @@ public class ReadTest {
public void saxReadJavaModelV2003() throws IOException { public void saxReadJavaModelV2003() throws IOException {
InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls");
ExcelListener excelListener = new ExcelListener(); ExcelListener excelListener = new ExcelListener();
EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, JavaModel.class), excelListener); EasyExcelFactory.readBySax(inputStream, new Sheet(2, 1, ReadModel.class), excelListener);
inputStream.close(); inputStream.close();
} }
@ -169,7 +169,7 @@ public class ReadTest {
excelReader.read(sheet); excelReader.read(sheet);
}else { }else {
sheet.setHeadLineMun(2); sheet.setHeadLineMun(2);
sheet.setClazz(JavaModel.class); sheet.setClazz(ReadModel.class);
excelReader.read(sheet); excelReader.read(sheet);
} }
} }

17
src/test/java/com/alibaba/easyexcel/test/WriteTest.java

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test; package com.alibaba.easyexcel.test;
import com.alibaba.easyexcel.test.model.JavaModel1; import com.alibaba.easyexcel.test.model.WriteModel;
import com.alibaba.easyexcel.test.util.FileUtil;
import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.metadata.Sheet;
@ -34,7 +35,7 @@ public class WriteTest {
writer.write1(createTestListObject(), sheet1); writer.write1(createTestListObject(), sheet1);
//写第二个sheet sheet2 模型上打有表头的注解,合并单元格 //写第二个sheet sheet2 模型上打有表头的注解,合并单元格
Sheet sheet2 = new Sheet(2, 3, JavaModel1.class, "第二个sheet", null); Sheet sheet2 = new Sheet(2, 3, WriteModel.class, "第二个sheet", null);
sheet2.setTableStyle(createTableStyle()); sheet2.setTableStyle(createTableStyle());
//writer.write1(null, sheet2); //writer.write1(null, sheet2);
writer.write(createTestListJavaMode(), sheet2); writer.write(createTestListJavaMode(), sheet2);
@ -51,7 +52,7 @@ public class WriteTest {
//写sheet2 模型上打有表头的注解 //写sheet2 模型上打有表头的注解
Table table2 = new Table(2); Table table2 = new Table(2);
table2.setTableStyle(createTableStyle()); table2.setTableStyle(createTableStyle());
table2.setClazz(JavaModel1.class); table2.setClazz(WriteModel.class);
writer.write(createTestListJavaMode(), sheet3, table2); writer.write(createTestListJavaMode(), sheet3, table2);
writer.finish(); writer.finish();
@ -62,7 +63,7 @@ public class WriteTest {
@Test @Test
public void writeV2007WithTemplate() throws IOException { public void writeV2007WithTemplate() throws IOException {
InputStream inputStream = new BufferedInputStream(new FileInputStream("/Users/jipengfei/temp.xlsx")); InputStream inputStream = FileUtil.getResourcesFileInputStream("temp.xlsx");
OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx"); OutputStream out = new FileOutputStream("/Users/jipengfei/2007.xlsx");
ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true); ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true);
//写第一个sheet, sheet1 数据全是List<String> 无模型映射关系 //写第一个sheet, sheet1 数据全是List<String> 无模型映射关系
@ -80,7 +81,7 @@ public class WriteTest {
writer.write1(createTestListObject(), sheet1); writer.write1(createTestListObject(), sheet1);
//写第二个sheet sheet2 模型上打有表头的注解,合并单元格 //写第二个sheet sheet2 模型上打有表头的注解,合并单元格
Sheet sheet2 = new Sheet(2, 3, JavaModel1.class, "第二个sheet", null); Sheet sheet2 = new Sheet(2, 3, WriteModel.class, "第二个sheet", null);
sheet2.setTableStyle(createTableStyle()); sheet2.setTableStyle(createTableStyle());
sheet2.setStartRow(20); sheet2.setStartRow(20);
writer.write(createTestListJavaMode(), sheet2); writer.write(createTestListJavaMode(), sheet2);
@ -96,7 +97,7 @@ public class WriteTest {
//写sheet2 模型上打有表头的注解 //写sheet2 模型上打有表头的注解
Table table2 = new Table(2); Table table2 = new Table(2);
table2.setTableStyle(createTableStyle()); table2.setTableStyle(createTableStyle());
table2.setClazz(JavaModel1.class); table2.setClazz(WriteModel.class);
writer.write(createTestListJavaMode(), sheet3, table2); writer.write(createTestListJavaMode(), sheet3, table2);
writer.finish(); writer.finish();
@ -123,7 +124,7 @@ public class WriteTest {
writer.write1(createTestListObject(), sheet1); writer.write1(createTestListObject(), sheet1);
//写第二个sheet sheet2 模型上打有表头的注解,合并单元格 //写第二个sheet sheet2 模型上打有表头的注解,合并单元格
Sheet sheet2 = new Sheet(2, 3, JavaModel1.class, "第二个sheet", null); Sheet sheet2 = new Sheet(2, 3, WriteModel.class, "第二个sheet", null);
sheet2.setTableStyle(createTableStyle()); sheet2.setTableStyle(createTableStyle());
writer.write(createTestListJavaMode(), sheet2); writer.write(createTestListJavaMode(), sheet2);
@ -137,7 +138,7 @@ public class WriteTest {
//写sheet2 模型上打有表头的注解 //写sheet2 模型上打有表头的注解
Table table2 = new Table(2); Table table2 = new Table(2);
table2.setTableStyle(createTableStyle()); table2.setTableStyle(createTableStyle());
table2.setClazz(JavaModel1.class); table2.setClazz(WriteModel.class);
writer.write(createTestListJavaMode(), sheet3, table2); writer.write(createTestListJavaMode(), sheet3, table2);
writer.finish(); writer.finish();

28
src/test/java/com/alibaba/easyexcel/test/model/BaseReadModel.java

@ -0,0 +1,28 @@
package com.alibaba.easyexcel.test.model;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
public class BaseReadModel extends BaseRowModel {
@ExcelProperty(index = 0)
protected String str;
@ExcelProperty(index = 1)
protected Float ff;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public Float getFf() {
return ff;
}
public void setFf(Float ff) {
this.ff = ff;
}
}

28
src/test/java/com/alibaba/easyexcel/test/model/BaseWriteModel.java

@ -0,0 +1,28 @@
package com.alibaba.easyexcel.test.model;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
public class BaseWriteModel extends BaseRowModel {
@ExcelProperty(value = {"表头1","表头1","表头31"},index = 0)
protected String p1;
@ExcelProperty(value = {"表头1","表头1","表头32"},index = 1)
protected String p2;
public String getP1() {
return p1;
}
public void setP1(String p1) {
this.p1 = p1;
}
public String getP2() {
return p2;
}
public void setP2(String p2) {
this.p2 = p2;
}
}

24
src/test/java/com/alibaba/easyexcel/test/model/JavaModel.java → src/test/java/com/alibaba/easyexcel/test/model/ReadModel.java

@ -1,17 +1,11 @@
package com.alibaba.easyexcel.test.model; package com.alibaba.easyexcel.test.model;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
public class JavaModel extends BaseRowModel { public class ReadModel extends BaseReadModel {
@ExcelProperty(index = 0)
private String str;
@ExcelProperty(index = 1)
private Float ff;
@ExcelProperty(index = 2) @ExcelProperty(index = 2)
private Integer mm; private Integer mm;
@ -37,22 +31,6 @@ public class JavaModel extends BaseRowModel {
@ExcelProperty(index = 18) @ExcelProperty(index = 18)
private String kk; private String kk;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public Float getFf() {
return ff;
}
public void setFf(Float ff) {
this.ff = ff;
}
public Integer getMm() { public Integer getMm() {
return mm; return mm;

2
src/test/java/com/alibaba/easyexcel/test/model/JavaModel2.java → src/test/java/com/alibaba/easyexcel/test/model/ReadModel2.java

@ -6,7 +6,7 @@ import com.alibaba.excel.metadata.BaseRowModel;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
public class JavaModel2 extends BaseRowModel { public class ReadModel2 extends BaseRowModel {
@ExcelProperty(index = 0) @ExcelProperty(index = 0)
private String str; private String str;

6
src/test/java/com/alibaba/easyexcel/test/model/JavaModel1.java → src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java

@ -6,13 +6,9 @@ import com.alibaba.excel.metadata.BaseRowModel;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Date; import java.util.Date;
public class JavaModel1 extends BaseRowModel { public class WriteModel extends BaseWriteModel {
@ExcelProperty(value = {"表头1","表头1","表头31"},index = 0)
private String p1;
@ExcelProperty(value = {"表头1","表头1","表头32"},index = 1)
private String p2;
@ExcelProperty(value = {"表头3","表头3","表头3"},index = 2) @ExcelProperty(value = {"表头3","表头3","表头3"},index = 2)
private int p3; private int p3;

8
src/test/java/com/alibaba/easyexcel/test/util/DataUtil.java

@ -1,6 +1,6 @@
package com.alibaba.easyexcel.test.util; package com.alibaba.easyexcel.test.util;
import com.alibaba.easyexcel.test.model.JavaModel1; import com.alibaba.easyexcel.test.model.WriteModel;
import com.alibaba.excel.metadata.Font; import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.metadata.TableStyle; import com.alibaba.excel.metadata.TableStyle;
import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.IndexedColors;
@ -54,10 +54,10 @@ public class DataUtil {
return head; return head;
} }
public static List<JavaModel1> createTestListJavaMode(){ public static List<WriteModel> createTestListJavaMode(){
List<JavaModel1> model1s = new ArrayList<JavaModel1>(); List<WriteModel> model1s = new ArrayList<WriteModel>();
for (int i = 0; i <10000 ; i++) { for (int i = 0; i <10000 ; i++) {
JavaModel1 model1 = new JavaModel1(); WriteModel model1 = new WriteModel();
model1.setP1("第一列,第行"); model1.setP1("第一列,第行");
model1.setP2("121212jjj"); model1.setP2("121212jjj");
model1.setP3(33+i); model1.setP3(33+i);

Loading…
Cancel
Save