有一个对象集合。我想在 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/