diff --git a/src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java index 0c77327a..06ff0703 100644 --- a/src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/ExcelHeadProperty.java @@ -44,9 +44,16 @@ public class ExcelHeadProperty { */ private void initColumnProperties() { if (this.headClazz != null) { - Field[] fields = this.headClazz.getDeclaredFields(); + List fieldList = new ArrayList(); + Class tempClass = this.headClazz; + //当父类为null的时候说明到达了最上层的父类(Object类). + while (tempClass != null) { + fieldList.addAll(Arrays.asList(tempClass .getDeclaredFields())); + tempClass = tempClass.getSuperclass(); + //得到父类,然后赋给自己 + } List> headList = new ArrayList>(); - for (Field f : fields) { + for (Field f : fieldList) { initOneColumnProperty(f); } //对列排序 diff --git a/src/test/java/com/alibaba/easyexcel/test/ReadTest.java b/src/test/java/com/alibaba/easyexcel/test/ReadTest.java index 52cac468..f92fedeb 100644 --- a/src/test/java/com/alibaba/easyexcel/test/ReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/ReadTest.java @@ -1,8 +1,8 @@ package com.alibaba.easyexcel.test; import com.alibaba.easyexcel.test.listen.ExcelListener; -import com.alibaba.easyexcel.test.model.JavaModel; -import com.alibaba.easyexcel.test.model.JavaModel2; +import com.alibaba.easyexcel.test.model.ReadModel; +import com.alibaba.easyexcel.test.model.ReadModel2; import com.alibaba.easyexcel.test.util.FileUtil; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.ExcelReader; @@ -38,7 +38,7 @@ public class ReadTest { @Test public void simpleReadJavaModelV2007() throws IOException { InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); - List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1,JavaModel.class)); + List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); inputStream.close(); print(data); } @@ -65,7 +65,7 @@ public class ReadTest { public void saxReadJavaModelV2007() throws IOException { InputStream inputStream = FileUtil.getResourcesFileInputStream("2007.xlsx"); 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(); } @@ -87,11 +87,11 @@ public class ReadTest { excelReader.read(sheet); }else if(sheet.getSheetNo() ==2){ sheet.setHeadLineMun(1); - sheet.setClazz(JavaModel.class); + sheet.setClazz(ReadModel.class); excelReader.read(sheet); }else if(sheet.getSheetNo() ==3){ sheet.setHeadLineMun(1); - sheet.setClazz(JavaModel2.class); + sheet.setClazz(ReadModel2.class); excelReader.read(sheet); } } @@ -121,7 +121,7 @@ public class ReadTest { @Test public void simpleReadJavaModelV2003() throws IOException { InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); - List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, JavaModel.class)); + List data = EasyExcelFactory.read(inputStream, new Sheet(2, 1, ReadModel.class)); inputStream.close(); print(data); } @@ -148,7 +148,7 @@ public class ReadTest { public void saxReadJavaModelV2003() throws IOException { InputStream inputStream = FileUtil.getResourcesFileInputStream("2003.xls"); 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(); } @@ -169,7 +169,7 @@ public class ReadTest { excelReader.read(sheet); }else { sheet.setHeadLineMun(2); - sheet.setClazz(JavaModel.class); + sheet.setClazz(ReadModel.class); excelReader.read(sheet); } } diff --git a/src/test/java/com/alibaba/easyexcel/test/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/WriteTest.java index 1b8d2ce1..cda8ced2 100644 --- a/src/test/java/com/alibaba/easyexcel/test/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/WriteTest.java @@ -1,6 +1,7 @@ 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.ExcelWriter; import com.alibaba.excel.metadata.Sheet; @@ -34,7 +35,7 @@ public class WriteTest { writer.write1(createTestListObject(), sheet1); //写第二个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()); //writer.write1(null, sheet2); writer.write(createTestListJavaMode(), sheet2); @@ -51,7 +52,7 @@ public class WriteTest { //写sheet2 模型上打有表头的注解 Table table2 = new Table(2); table2.setTableStyle(createTableStyle()); - table2.setClazz(JavaModel1.class); + table2.setClazz(WriteModel.class); writer.write(createTestListJavaMode(), sheet3, table2); writer.finish(); @@ -62,7 +63,7 @@ public class WriteTest { @Test 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"); ExcelWriter writer = EasyExcelFactory.getWriterWithTemp(inputStream,out,ExcelTypeEnum.XLSX,true); //写第一个sheet, sheet1 数据全是List 无模型映射关系 @@ -80,7 +81,7 @@ public class WriteTest { writer.write1(createTestListObject(), sheet1); //写第二个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.setStartRow(20); writer.write(createTestListJavaMode(), sheet2); @@ -96,7 +97,7 @@ public class WriteTest { //写sheet2 模型上打有表头的注解 Table table2 = new Table(2); table2.setTableStyle(createTableStyle()); - table2.setClazz(JavaModel1.class); + table2.setClazz(WriteModel.class); writer.write(createTestListJavaMode(), sheet3, table2); writer.finish(); @@ -123,7 +124,7 @@ public class WriteTest { writer.write1(createTestListObject(), sheet1); //写第二个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()); writer.write(createTestListJavaMode(), sheet2); @@ -137,7 +138,7 @@ public class WriteTest { //写sheet2 模型上打有表头的注解 Table table2 = new Table(2); table2.setTableStyle(createTableStyle()); - table2.setClazz(JavaModel1.class); + table2.setClazz(WriteModel.class); writer.write(createTestListJavaMode(), sheet3, table2); writer.finish(); diff --git a/src/test/java/com/alibaba/easyexcel/test/model/BaseReadModel.java b/src/test/java/com/alibaba/easyexcel/test/model/BaseReadModel.java new file mode 100644 index 00000000..be65aa76 --- /dev/null +++ b/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; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/model/BaseWriteModel.java b/src/test/java/com/alibaba/easyexcel/test/model/BaseWriteModel.java new file mode 100644 index 00000000..8688216c --- /dev/null +++ b/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; + } +} diff --git a/src/test/java/com/alibaba/easyexcel/test/model/JavaModel.java b/src/test/java/com/alibaba/easyexcel/test/model/ReadModel.java similarity index 82% rename from src/test/java/com/alibaba/easyexcel/test/model/JavaModel.java rename to src/test/java/com/alibaba/easyexcel/test/model/ReadModel.java index 25b9cfe5..fe901719 100644 --- a/src/test/java/com/alibaba/easyexcel/test/model/JavaModel.java +++ b/src/test/java/com/alibaba/easyexcel/test/model/ReadModel.java @@ -1,17 +1,11 @@ package com.alibaba.easyexcel.test.model; import com.alibaba.excel.annotation.ExcelProperty; -import com.alibaba.excel.metadata.BaseRowModel; import java.math.BigDecimal; import java.util.Date; -public class JavaModel extends BaseRowModel { - @ExcelProperty(index = 0) - private String str; - - @ExcelProperty(index = 1) - private Float ff; +public class ReadModel extends BaseReadModel { @ExcelProperty(index = 2) private Integer mm; @@ -37,22 +31,6 @@ public class JavaModel extends BaseRowModel { @ExcelProperty(index = 18) 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() { return mm; diff --git a/src/test/java/com/alibaba/easyexcel/test/model/JavaModel2.java b/src/test/java/com/alibaba/easyexcel/test/model/ReadModel2.java similarity index 98% rename from src/test/java/com/alibaba/easyexcel/test/model/JavaModel2.java rename to src/test/java/com/alibaba/easyexcel/test/model/ReadModel2.java index baf93fc1..a64f7011 100644 --- a/src/test/java/com/alibaba/easyexcel/test/model/JavaModel2.java +++ b/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.util.Date; -public class JavaModel2 extends BaseRowModel { +public class ReadModel2 extends BaseRowModel { @ExcelProperty(index = 0) private String str; diff --git a/src/test/java/com/alibaba/easyexcel/test/model/JavaModel1.java b/src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java similarity index 91% rename from src/test/java/com/alibaba/easyexcel/test/model/JavaModel1.java rename to src/test/java/com/alibaba/easyexcel/test/model/WriteModel.java index e19a6f96..acb17c1e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/model/JavaModel1.java +++ b/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.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) private int p3; diff --git a/src/test/java/com/alibaba/easyexcel/test/util/DataUtil.java b/src/test/java/com/alibaba/easyexcel/test/util/DataUtil.java index d2bafd1c..505dab55 100644 --- a/src/test/java/com/alibaba/easyexcel/test/util/DataUtil.java +++ b/src/test/java/com/alibaba/easyexcel/test/util/DataUtil.java @@ -1,6 +1,6 @@ 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.TableStyle; import org.apache.poi.ss.usermodel.IndexedColors; @@ -54,10 +54,10 @@ public class DataUtil { return head; } - public static List createTestListJavaMode(){ - List model1s = new ArrayList(); + public static List createTestListJavaMode(){ + List model1s = new ArrayList(); for (int i = 0; i <10000 ; i++) { - JavaModel1 model1 = new JavaModel1(); + WriteModel model1 = new WriteModel(); model1.setP1("第一列,第行"); model1.setP2("121212jjj"); model1.setP3(33+i);