我需要在ireport中进行设计。
我有三个VO
DeliveryAllocations {
private String collectorCode;
private String collectorName;
private String month;
private List<PlantVO> plants = new ArrayList<PlantVO>();
with setter/getters
}
PlantVO{
private String plantCode;
private String plantName;
private String TotalWeight;
private List<PlantAllocationVO> allocations = new ArrayList<PlantAllocationVO>();
with setter/getters
}
PlantAllocationVO{
private String weight;
private String customerType;
private String customerValue;
private String comment;
private String date;
with setters/getters
}
现在,我想在表格格式的报告中显示PlantVO
的两个字段和PlantAllocationVO
的三个字段。
我如何实现这一目标? 如何获取数据以设计具有此类结构的报告?
答案 0 :(得分:1)
这可以通过使用子报告并作为数据源传递来完成
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{allocations})
示例代码:
主要报告(PlantVO.jrxml
)
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PlantVO" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="55da4c97-0a7c-447f-b3a7-2713d483523d">
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
</parameter>
<field name="plantName" class="java.lang.String"/>
<field name="totalWeight" class="java.lang.Double"/>
<field name="allocations" class="java.util.List"/>
<detail>
<band height="60" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="211879f7-331e-4526-bb0d-e7f0314f71b3"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA["Plant name: " + $V{REPORT_COUNT}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="200" height="20" uuid="f2d206d5-61fb-4238-93cf-c7dd16403a48"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{plantName}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="100" y="20" width="400" height="20" uuid="f3ca3eba-cb93-4ab4-8931-c399a8430841"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{allocations})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "PlantAllocationVO_subreport.jasper"]]></subreportExpression>
</subreport>
<staticText>
<reportElement positionType="Float" x="100" y="40" width="200" height="20" uuid="db7b40e6-37fe-4815-b912-fc5afaf966fb"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<text><![CDATA[TOTAL WEIGHT:]]></text>
</staticText>
<textField pattern="###0">
<reportElement positionType="Float" x="300" y="40" width="200" height="20" uuid="f1670bb4-efec-492c-b69c-0de668bda244"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{totalWeight}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
子报告(PlantAllocationVO_subreport
)
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="PlantAllocationVO_subreport" pageWidth="400" pageHeight="802" columnWidth="400" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="417a7f13-6776-4773-94ab-0be5c01605c7">
<field name="date" class="java.lang.String"/>
<field name="weight" class="java.lang.Double"/>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="200" height="20" uuid="34424fa2-18d0-4859-825a-a07f2a826f55"/>
<textElement verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
</textField>
<textField pattern="###0">
<reportElement x="200" y="0" width="200" height="20" uuid="a0e2ae10-906e-4d0f-aebd-30fc0c694aca"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{weight}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
使用主代码
JasperReport report = JasperCompileManager.compileReport("PlantVO.jrxml");
Map<String, Object> map = new HashMap<String, Object>();
JasperPrint print = JasperFillManager.fillReport(report, map,new JRBeanCollectionDataSource(deliveryAllocations.getPlants()));
JasperExportManager.exportReportToPdfFile(print, "PlantVO.pdf");
将产生结果:
一些设计说明:
我会保留正确的字段类型(例如weight
为Double
或
Integer
)并在jrxml中应用模式(参见示例),这将有所帮助
你确实正确导出到excel。
TotalWeight
没有必要将此作为字段,它可以
由子报告计算并传回主报告。
玩得开心