JasperReports学习笔记5-其它数据生成动态的报表(WEB)
一、空数据(Empty Datasources)
就是说JRXML文件里面的内容都是静态的,直接使用
JREmptyDataSource就行了
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), new JREmptyDataSource());
二、Map数组(Map Datasources)
有的时候数据不一定要从数据库里面来,还有可能从Map数组里面来成生Map[]的方法
private Map[] initializeMapArray()
{
//你可以把数组里面的每个map看成一个对象,就相于数据库里面的每个字段
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);
四、对象数组(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里面调用
JRDataSource dataSource = createReportDataSource();
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), dataSource);
五、对象集合
和对象数组基本上一样,就是最后生成的是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数据成生报表(XML as Datasource)
JRXmlDataSource xmlDataSource = new JRXmlDataSource(
new BufferedInputStream(getServletConfig().getServletContext()
.getResourceAsStream("/reports/AircraftData.xml")),
"/AircraftData/aircraft");
七、json数据源
json数据
![](http://img.e-com-net.com/image/product/d3c975ab092b4b77b3c90271c80dcc88.gif)
![](http://img.e-com-net.com/image/product/d3c975ab092b4b77b3c90271c80dcc88.gif)
![](http://img.e-com-net.com/image/product/d3c975ab092b4b77b3c90271c80dcc88.gif)
![](http://img.e-com-net.com/image/product/d3c975ab092b4b77b3c90271c80dcc88.gif)
![](http://img.e-com-net.com/image/product/d3c975ab092b4b77b3c90271c80dcc88.gif)
ireport配置截图: