一、空数据(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数据源
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> row1 = new HashMap<String, Object>();
Map<String, Object> row2 = new HashMap<String, Object>();
Map<String, Object> row = new HashMap<String, Object>();
row.put("field1", "001");
row.put("field2", "张三");
row.put("field3", "中国上海");
row.put("field4", "field4");
row.put("field5", "field5");
list.add(row);
row = new HashMap<String, Object>();
row.put("field1", "002");
row.put("field2", "李四");
row.put("field3", "中国杭州");
row.put("field4", "field4");
row.put("field5", "field5");
list.add(row);
row = new HashMap<String, Object>();
row.put("field1", "003");
row.put("field2", "王五");
row.put("field3", "中国南昌");
row.put("field4", "field4");
row.put("field5", "field5");
list.add(row);
row2.put("uAnswer", list);
row1.put("qNaire", row2);
JSONObject jsonObject = JSONObject.fromObject(row1);
String json = jsonObject.toString();
Map<String, Object> jasperPara = new HashMap<String, Object>();
jasperPara.put("TITLE", "json数据报表演示样例(20131012)");
InputStream is = new ByteArrayInputStream(json.getBytes());
jasperPara.put("JSON_INPUT_STREAM", is);
jasperPara.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.CHINA);
jasperPara.put(JRParameter.REPORT_LOCALE, Locale.CHINA);
json数据
{"qNaire": {
"uAnswer": [
{
"field1": "001",
"field2": "张三",
"field3": "中国上海",
"field4": "field4",
"field5": "field5",
},
{
"field1": "002",
"field2": "李四",
"field3": "中国杭州",
"field4": "field4",
"field5": "field5",
},
{
"field1": "003",
"field2": "王五",
"field3": "中国南昌",
"field4": "field4",
"field5": "field5",
}
]
}}
ireport配置截图: