如何设计表格格式的报告?

How to design report with tabular format?

我有在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 的三个字段。

我该如何实现? 如何获取数据以设计具有这种结构的报表?

这是通过使用 子报表 并作为数据源传递来完成的

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>

main代码

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");

将产生这个结果:

一些设计笔记:

  1. 我会保留正确类型的字段(例如 weight 作为 DoubleInteger) 并在 jrxml 中应用模式(参见示例),这将有所帮助 您正确导出到 excel.

  2. TotalWeight 没有必要把它作为一个字段,它可以 由子报表计算并传回主报表。

玩得开心