如何使用Jasper Report在Java bean中生成带有3个嵌套列表的表?

时间:2019-04-10 14:21:31

标签: java eclipse pdf jasper-reports javabeans

我正在jasper report中创建一个带有3个嵌套列表的表,并且我成功地创建了该表直到第二个列表,但是对于第三个列表,它不起作用。有什么办法吗?我正在为第二个列表使用子报表。

// Java代码

public static void main(String[] args) {
    String outFile = "Report.pdf";

    Util util = new Util();

    /* Create Vscs */


    /* Create Categories */

    List<Category> catogeries = new ArrayList<Category>();
    int randomLength = util.getNewRandomNumber();
    System.out.println("***************************");
    System.out.println("Total Categories Length "+randomLength);
    for (int i = 0; i < randomLength; i++) {

        List<Vsc> Vscs = new ArrayList<Vsc>();

        int vscLength = util.getNewRandomNumber();

        System.out.println("***************************");
        System.out.println("Total VSCs Length "+vscLength);
        for (int j = 0; j < vscLength; j++) {
            Integer id = util.getNewRandomNumber();
            String temp = util.getNewRandomString();
            Vscs.add(new Vsc(id, temp, temp, temp, id.toString(), id.toString(), id.toString()));
        }
        System.out.println(Vscs);
        String temp = util.getNewRandomString();
        catogeries.add(new Category(temp, Vscs));
    }

    System.out.println(catogeries);
    /* Convert List to JRBeanCollectionDataSource */
    JRBeanCollectionDataSource CategoriesJRBean = new JRBeanCollectionDataSource(
            catogeries);

    /* Map to hold Jasper report Parameters */
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("CategoryDataSource", CategoriesJRBean);

    /* Using compiled version(.jasper) of Jasper report to generate PDF */

    try {

        /* Using compiled version(.jasper) of Jasper report to generate PDF */
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                "template.jasper", parameters, CategoriesJRBean);

        /* outputStream to create PDF */

        OutputStream outStream = new FileOutputStream(new File(outFile));

        /* Write content to PDF file */

        JasperExportManager.exportReportToPdfStream(jasperPrint, outStream);

        System.out.println("File Generated");

    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

//碧玉报告主文件代码

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="template" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="1e86ce9a-f7ec-493e-bb1c-193fda92340b">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="CategoryDataset" uuid="3cc9e312-e929-4940-aff6-d8d83e24b723">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="vscs" class="java.util.List"/>
    </subDataset>
    <parameter name="CategoryDataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="vscs" class="java.util.List"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="408">
            <componentElement>
                <reportElement x="0" y="0" width="550" height="180" uuid="26266d16-7d14-4cee-9fbf-c9d1ae5f037c">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="CategoryDataset" uuid="f21c7634-7f14-4267-97b7-517d83b2ca3a">
                        <dataSourceExpression><![CDATA[$P{CategoryDataSource}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="60" uuid="a34069ec-2184-4c0b-8b2d-f9276778ab61">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="60" height="40" uuid="e0d53137-91bf-4314-80be-726bc23897b5"/>
                                <text><![CDATA[Category]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="50" uuid="4b3bef69-ea8f-43d9-b99d-a1360e280479"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="2e1524b4-9536-482d-b62c-5754432a9637">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="50" height="40" uuid="d4623cd9-e0d6-415f-ad25-8b7a64a96f36"/>
                                <text><![CDATA[VSC Code]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <subreport>
                                <reportElement x="0" y="0" width="50" height="50" uuid="f07c137e-f8dd-4ffd-94c7-f699d54ae171"/>
                                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{vscs})]]></dataSourceExpression>
                                <subreportExpression><![CDATA["testreport.jasper"]]></subreportExpression>
                            </subreport>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="80" uuid="5af871b9-e5dd-4339-b849-0d86f184ac44">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="80" height="40" uuid="85742d12-174a-4cc8-9634-5da9a2562e52"/>
                                <text><![CDATA[Item]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <subreport>
                                <reportElement x="0" y="0" width="80" height="50" uuid="f3ae48d0-b447-4e53-97d3-3cec524e31b5"/>
                                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{vscs})]]></dataSourceExpression>
                                <subreportExpression><![CDATA["VscItem.jasper"]]></subreportExpression>
                            </subreport>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="70" uuid="47b5cc43-a32f-493f-9b84-35caf34c57fa">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="70" height="40" uuid="fed179b7-c0e5-43e3-a789-54e125c3ed75"/>
                                <text><![CDATA[Description]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <subreport>
                                <reportElement x="0" y="0" width="70" height="50" uuid="0193c06f-fbf7-4428-9e40-109b8cdcc89c"/>
                                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{vscs})]]></dataSourceExpression>
                                <subreportExpression><![CDATA["VscDesc.jasper"]]></subreportExpression>
                            </subreport>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="80" uuid="9852409d-8381-401e-bc68-014b1ffb6803">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="80" height="40" uuid="83b37e89-0ee3-41ed-bd06-4c60c5c7359b"/>
                                <text><![CDATA[Unit]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <subreport>
                                <reportElement x="0" y="0" width="80" height="50" uuid="0781127c-0fb4-4226-a688-9147eb37ba40"/>
                                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{vscs})]]></dataSourceExpression>
                                <subreportExpression><![CDATA["VscUnit.jasper"]]></subreportExpression>
                            </subreport>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="50" uuid="f794b140-1b69-43a3-9e35-8d96408193b8">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column6"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="50" height="40" uuid="e88c6f68-4328-487a-892d-a1ded1d40c37"/>
                                <text><![CDATA[Approx. Unit Cost (USD)]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <subreport>
                                <reportElement x="0" y="0" width="50" height="50" uuid="7adf0000-1972-47cc-a1fa-1972ef237cac"/>
                                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{vscs})]]></dataSourceExpression>
                                <subreportExpression><![CDATA["VscApproxUnitCost.jasper"]]></subreportExpression>
                            </subreport>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="70" uuid="bfcb8fc6-07ac-4343-9ffd-637cd52d09c2">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column7"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="70" height="40" uuid="3d49b022-0ab1-44b8-9276-8c1cf6df98b0"/>
                                <text><![CDATA[PHY. INV.]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <subreport>
                                <reportElement x="0" y="0" width="70" height="50" uuid="6c8a56b6-1c04-4651-9eb8-7b75e441a576"/>
                                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{vscs})]]></dataSourceExpression>
                                <subreportExpression><![CDATA["VscPhyInv.jasper"]]></subreportExpression>
                            </subreport>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="60" uuid="341a99c2-ff27-446d-a758-5a4bc3a1134c">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column8"/>
                        <jr:tableHeader style="Table_TH" height="40" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="60" height="40" uuid="f83fb9c8-c6da-4e8b-b8ed-6c5021c64925"/>
                                <text><![CDATA[REQ.QTY]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell style="Table_TD" height="50">
                            <subreport>
                                <reportElement x="0" y="0" width="60" height="50" uuid="14363048-9a16-4be9-badf-d72af940c385"/>
                                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{vscs})]]></dataSourceExpression>
                                <subreportExpression><![CDATA["VscReqQty.jasper"]]></subreportExpression>
                            </subreport>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

运行代码后得到的输出。

实际输出

enter image description here

预期产量

enter image description here

谢谢

1 个答案:

答案 0 :(得分:0)

让我使用示例图像的术语,其中我们要遍历国家,因为每个迭代都遍历每个城市,因此要遍历每个放置

在我看来,您最里面的迭代(示例图像中的 Places )需要是每个 City 的迭代。因此,您的子报表必须是一个子表,范围涵盖所有三列。

enter image description here

尽管这感觉像是一种骇人听闻的方式。添加迭代需要大量的单击(如果直接在XML上工作,则可以键入)。而且布局仅在本地发生,因此一个 city 的places列的列宽可能与另一个 city 的列宽不同。看到带有粉红色箭头的金线。

enter image description here

我目前正面临着同样的挑战。解决此问题的唯一方法是使这些列具有固定的宽度。