java - 查看报表中的对象集合

标签 java swing jasper-reports

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

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

公共(public)类颜色{int id;字符串名称;//以及 getter 和 setter } 公共(public)类类别{int id;字符串名称;//以及 getter 和 setter} 然后集合中有 Item 对象。我想在 jasper 报告中显示项目的颜色、类别 ID 和名称。请任何人告诉我如何做到这一点(如何在 jasper 报告中编写表达式;jrxml)。 buyList 是我的由 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();
    }`

最佳答案

为了显示例如。您必须在 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 的一个暗示性示例。我认为您可以将其调整为您需要的其他字段,相当简单。

关于java - 查看报表中的对象集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7847425/

相关文章:

java - 如何使用 InputMap 捕获 CTRL + mouseWheel 事件

java - 在 Java 中将枚举值绑定(bind)到 RadioButton

java - 如何使用 JasperReports 自动生成报告

java - 如何将jasper文件路径传递给子报表的子报表

java - 根据列值更改JTable整行的背景颜色

java - 这两种从 LocalDateTime 创建 DateTime 的方法有什么区别?

java - Swing GUI 和翻译器问题

java - 贾斯珀报告 : wrong positioning of text fields in Internet Explorer

java - 我的一个面板没有显示

java - 如何知道属性文件中是否存在属性?