查看报表中的对象集合

时间:2011-10-21 09:25:11

标签: java swing jasper-reports

有一个对象集合。我想在jasper报告中显示它。让我们说Item类;

public class Item{ Color color; Category category;  and getters and setters for color & category}  

public class Color{int id; String name; //and getters and setters} public class Category {int id; String name; // and getters and setter} 然后该集合具有Item对象。我想在jasper报告中显示项目的颜色,类别ID和名称。请任何人告诉我如何做到这一点(如何在jasper报告中写出表达式; jrxml)。 purchaseList是我的集合,由Item对象组成。

    `try {
        InputStream in = getClass().getResourceAsStream("/Reports/invoice.jasper");
        Map<String, Object> params = new HashMap<String, Object>();

        JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(purchasingList);

        JasperPrint jasperPrint = JasperFillManager.fillReport(in, params, datasource);
        JasperViewer.viewReport(jasperPrint, true);

    } catch (Exception e) {
        e.printStackTrace();
    }`

1 个答案:

答案 0 :(得分:1)

为了表示例如。您需要在jrxml

中添加类似内容的类别ID
<?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">
    <property name="ireport.zoom" value="1.0" />
    <property name="ireport.x" value="0" />
    <property name="ireport.y" value="0" />
    <style name="Heading 1" isDefault="false" fontSize="16" isBold="true" />
    <queryString>
                <![CDATA[]]>
    </queryString>
    <field name="category" class="Category">
        <fieldDescription><![CDATA[category]]></fieldDescription>
    </field>
    <title>
        <band height="21">
            <staticText>
                <reportElement style="Heading 1"/>
                <textElement textAlignment="Center">
                    <font isUnderline="true" />
                </textElement>
                <text>Purchases</text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band splitType="Stretch">
            <staticText>
                <reportElement/>

                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Arial" size="12" isBold="true" />
                </textElement>
                <text>Category Id</text>
            </staticText>
    </pageHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement/>
                <textElement verticalAlignment="Bottom">
                    <font fontName="Arial" isItalic="true" />
                </textElement>
                <textFieldExpression class="java.lang.Integer"><![CDATA[$F{category}.getId()]]></textFieldExpression>
            </textField>
    </detail>
</jasperReport>

您可以根据需要更改格式选项。

同样,这是类别ID的一个启发性的例外。我可以把它调整到你需要的其他领域,相当容易。