about 4 years ago

之前一篇用了Java原生Map來做為資料存放的接口,但是不知道如何改成我自己定義的Bean,所以在網路上又找了一篇來實做看看。

1.跟前一篇操作差不多,沒看過的可以去翻一下這篇jasperreport and ireport use javabean in 5.5,所以跳過。
這邊會多使用到此套件
commons-beanutils-1.8.3.jar

2.在eclipse中設計你要使用的Java Bean,比如說是訂單報表

Morder.java (省略 get & set)
package com.payment.javabean;

import java.sql.Timestamp;

public class Morder {
    private String ordersn;
    private String merchantnumber;
    private String orgordernumber;
    private Long transnumber;
    private Integer orderamount;
    private Integer depositflag;
    private Integer orderstatus;
    private String returnurl;
    private String notifyurl;
    private Timestamp timecreate;
    private Timestamp timeapprove;
    private Timestamp timereversal;
    private Timestamp timesettle;
    private Integer approveamount;
    private String authcode;
    private String panmask;
    private String bankid;
    private String bankmid;
    private String banktid;
    private Integer depositstatus;
    private Integer settlestatus;
}

3.設計完你要用在報表中的元件之後,使用eclipse功能 檔案->匯出


選擇匯出成jar檔

匯出的設定如下

之後都使用預設值下一步到完成匯出

4.在iReport Designer設定Classpath,好讓他直接使用class中的屬性
工具->選項 的 Classpath頁籤 選擇 Add Jar

5.開啟新的報表檔案
File -> New -> Open this Template


選擇存檔路徑

新建完報表後,你可以從右邊調色盤中選取Static Text放在報表Column Header作為欄位表頭

點擊這顆按鈕設定資料來源

切換到JavaBean Datasource,輸入要使用的JavaBean名稱,點選Read attributs,在下面的欄位選擇要引用的屬性,再點擊Add selected field(s),再按完成

成功之後可以看到你的Fields已經加入了你要使用的Class屬性

從調色盤拖曳Text Field放置於Detail1區域,再點擊右鍵,選擇Edit expressio

刪除沒有對應的$F{field},從下方選擇你要的欄位

其實快一點也可以從左邊 Report Inspector中的Fields直接拉進報表中,就會有上面兩種東西,只是順便練習一下怎麼修改。

對報表做編譯。

6.編寫報表產出程式

ReportTest2.java
package com.payment.report;

import java.util.*;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;

import com.payment.javabean.Morder;

public class ReportTest2 {
    public static void main(String[] args) {
        String jasperFile = "D:/workspace/PaymentReport/test/report1.jasper";
        String outFile = "D:/workspace/PaymentReport/test/report.pdf";

        try {
            JasperReport jasperreport = (JasperReport) JRLoader.loadObjectFromFile(jasperFile);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperreport, null, new JRBeanCollectionDataSource(ReportTest2.getData()));
            JRExporter exporter = new JRPdfExporter();
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFile);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.exportReport();
        } catch (JRException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
    }
    
    /**
     * 取得虛擬資料
     * @return
     */
    public static List getData(){
        Random r = new Random();
        ArrayList list = new ArrayList();
        for(int i=0;i<3;i++){
            Morder order = new Morder();
            order.setOrdersn(String.valueOf(r.nextInt(99999999)));
            order.setOrderamount(r.nextInt(99999));
            list.add(order);
        }
        return list;
    }
}

完成收工

順帶一提,可以採用即時編輯的方式,也就是說可以提供原始的jrxml檔案,由程式內部
即時編譯,來產生報表,如果臨時要調整,也可以由系統工程師自己修改XML就可以了,範例如下

ReportTest3.java
package com.payment.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

import com.payment.javabean.Morder;

public class ReportTest3 {
    public static JasperDesign jasperDesign;
    public static JasperPrint jasperPrint;
    public static JasperReport jasperReport;
    public static String reportTemplateUrl = "person-template.jrxml";

    public static void main(String[] args) {
        String jasperFile = "D:/workspace/PaymentReport/test/report1.jrxml";
        try{
            //如果把原始檔包在資源裡面的寫法

            //InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("report1.jrxml");

            //指定實際檔案

            InputStream resourceAsStream = new FileInputStream(new File(jasperFile));
            jasperDesign = JRXmlLoader.load(resourceAsStream);
            jasperReport = JasperCompileManager.compileReport(jasperDesign);
            jasperPrint = JasperFillManager.fillReport(jasperReport, null, new JRBeanCollectionDataSource(ReportTest3.getData()));
            //使用JasperViewer檢視

            JasperViewer.viewReport(jasperPrint);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    /**
     * 取得虛擬資料
     * @return
     */
    public static List getData(){
        Random r = new Random();
        ArrayList list = new ArrayList();
        for(int i=0;i<3;i++){
            Morder order = new Morder();
            order.setOrdersn(String.valueOf(r.nextInt(99999999)));
            order.setOrderamount(r.nextInt(99999));
            list.add(order);
        }
        return list;
    }
}

← jasperreport 中文顯示 eclipse generate javadocs →
 
comments powered by Disqus