标签:java 报表导入 easyreport
在导入报表的时候,经常会遇到对象属性是集合或数组形似的,如学生会有多个老师,那么这种属性是集合或数组形式的报表如何设置,以及导入呢。
Teacher类
package model;
public class Teacher {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return name+"|"+sex;
}
}
Student类
package model;
import java.util.List;
public class Student {
private String name;
private int age;
private Course course;
//teacher集合
private List<Teacher> teachers;
//private Teacher[] teachers;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
}
导入数据
姓名 | 年龄 | 科目 | 分数 | 教师姓名 | 教师性别 |
王老五 | 12 | 语文 | 80 | 张三丰 | 男 |
Excel导入DEMO TestExcelComPro
package example;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import model.Student;
import test.AbstractExcelTest;
import com.easyReport.read.temp.PropertyTemp;
import com.easyReport.read.temp.ReadTemp;
public class TestExcelComPro extends AbstractExcelTest<Student> {
@Override
public void test() {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(new File("testFile/test2.xls"));
// testReadBean(fileInputStream);
testReadMap(fileInputStream);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected ReadTemp bulidReadTemp() {
ReadTemp readTemp = new ReadTemp(1);
readTemp.addPropertyTemp(new PropertyTemp("姓名", "name"));
readTemp.addPropertyTemp(new PropertyTemp("年龄", "age"));
readTemp.addPropertyTemp(new PropertyTemp("科目", "course.name"));
readTemp.addPropertyTemp(new PropertyTemp("分数", "course.score"));
//teachers设置和course对象属性设置是一样的
readTemp.addPropertyTemp(new PropertyTemp("教师姓名", "teachers.name"));
readTemp.addPropertyTemp(new PropertyTemp("教师性别", "teachers.sex"));
return readTemp;
}
@Override
protected void writeBeanList(List<Student> modelLists) {
System.out.println("--------------bean---------------");
for (Student s : modelLists) {
System.out.println(s.getName() + "|"
+ s.getAge()+s.getCourse().getName()+"|"+s.getCourse().getScore()+"|"+s.getTeachers().get(0));
}
}
@Override
protected void writeMapList(List<Map<String, Object>> mapLists) {
System.out.println("--------------map---------------");
for (Map<String, Object> m : mapLists) {
System.out.println(m.get("name") + "|" + m.get("age")+"|" + m.get("course.name")+"|" + m.get("course.score")+
"|"+m.get("teachers.name")+"|"+m.get("teachers.sex"));
}
}
}
运行结果:
--------------bean---------------
王老五|12语文|80.0|张三丰|男
注意:
集合和数组内容必须为用户定义对象,且生成的数组或集合内容有且只有一个。
如果需要添加多个对象,需要手动实现,下一节讲解。
老师性别我们存入的时候往往是F,M形式表示,那么需要对导入性别数据进行格式转化。
我们可以通过Teacher setSex实现格式转换。在这里我们主要讲解通过实现IDataFormater接口实现数据格式化。
package formater;
import com.easyReport.dataformater.IDataFormater;
public class SexFormater implements IDataFormater{
public static final String MALE = "M";
public static final String FEMALE = "F";
@Override
public Object format(Object value) {
return format(value, null);
}
@Override
public Object format(Object value, String format) {
if(value == null)
return "";
String sex = value.toString();
if(sex.contains("男"))
return MALE;
if(sex.contains("女"))
return FEMALE;
return value;
}
}
Excel导入DEMO TestExcelComPro 修改如下
protected ReadTemp bulidReadTemp() {
ReadTemp readTemp = new ReadTemp(1);
readTemp.addPropertyTemp(new PropertyTemp("姓名", "name"));
readTemp.addPropertyTemp(new PropertyTemp("年龄", "age"));
readTemp.addPropertyTemp(new PropertyTemp("科目", "course.name"));
readTemp.addPropertyTemp(new PropertyTemp("分数", "course.score"));
readTemp.addPropertyTemp(new PropertyTemp("教师姓名", "teachers.name"));
readTemp.addPropertyTemp(new PropertyTemp("教师性别", "teachers.sex",new SexFormater()));
return readTemp;
}
运行结果:
--------------bean---------------
王老五|12语文|80.0|张三丰|M
本文出自 “菜鸟博博” 博客,请务必保留此出处http://zhangxiao2.blog.51cto.com/6342789/1585861
java easyreport 导入excel、 txt 数据复合属性(二)
标签:java 报表导入 easyreport
原文地址:http://zhangxiao2.blog.51cto.com/6342789/1585861