日期:2014-05-17  浏览次数:21187 次

JasperReport 结合iReport报表开发

一、准备
下载iReport3.7.1、JasperReport3.7.1、Struts2.1.8及其他需要的包,需导入包如下图:

如需生成Excel的话,还需导入poi-3.5.jar包。


二、整合Struts2+JasperReport3
1. 新建以下三个pojo类:

package  com.xy.report.pojo;

public   class  Address {
     private  String city;
     private  String zipcode;
     public  String getCity() {
         return  city;
    }
     public   void  setCity(String city) {
         this .city  =  city;
    }
     public  String getZipcode() {
         return  zipcode;
    }
     public   void  setZipcode(String zipcode) {
         this .zipcode  =  zipcode;
    }
} 

?

package  com.xy.report.pojo;

import  java.util.HashSet;
import  java.util.Set;

public   class  Company  implements  Comparable < Company > {
     private  String name;
     private  Address address;
     private  Set < Staff >  staffs  =   new  HashSet < Staff > ();
    
    @Override
     public   int  compareTo(Company o) {
         return  o.getName().compareTo( this .getName());
    }

     public  String getName() {
         return  name;
    }
     public   void  setName(String name) {
         this .name  =  name;
    }
     public  Address getAddress() {
         return  address;
    }
     public   void  setAddress(Address address) {
         this .address  =  address;
    }
     public  Set < Staff >  getStaffs() {
         return  staffs;
    }
     public   void  setStaffs(Set < Staff >  staffs) {
         this .staffs  =  staffs;
    }

} 

?

?Company中compareTo方法用于在报表中排序。

?

package  com.xy.report.pojo;

public   class  Staff {
     private  String name;
     private  Company company;

     public  String getName() {
         return  name;
    }

     public   void  setName(String name) {
         this .name  =  name;
    }

     public  Company getCompany() {
         return  company;
    }

     public   void  setCompany(Company company) {
         this .company  =  company;
    }
    
    
} 

?以上Address是Company的组件,Company与Staff是一对多关系。

?

2. 新建Action

?

package  com.xy.report.action;

import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.List;
import  java.util.Map;

import  com.opensymphony.xwork2.ActionSupport;
import  com.xy.report.pojo.Address;
import  com.xy.report.pojo.Company;
import  com.xy.report.pojo.Staff;

public   class  DemoAction  extends  ActionSupport{

     private   static   final   long  serialVersionUID  =   - 5460323122712890562L ;
    
     private  List < Staff >  list;
     private  Map < String,Object >  map;
    
    @Override
     public  String execute(){
        list  =  getData();
        map  =  getParameter();
        
         return  SUCCESS;
    }
    
     private  List < Staff >  getData(){
        Address address  =   new  Address();
        address.setCity( " 上海 " );
        address.setZipcode( " 201400 " );

        Company company1  =   new  Company();
        company1.setName( " 飞利浦 " );
        company1.setAddress(address);
        Company company2  =   new  Company();
        company2.setName( " 谷歌 " );
        company2.setAddress(address);

        Staff staff1  =   new  Staff();
        staff1.setName( " 张三 " );
        staff1.setCompany(company1);
        Staff staff2  =   new  Staff();
        staff2.setName( " 李四 " );
        staff2.setCompany(company2);
        Staff staff3  =   new  Staff();
        staff3.setName( " 王五 " );
        staff3.setCompany(company1);
        
        List < Staff >  list  =   new  ArrayList < Staff > ();
        list.add(staff1);
        list.add(staff2);
        list.add(staff