jasperreports 사용자 정의 데이터 원본

3009 단어 자바 보고서
jasperreports-5.6.0 버 전의 가방 을 사용 합 니 다.
필요:  보고 서 는 기본 Basic 대상 과 집합 클래스 CustomList 대상 으로 구성 되 어 있 습 니 다.
질문:jasperreports 에서 사용 가능 한 datasource 클래스 를 제공 하지 않 았 습 니 다.
해결 방법:DataSource 를 사용자 정의 하여 JrDataSource 인 터 페 이 스 를 실현 하면 됩 니 다.
JRBean Collection DataSource 를 본 떠 ReportDataSource 를 썼 습 니 다.JRAbstractBeanDataSource 를 계승 하도록 합 니 다.코드 는 다음 과 같 습 니 다.
public class ReportDataSource extends JRAbstractBeanDataSource {
    /**
     *
     */
    private Collection> data;
    private Iterator> iterator;
    private Object currentBean;

    private Object basicData;//    

    /**
     *
     */
    public ReportDataSource(Object basicData,Collection> beanCollection)
    {
        this(basicData,beanCollection, true);
    }
    /**
     *
     */
    public ReportDataSource(Object basicData,Collection> beanCollection, boolean isUseFieldDescription)
    {
        super(isUseFieldDescription);

        this.basicData = basicData;
        this.data = beanCollection;

        if (this.data != null)
        {
            this.iterator = this.data.iterator();
        }
    }


    /**
     *
     */
    public boolean next()
    {
        boolean hasNext = false;

        if (this.iterator != null)
        {
            hasNext = this.iterator.hasNext();

            if (hasNext)
            {
                this.currentBean = this.iterator.next();
            }
        }

        return hasNext;
    }


    /**
     *
     */
    public Object getFieldValue(JRField field) throws JRException
    {
        if(field.getName().contains("basic.")){
            return getBeanProperty(basicData,field.getName().split("\\.")[1]);
        }
        return getFieldValue(currentBean, field);
    }


    /**
     *
     */
    public void moveFirst()
    {
        if (this.data != null)
        {
            this.iterator = this.data.iterator();
        }
    }

    /**
     * Returns the underlying bean collection used by this data source.
     *
     * @return the underlying bean collection
     */
    public Collection> getData()
    {
        return data;
    }

    /**
     * Returns the total number of records/beans that this data source
     * contains.
     *
     * @return the total number of records of this data source
     */
    public int getRecordCount()
    {
        return data == null ? 0 : data.size();
    }

    /**
     * Clones this data source by creating a new instance that reuses the same
     * underlying bean collection.
     *
     * @return a clone of this data source
     */
    public ReportDataSource cloneDataSource()
    {
        return new ReportDataSource(basicData,data);
    }
위의 코드 는 Jrbean Collection DataSource 를 수정 한 것 으로 몇 줄 의 코드 만 바 뀌 었 을 뿐 jrmxl 파일 에서 basic.속성 명 으로 Basic 대상 의 속성 을 표시 할 수 있 습 니 다.
주의해 야 할 것 은 CustomList 가 비어 있 으 면 보고 서 를 출력 할 수 없고,필요 하 다 면 ReportDataSource 구조 기 에 추가 할 수 있다 는 점 이다.  집합 이 비어 있 는 지 여 부 를 판단 하 는 코드 가 비어 있 으 면 길이 가 1 인 집합 을 초기 화 합 니 다.

좋은 웹페이지 즐겨찾기