JasperReports學(xué)習(xí)筆記5-其它數(shù)據(jù)生成動態(tài)的報表(WEB)
一、空數(shù)據(jù)(Empty Datasources)
就是說JRXML文件里面的內(nèi)容都是靜態(tài)的,直接使用
JREmptyDataSource就行了
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), new JREmptyDataSource());
二、Map數(shù)組(Map Datasources)
有的時候數(shù)據(jù)不一定要從數(shù)據(jù)庫里面來,還有可能從Map數(shù)組里面來成生Map[]的方法
private Map[] initializeMapArray()
{
//你可以把數(shù)組里面的每個map看成一個對象,就相于數(shù)據(jù)庫里面的每個字段
HashMap[] reportRows = new HashMap[4];
HashMap row1Map = new HashMap();
HashMap row2Map = new HashMap();
HashMap row3Map = new HashMap();
HashMap row4Map = new HashMap();
row1Map.put("tail_num", "N263Y");
row1Map.put("aircraft_serial", "T-11");
row1Map.put("aircraft_model", "39 ROSCOE TRNR RACER");
row1Map.put("engine_model", "R1830 SERIES");
row2Map.put("tail_num", "N4087X");
row2Map.put("aircraft_serial", "BA100-163");
row2Map.put("aircraft_model", "BRADLEY AEROBAT");
row2Map.put("engine_model", "R2800 SERIES");
row3Map.put("tail_num", "N43JE");
row3Map.put("aircraft_serial", "HAYABUSA 1");
row3Map.put("aircraft_model", "NAKAJIMA KI-43 IIIA");
row3Map.put("engine_model", "R1830 SERIES");
row4Map.put("tail_num", "N912S");
row4Map.put("aircraft_serial", "9973CC");
row4Map.put("aircraft_model", "PA18-150");
row4Map.put("engine_model", "R-1820 SER");
reportRows[0] = row1Map;
reportRows[1] = row2Map;
reportRows[2] = row3Map;
reportRows[3] = row4Map;
return reportRows;
}
生成JRMapArrayDataSource對象
private JRDataSource createReportDataSource()
{
JRMapArrayDataSource dataSource;
Map[] reportRows = initializeMapArray();
dataSource = new JRMapArrayDataSource(reportRows);
return dataSource;
}
三、Map集合
private JRDataSource createReportDataSource()
{
JRMapCollectionDataSource dataSource;
Collection reportRows = initializeMapCollection();
dataSource = new JRMapCollectionDataSource(reportRows);
return dataSource;
}
private Collection initializeMapCollection()
{
ArrayList reportRows = new ArrayList();
HashMap row1Map = new HashMap();
HashMap row2Map = new HashMap();
HashMap row3Map = new HashMap();
HashMap row4Map = new HashMap();
row1Map.put("tail_num", "N263Y");
row1Map.put("aircraft_serial", "T-11");
row1Map.put("aircraft_model", "39 ROSCOE TRNR RACER");
row1Map.put("engine_model", "R1830 SERIES");
row2Map.put("tail_num", "N4087X");
row2Map.put("aircraft_serial", "BA100-163");
row2Map.put("aircraft_model", "BRADLEY AEROBAT");
row2Map.put("engine_model", "R2800 SERIES");
row3Map.put("tail_num", "N43JE");
row3Map.put("aircraft_serial", "HAYABUSA 1");
row3Map.put("aircraft_model", "NAKAJIMA KI-43 IIIA");
row3Map.put("engine_model", "R1830 SERIES");
row4Map.put("tail_num", "N912S");
row4Map.put("aircraft_serial", "9973CC");
row4Map.put("aircraft_model", "PA18-150");
row4Map.put("engine_model", "R-1820 SER");
reportRows.add(row1Map);
reportRows.add(row2Map);
reportRows.add(row3Map);
reportRows.add(row4Map);
return reportRows;
}
JRDataSource dataSource = createReportDataSource();
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), dataSource);
四、對象數(shù)組(Java Objects as Datasources)對象JAVABEAN
private JRDataSource createReportDataSource()
{
JRBeanArrayDataSource dataSource;
AircraftData[] reportRows = initializeBeanArray();
dataSource = new JRBeanArrayDataSource(reportRows);
return dataSource;
}
private AircraftData[] initializeBeanArray()
{
AircraftData[] reportRows = new AircraftData[4];
reportRows[0] = new AircraftData("N263Y", "T-11", "39 ROSCOE TRNR
RACER", "R1830 SERIES");
reportRows[1] = new AircraftData("N4087X", "BA100-163", "BRADLEY
AEROBAT", "R2800 SERIES");
reportRows[2] = new AircraftData("N43JE", "HAYABUSA 1", "NAKAJIMA
KI-43 IIIA", "R1830 SERIES");
reportRows[3] = new AircraftData("N912S", "9973CC", "PA18-150",
"R-1820 SER");
return reportRows;
}
//最后servlet里面調(diào)用
JRDataSource dataSource = createReportDataSource();
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), dataSource);
五、對象集合
和對象數(shù)組基本上一樣,就是最后生成的是JRBeanCollectionDataSource
private JRDataSource createReportDataSource()
{
JRBeanCollectionDataSource dataSource;
Collection reportRows = initializeBeanCollection();
dataSource = new JRBeanCollectionDataSource(reportRows);
return dataSource;
}
JRDataSource dataSource = createReportDataSource();
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), dataSource);
六、XML數(shù)據(jù)成生報表(XML as Datasource)
JRXmlDataSource xmlDataSource = new JRXmlDataSource(
new BufferedInputStream(getServletConfig().getServletContext()
.getResourceAsStream("/reports/AircraftData.xml")),
"/AircraftData/aircraft");
七、json數(shù)據(jù)源







































json數(shù)據(jù)

























ireport配置截圖:
posted on 2013-10-15 16:07 lanjh 閱讀(621) 評論(0) 編輯 收藏 所屬分類: 報表