2013-03-20 188 views
0

我正在Java中使用Jasper Report。如何在Jasper Reports中動態添加多個數據片段

在Java中,我可以動態地填充與下面的代碼數據的豆:

List<ThemeBean> themes = new ArrayList<ThemeBean>(); 
    CSVReader csvReader = new CSVReader(new FileReader(csvFilename)); 
    List<String[]> data = csvReader.readAll(); 
    for(String[] d : data) { 
     ThemeBean tb = new ThemeBean(); 
     tb.setThemes(d[0]); 
     tb.setComments(d[1]); 
     tb.setSentiment(d[2]); 
     themes.add(tb); 
    } 
     JasperDesign jasperDesign = JRXmlLoader.load(fileName); 
     JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(themes); 
     JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); 
     JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, ds); 

時的.jrxml文件已在標籤內指定,textFieldExpression正確的字段這種運作良好...

下會動態填充的是建名單:

<textFieldExpression><![CDATA[$F{themes}]]></textFieldExpression> 

我的問題是搞清楚如何了兩個差異做動態在同一報告中的不同表格。看來,我只能用於動態添加一個數據迭代。我試圖在同一報告中生成兩個完全不同的表格的結果。如果這個問題不清楚,請讓我知道,我會嘗試解決它。謝謝。

+0

我不知道如果我完全明白你的問題,因爲我用和舊版本的碧玉,我真的不處理底層體系結構,因爲它已經在我們的系統中實現,但也許你想使用的是子報表。 – jonny2k9 2013-03-20 23:44:35

+0

什麼意思是「兩個不同的數據集」?
1.是否在一個報告中嘗試使用列表和列表?並在一個表中顯示所有記錄?
2.或者嘗試使用列表進行第一次執行,列表進行第二次執行?有一個字段$ F {主題} – sanBez 2013-03-21 06:47:24

+0

這是一個嘗試在一個表中使用列表,列表是一個不同的表,但都在同一個報表中 – Tyrick 2013-03-21 16:28:25

回答

0

賈斯珀思想:一個報告 - 一個數據源。

在主報告中使用2個子報表。 Main.jrxml包含子報表1(用於顯示列表<ThemeBean>)和子報表2(用於列表<AnotherBean>)。 Subreport1和Subreport2放入標題(或摘要)區域。

定義在Main.jrxml參數:themesPar,anotherPar作爲java.util.Collection中

認沽集合作爲參數到主報告:

List<ThemeBean> themes = new ArrayList<ThemeBean>(); 
themes.add(...); 
... 
List<AnotherBean> blablas = new ArrayList<AnotherBean>(); 
blablas.add(...); 
... 

HashMap<String, Object> parameters = new HashMap<String, Object>(); 
parameters.put("themesPar", themes); 
parameters.put("anotherPar", blablas); 

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource()); 

對於主報告寫像

subreport1數據源表達
new JRBeanCollectionDataSource($P{themesPar}) 

並以與$ P {anotherPar}相同的方式用於subreport2。 在這兩種情況下設置子報表

Connection Type = "Use a datasource expression" 

爲什麼任何新手問這個問題??? :) 爲什麼這個解決方案不存在於jasperforge FAQ?

0

您可以通過創建另一個擁有beanCollection的bean來將多個bean集合傳遞給報表。這樣一個bean是這樣的:

public class DataBean { 
    private Collection beanCollection = null; 

    public DataBean() { 
    } 

    public Collection getBeanCollection() { 
     return beanCollection; 
    } 

    public void setBeanCollection(Collection beanCollection) { 
     this.beanCollection = beanCollection; 
    } 
} 

我認爲其他的收集將是一個不同的類型ThemeBean的。我以FruitBean爲例。對於要傳遞的每個bean集合,創建一個DataBean並將其添加到集合,然後將集合傳遞給該報告。所以,你的代碼將改爲是這樣的:

List<DataBean> allData = new ArrayList<DataBean>(); 

//...Create and populate `List<ThemeBean> themes` 

DataBean db = new DataBean(); 
db.setBeanCollection(themes); 
allData.add(db); 

//...Create and populate `List<FruitBean> fruits` 

db = new DataBean(); 
db.setBeanCollection(fruits); 
allData.add(db); 

//... Load the report 
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(allData); 
//... Fill the report 

您使用此數據源填充將是一個shell報告的報告,包含爲您創建每個DataBean子報表。只會出現在本報告中提供一個字段:$F{beanCollection}

設置報表(S)來

new JRBeanCollectionDataSource($F{beanCollection})數據源表達。

ThemeBeanFruitBean的字段將在子報表中可用。

如果您更願意使用列表組件而不是子報表,那也可以。

0

賈斯珀思想:一個報告 - 一個數據源。

在主報告中使用2個子報表。 Main.jrxml包含subreport1(用於顯示List)和subreport2(用於List)。 Subreport1和Subreport2放入標題(或摘要)區域。

定義在Main.jrxml參數:themesPar,anotherPar作爲java.util.Collection中