为什么只打印我的数据源的第一个对象?

时间:2019-05-20 13:07:33

标签: java jasper-reports subreport

我正在尝试打印报告。我向报告1发送了一个包含3个对象的数据源。 乐队详细信息包含一个子报告(报告2)。 我的问题:为什么只打印一次?

每个发送的对象包含另一个带有3个对象的数据源。 报表2将此数据源发送到报表3,并打印行。 (我的期望如何。)

为什么从报告2到报告3起作用?为什么从报告1到报告2不起作用?

这是一个JavaFX应用程序。

我的测试:

try {
   File report1File = Actions.getReportFromPackage("layout/spProgramm.jrxml");
   File report2File = Actions.getReportFromPackage("layout/spWeek.jrxml");

   JasperReport report1 = JasperCompileManager.compileReport(report1File.getAbsolutePath());
   JasperReport report2 = JasperCompileManager.compileReport(report2File.getAbsolutePath());

   ArrayList<JRWeek> report2ArrayList = new ArrayList<>();

   report2ArrayList.add(JRWeek.newObject("1"));
   report2ArrayList.add(JRWeek.newObject("2"));
   report2ArrayList.add(JRWeek.newObject("3"));

   JRBeanCollectionDataSource report2DataSource = new JRBeanCollectionDataSource(report2ArrayList);

   Map<String, Object> parameters = new HashMap<String, Object>();
   parameters.put("report2", report2);
   parameters.put("report2DataSource", report2DataSource);

   JasperPrint jasperPrint = JasperFillManager.fillReport(report1, parameters, new JREmptyDataSource());

   JasperViewer jv = new JasperViewer(jasperPrint, false);
} catch (JRException e) {
   System.out.println(e.getMessage());
}

班级:JRWeek

public class JRWeek {

  private JasperReport report3;
  private JRBeanCollectionDataSource report3DataSource;

  public static JRWeek newObject(String week) throws JRException {
    JRWeek jrWeek = new JRWeek();

    File report3File = Actions.getReportFromPackage("layout/spRows.jrxml");
    JasperReport report3 = JasperCompileManager.compileReport(report3File.getAbsolutePath());

    ArrayList<JRRow> jrRows = new ArrayList<>();
    jrRows.add(new JRRow("REPORT 3 WEEK" + week, "ROW 1"));
    jrRows.add(new JRRow("REPORT 3 WEEK" + week, "ROW 2"));
    jrRows.add(new JRRow("REPORT 3 WEEK" + week, "ROW 3"));

    jrWeek.setReport3(report3);
    jrWeek.setReport3DataSource(new JRBeanCollectionDataSource(jrRows));

    return jrWeek;
  }
// Getter and Setter
}

类别:JRRow

public class JRRow {

  private String text1;
  private String text2;

  public JRRow(String text1, String text2) {
    super();
    this.text1 = text1;
    this.text2 = text2;
  }
// Getter and Setter
}

报告1:spProgramm.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="spMeetingProgramm" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0053568c-4b27-4125-bdd4-3172dc64051b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <parameter name="report2" class="net.sf.jasperreports.engine.JasperReport"/>
    <parameter name="report2DataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <pageHeader>
        <band height="60">
            <rectangle>
                <reportElement x="0" y="0" width="555" height="60" forecolor="#FFFFFF" backcolor="#0B6798" uuid="976d792c-c89b-4ea1-bda7-af6b30392492"/>
                <graphicElement>
                    <pen lineWidth="0.0" lineColor="#0B6798"/>
                </graphicElement>
            </rectangle>
            <textField>
                <reportElement x="0" y="0" width="555" height="30" forecolor="#FFFFFF" uuid="43b185ef-16a8-40b2-a933-ecc0fe3adc70"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["REPORT 1 HEADER 1"]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="30" width="555" height="30" forecolor="#FFFFFF" uuid="9172e851-2e29-4f95-9542-519930e7aa3d"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="16" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["REPORT 1 HEADER 2"]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <detail>
        <band height="35">
            <subreport>
                <reportElement stretchType="RelativeToBandHeight" x="0" y="10" width="555" height="25" uuid="aae0c36a-3373-45cb-a030-ed835e3d0005"/>
                <dataSourceExpression><![CDATA[$P{report2DataSource}]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{report2}]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

报告2:spWeek.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="spMeetingWeek" pageWidth="555" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="e6d013b3-aa4c-4ba7-ab71-aaec14a42962">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <field name="report3" class="net.sf.jasperreports.engine.JasperReport"/>
    <field name="report3DataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <group name="weekHeader">
        <groupHeader>
            <band height="17">
                <rectangle>
                    <reportElement x="0" y="0" width="555" height="17" backcolor="#17365D" uuid="70d3c82d-21e2-4dd1-80a9-8ca137ebe569"/>
                    <graphicElement>
                        <pen lineWidth="0.5"/>
                    </graphicElement>
                </rectangle>
                <textField>
                    <reportElement x="5" y="0" width="550" height="17" forecolor="#FFFFFF" uuid="45cdacc1-7833-4296-b942-1ce3ab998189"/>
                    <textElement verticalAlignment="Top">
                        <font fontName="SansSerif" size="12" isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA["REPORT 2 HEADER 1"]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
    <group name="header1">
        <groupHeader>
            <band height="17">
                <rectangle>
                    <reportElement x="0" y="0" width="555" height="17" backcolor="#DEA900" uuid="a5f3a182-963d-456a-9102-de1bbf18895d"/>
                    <graphicElement>
                        <pen lineWidth="0.5"/>
                    </graphicElement>
                </rectangle>
                <textField>
                    <reportElement x="5" y="0" width="550" height="17" forecolor="#FFFFFF" uuid="4f94c435-2a99-4c90-8fea-2d30e7c04272"/>
                    <textElement verticalAlignment="Top">
                        <font fontName="SansSerif" size="12" isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA["REPORT 2 HEADER 2"]]></textFieldExpression>
                </textField>
            </band>
        </groupHeader>
    </group>
    <group name="header1Rows">
        <groupHeader>
            <band height="5">
                <subreport>
                    <reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="555" height="5" uuid="0daf0871-3f2e-4287-b42a-f99b554bcd62"/>
                    <dataSourceExpression><![CDATA[$F{report3DataSource}]]></dataSourceExpression>
                    <subreportExpression><![CDATA[$F{report3}]]></subreportExpression>
                </subreport>
            </band>
        </groupHeader>
    </group>
</jasperReport>

报告3:spRows.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="spMeetingMinistryPartRow" pageWidth="555" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="e6d013b3-aa4c-4ba7-ab71-aaec14a42962">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <field name="text1" class="java.lang.String"/>
    <field name="text2" class="java.lang.String"/>
    <detail>
        <band height="17">
            <rectangle>
                <reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="555" height="17" uuid="09a1f023-2151-43df-831a-6f3a2d19494d"/>
                <graphicElement>
                    <pen lineWidth="0.5"/>
                </graphicElement>
            </rectangle>
            <textField isStretchWithOverflow="true">
                <reportElement stretchType="RelativeToBandHeight" x="60" y="0" width="217" height="17" uuid="0afdce9b-710f-41e3-b271-ca4e828c2be4"/>
                <textElement verticalAlignment="Middle">
                    <font fontName="SansSerif" size="11"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{text1}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" pattern="">
                <reportElement stretchType="RelativeToBandHeight" x="282" y="0" width="134" height="17" uuid="5e325968-5b1f-44f1-a326-89cc52620b6c"/>
                <textElement verticalAlignment="Middle">
                    <font size="11"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{text2}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

结果

enter image description here

1 个答案:

答案 0 :(得分:0)

将报表3 / spRows.jrxml子报表元素放置在报表2 / spWeek.jrxml报表的详细信息栏中。

您现在有了一个没有组表达式的组标题,并且这样的标题在报表中只打印一次。