JasperReport学习笔记5-其它数据生成动态的报表(WEB)

1.空数据(Empty Datasources)
就是说JRXML文件里面的内容都是静态的,直接使用
JREmptyDataSource就行了
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), new JREmptyDataSource());


2.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;
}


Servlet里面
JRDataSource dataSource = createReportDataSource();
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), dataSource);


3.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);


4.对象数组(Java Objects as Datasources)对象JAVABEAN
public class AircraftData
{
public AircraftData(String tail, String serial, String model,
String engine)
{
setTailNum(tail);
setAircraftSerial(serial);
setAircraftModel(model);
setEngineModel(engine);
}
public AircraftData()
{
}
private String tailNum;
private String aircraftSerial;
private String aircraftModel;
private String engineModel;
//set get method.....
}


JRXML文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="AircraftReport">
<field name="tailNum" class="java.lang.String"/>
<field name="aircraftSerial" class="java.lang.String"/>
<field name="aircraftModel" class="java.lang.String"/>
<field name="engineModel" class="java.lang.String"/>
<pageHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="69" height="24"/>
<textElement verticalAlignment="Bottom"/>
<text>
<![CDATA[Tail Number: ]]>
</text>
</staticText>
<staticText>
<reportElement x="140" y="0" width="69" height="24"/>
<text>
<![CDATA[Serial Number: ]]>
</text>
</staticText>
<staticText>
<reportElement x="280" y="0" width="69" height="24"/>
<text>
<![CDATA[Model: ]]>
</text>
</staticText>
<staticText>
<reportElement x="420" y="0" width="69" height="24"/>
<text>
<![CDATA[Engine: ]]>
</text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="30">
<textField>
<reportElement x="0" y="0" width="69" height="24"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{tailNum}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="140" y="0" width="69" height="24"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{aircraftSerial}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="280" y="0" width="69" height="24"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{aircraftModel}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="420" y="0" width="69" height="24"/>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{engineModel}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>


调用
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);


5.对象集合
和对象数组基本上一样,就是最后生成的是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);


6.XML数据成生报表(XML as Datasource)
<?xml version="1.0" encoding="UTF-8"?>
<AircraftData>
<aircraft>
<tail_num>N263Y</tail_num>
<aircraft_serial>T-11</aircraft_serial>
<aircraft_model>39 ROSCOE TRNR RACER</aircraft_model>
<engine_model>R1830 SERIES</engine_model>
</aircraft>
<aircraft>
<tail_num>N4087X</tail_num>
<aircraft_serial>BA100-163</aircraft_serial>
<aircraft_model>BRADLEY AEROBAT</aircraft_model>
<engine_model>R2800 SERIES</engine_model>
</aircraft>
<aircraft>
<tail_num>N43JE</tail_num>
<aircraft_serial>HAYABUSA 1</aircraft_serial>
<aircraft_model>NAKAJIMA KI-43 IIIA</aircraft_model>
<engine_model>R1830 SERIES</engine_model>
</aircraft>
<aircraft>
<tail_num>N912S</tail_num>
<aircraft_serial>9973CC</aircraft_serial>
<aircraft_model>PA18-150</aircraft_model>
<engine_model>R-1820 SER</engine_model>
</aircraft>
</AircraftData>

这样获得
JRXmlDataSource xmlDataSource = new JRXmlDataSource(
new BufferedInputStream(getServletConfig().getServletContext()
.getResourceAsStream("/reports/AircraftData.xml")),
"/AircraftData/aircraft");


JREmptyDataSource,JRMapArrayDataSource,JRMapCollectionDataSource,JRBeanArrayDataSource,JRBeanCollectionDataSource,JRXmlDataSource
他们都implements JRDataSource
主要目地就是不从数据库里面取数据,这个方面和Hibernate结合

你可能感兴趣的:(xml,Hibernate,Web,servlet,F#)