圖示ireport 中使用javabean 作數據源開發 基於 jasperreports 報表 過程
——學習筆記系列之ireport起步
xmlin
本文不講原理,因爲網上的資源很多,本文以一個簡單的日銷售報表爲例,記錄在ireport中使用javabean 作數據源開發 基於 jasperreports 報表 過程 .
一. 準備工作
1.正確安裝jdk
2.到
http://ireport.sourceforge.net/ 去下載ireport,本文使用1.3.1版本,解壓iReport在任意目錄,解壓後的文件裏面有一個iReport.bat,通過雙擊運行。
3.到
http://jasperreports.sourceforge.net/index.html 下載jasperreport,本文使用的是1.3.1版本,解壓到任意目錄,開發中需要使用其中的
jasperreports-1.3.1.jar , commons-logging-1.0.2.jar , commons-collections-2.1.jar.
二.創建javabean
1.創建DailySales.java,一個簡單VO bean。
import java.io.Serializable;
public class DailySales implements Serializable
{
private static final long serialVersionUID = 1L;
private String productNo ;
private String productName ;
private int number ;
private int money ;
private int id ;
public DailySales(String productNo, String productName, int number, int money)
{
this . productNo = productNo;
this . productName = productName;
this . number = number;
this . money = money;
}
public String getProductNo()
{
return productNo ;
}
public void setProductNo(String productNo)
{
this . productNo = productNo;
}
public String getProductName()
{
return productName ;
}
public void setProductName(String productName)
{
this . productName = productName;
}
public int getNumber()
{
return number ;
}
public void setNumber( int number)
{
this . number = number;
}
public int getMoney()
{
return money ;
}
public void setMoney( int money)
{
this . money = money;
}
public int getId()
{
return id ;
}
public void setId( int id)
{
this . id = id;
}
}
2. 創建 DailySalesDataSource.java, 這是報表的數據源。這個類實現了 jasperreports 中提供的數據源接口 JRDataSource, 實現其中的兩個方法 :next() 和 getFieldValue(JRField field) 。
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
public class DailySalesDataSource implements JRDataSource
{
/**
* 測試數據,實際項目中是動態獲取,也不一定是數組,可以是其它的數據類型 .
*/
private Object[][] data =
{
{ " 貨號 1" , " 物品1 " , 1,1000},
{ " 貨號 2" , " 物品 2" , 2,2000},
{ " 貨號 3" , " 物品 3" , 3,3000},
{ " 貨號 4" , " 物品 4" , 4,4000},
{ " 貨號 5" , " 物品 5" , 5,5000},
{ " 貨號 6" , " 物品 6" , 6,6000},
{ " 貨號 7" , " 物品 7" , 7,7000},
{ " 貨號 8" , " 物品 8" , 8,8000},
{ " 貨號 9" , " 物品 9" , 9,9000},
{ " 貨號 10" , " 物品 10" , 10,10000}
};
private int index = -1;
public DailySalesDataSource()
{
}
/**
* 實現了 JRDataSource 中的方法.判斷是否還有下一個.
*/
public boolean next() throws JRException
{
index ++;
return ( index < data . length );
}
/**
* 實現了 JRDataSource 中的方法.
* @param field 是對應報表中的要填充的字段的名稱.
*/
public Object getFieldValue(JRField field ) throws JRException
{
Object value = null ;
String fieldName = field .getName();
if ( "id" .equals(fieldName))
{
value = index+1 ;
}
else if ( "productNo" .equals(fieldName))
{
value = data [ index ][0];
}
else if ( "productName" .equals(fieldName))
{
value = data [ index ][1];
}
else if ( "number" .equals(fieldName))
{
value = data [ index ][2];
}
else if ( "money" .equals(fieldName))
{
value = data [ index ][3];
}
return value;
}
}
3 .在 ireport 中使用,取得測試數據源 , 如果不使用 ireport 工具,則只需要上面的兩個類。
import java.util.Arrays;
import java.util.Collection;
/**
* 簡單工廠類,取得測試數據
* @author xmlin
*
*/
public class DailySalesFactory
{
private static DailySales[] data =
{
new DailySales( " 貨號 1" , " 物品1 " , 1,1000),
new DailySales( " 貨號 2" , " 物品 2" , 2,2000),
new DailySales( " 貨號 3" , " 物品 3" , 3,3000),
new DailySales( " 貨號 4" , " 物品 4" , 4,4000),
new DailySales( " 貨號 5" , " 物品 5" , 5,5000),
new DailySales( " 貨號 6" , " 物品 6" , 6,6000),
new DailySales( " 貨號 7" , " 物品 7" , 7,7000),
new DailySales( " 貨號 8" , " 物品 8" , 8,8000),
new DailySales( " 貨號 9" , " 物品 9" , 9,9000),
new DailySales( " 貨號 10" , " 物品 10" , 10,10000)
};
public static Object[] getBeanArray()
{
return data ;
}
public static Collection getBeanCollection()
{
return Arrays.asList ( data );
}
}
三.使用ireport開發報表樣式
1.新建一個項目。
2.設置類路徑,在菜單“options”中選擇Classpath,點擊在彈出框中的add folder,填寫javabean編譯成的.class文件存放的路徑. 點save Classpath完成。如圖
3.設置數據源.在菜單"Data"中選擇”Connection/Data sources”, 點擊在彈出框中的new按鈕增加一個數據源.如圖
其中Name隨便取一個名字,Type of Connection/Data 選擇 JavaBeans set data source,如果使用其它的數據源則選擇其它的選項.Factory class 爲我們剛纔創建的Factory類,裏面包含取得測試數據的靜態方法getBeanCollection().用Test測試是否成功,點Save保存.(如果不成功的話,則應該看看那Factory這個類是不是在包內,這個類不能放在包內。應該將其放到classpath下面)
4. 設置活動連接.在菜單"Data"中選擇”Set Active Connection”.
5.Report Query , 在菜單"Data"中選擇” Report Query”,填寫javabean,即我們創建的VO bean.如圖
6.設計報表.
設計日期字段如圖
設計填充字段,如圖
設計頁數字段如圖
設計好的報表樣式如圖
點菜單"build"中的"compile"進行編譯,然後再”execute with connection datasource”就可以看到報表的結果了.
報表設計完成,在ireport的執行路徑下會生成一個DailySales.jasper的文件.
四.編寫測試類.
生成的DailySales.jasper可以在web服務端生成報表,也可以在肥客戶端如swing生成,這裏寫一個在肥客戶端和簡單運用.
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
public class TestReport
{
public static void main(String[] args)
{
TestReport.showReport ();
}
private static void showReport()
{
String reportPath = "D://dailySales.jasper" ;
Map parameters = new HashMap();
// 如果報表中有用到變量,在這裏給它賦值.
//parameters.put("ReportTitle", " 報表標題 ");
try
{
JasperReport jasperReport = (JasperReport) JRLoader.loadObject (reportPath);
JasperPrint jasperPrint = JasperFillManager.fillReport (jasperReport, parameters, new DailySalesDataSource());
JasperViewer jrview = new JasperViewer(jasperPrint);
jrview.setPreferredSize( new Dimension(200,100));
jrview.setVisible( true );
}
catch (JRException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}