简体   繁体   English

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

[英]How to generate table with 3 nested list in java beans using jasper report?

I am creating a table in jasper report with 3 nested lists and I successfully created the table till the 2nd list but for the 3rd list, it is not working. 我正在jasper report中创建一个带有3个嵌套列表的表,并且成功创建了该表直到第二个列表,但是对于第三个列表,它不起作用。 is there any way to do this? 有什么办法吗? I am using sub-report for 2nd list. 我正在为第二个列表使用子报告。

// Java code // 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();
    }

}

// jasper report main file code //碧玉报告主文件代码

<?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>

The output which I am getting after running the code. 运行代码后得到的输出。

actual output 实际产量

在此处输入图片说明

expected output 预期产量

在此处输入图片说明

Thank you 谢谢

Let me use the terms of the example image where we are iterating over Countries , foreach which we iterate over each City , for which we iterate over each Place . 让我使用示例图像的术语,其中遍历国家/地区 ,foreach遍历每个城市 ,遍历每个地方

It looks to me that your inner-most iteration ( Places in the example image) needs to be an iteration per City . 在我看来,您最里面的迭代(示例图像中的Places )需要是每个City的迭代。 Thus, your subreport needs to be a subtable, ranging over all three columns. 因此,您的子报表需要是一个子表,范围涵盖所有三列。

 </ s> </ s> </ s> 在此处输入图片说明

This feels like a hacky way to do this though. 不过,这感觉像是一种拙劣的方式。 Adding an iteration is a lot of clicking (or typing if working on XML directly). 添加迭代需要大量的单击(如果直接在XML上工作,则可以键入)。 And the layouting only happens locally, so that the column widths of the places column of one city are potentially different for another city . 而且布局仅在本地进行,因此一个城市的places列的列宽可能与另一座城市不同。 See the golden lines with the pink arrows. 看到带有粉红色箭头的金线。

 </ s> </ s> </ s> 在此处输入图片说明

I am currently facing the same challenge. 我目前正面临着同样的挑战。 The only way to fix this that I came up with was to make these columns have fixed widths. 解决此问题的唯一方法是使这些列具有固定的宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM