ireport子报表《javabean数据源,springmvc实现》
程序员文章站
2022-04-30 20:38:24
...
1.父报表和子报表的制作
效果图:
1.新建文件
2.删除其它ban,只留下detail1。
原因:把数据放在其它ban,显示时,只有一条数据。
3.添加javabean
(1)修改calsspath
工具–》选项–》classpath–》addFolder
(2)添加javabean
点击
然后
Calss name的路径是class文件相对classpath路径的地址。然后选择要显示的内容(这里是name和list类employee)
4.添加显示内容
(1)把左边files下的name拖到detail1和右边组件面板的subReport拖到detail1(拖subreport的时候创建子报表,过程和组报表一样,数据源选择空)
子报表效果图:
主报表效果图:
5.主报表和子报表添加关联(主报表向子报表传递数据)
在主报表中点击子报表,在属性栏中修改connection type为use a datasource expession;修改data source Expression 为new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{employee})
6.编译生成jasper文件(开发时主需要用到主报表Jasper文件)
2.代码
1.两个实体类
public class Employee {
private int number;
private String empName;
//get和set省略
//构造函数,用来添加数据
public Employee(int number, String empName) {
super();
this.number = number;
this.empName = empName;
}
public class Company {
private String name;
private List<Employee> employee;
//get、set
//构造函数,用来添加数据
public Company(String name, List<Employee> employee) {
super();
this.name = name;
this.employee = employee;
}
public Company() {
// TODO Auto-generated constructor stub
}
//数据源
public static List getData(){
List<Company> listCompany1=new ArrayList<>();
Employee employee1=new Employee(1,"xie");
Employee employee2=new Employee(2,"wu");
Employee employee3=new Employee(3,"cheng");
Employee employee4=new Employee(4,"good");
List<Employee> listEmployee1=new ArrayList<>();
List<Employee> listEmployee2=new ArrayList<>();
listEmployee1.add(employee1);
listEmployee1.add(employee2);
listEmployee2.add(employee3);
listEmployee2.add(employee4);
Company company1=new Company("company1",listEmployee1);
Company company2=new Company("company2",listEmployee2);
listCompany1.add(company1);
listCompany1.add(company2);
return listCompany1;
}
2.springmvc代码
@RequestMapping(value = "/report", method = RequestMethod.GET)
public String report(Model model) {
// 报表数据源
//JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
JRDataSource jrDataSource = new JRBeanCollectionDataSource(Company.getData());
// 动态指定报表模板url
//model.addAttribute("url", "/WEB-INF/jasper/spring_report.jasper");
model.addAttribute("url", "/WEB-INF/jasper/reportSub0711.jasper");
model.addAttribute("format", "pdf"); // 报表格式
model.addAttribute("jrMainDataSource", jrDataSource);
return "reportView"; // 对应jasper-views.xml中的bean id
}
3.JasperReport+springmvc整合开发详情请看http://blog.csdn.net/xht555/article/details/43409637
4.本项目代码: