gpt4 book ai didi

java - 如何使用列表在 JasperReports 中生成动态表

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:07:13 28 4
gpt4 key购买 nike

我正在尝试在 Jasper 中生成表格报告。具体来说,我正在尝试像这样遍历表中的列表:

Java 代码:

List<String> severity = (List<String>)session.getAttribute("severity"); 
List<String> messages = (List<String>)session.getAttribute("messages");

LinkedHashMap parameters = new LinkedHashMap();
parameters.put("severity", severity);
parameters.put("messages", messages);

jPrint = JasperFillManager.fillReport(jasperreport,parameters,new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(jPrint,baos);

JRXML:

<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="jasper_report_template" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a3d5ea75-61fe-4bd0-a2ef-43652ca82bb5">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="severity" class="java.util.List">
<fieldDescription><![CDATA[severity]]></fieldDescription>
</field>
<field name="messages" class="java.util.List">
<fieldDescription><![CDATA[messages]]></fieldDescription>
</field>
<columnHeader>
<band height="23" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="-180" width="535" height="15" backcolor="#70A9A9" uuid="9b5b79e0-e146-41d9-b665-6cf9441c44f7"/>
<box>
<bottomPen lineWidth="1.0" lineColor="#CCCCCC"/>
</box>
<text><![CDATA[ ]]></text>
</staticText>
<staticText>
<reportElement x="290" y="0" width="121" height="15" uuid="427a0dc3-acc5-445d-afdf-325f56220d5b"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Error Severity]]></text>
</staticText>
<staticText>
<reportElement x="0" y="3" width="136" height="15" uuid="9d05c15b-478c-49d3-a334-c5d6bfc62e53"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Erorr Messages]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="16" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="535" height="14" backcolor="#E5ECF9" uuid="b0ac6c6e-6d86-44de-801c-fcb85f1c5cb7"/>
<box>
<bottomPen lineWidth="0.25" lineColor="#CCCCCC"/>
</box>
<text><![CDATA[ ]]></text>
</staticText>
<textField>
<reportElement x="290" y="0" width="121" height="15" uuid="d106ecf9-2dc8-49fb-b63c-e348b96f871d"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{severity}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="136" height="15" uuid="d106ecf9-2dc8-49fb-b63c-e348b96f871d"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{messages}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

所以我的问题是:

1)如何在Jrxml中打印这些列表?

2) 这是在 JRXML 中遍历列表的正确方法吗?

3)如何写JRXML代码迭代List? (子报表在这种情况下有用吗?)

最佳答案

传递两个 LinkedHashMap 并不是您需要实现的最佳数据结构。

如果您像下面这样更改您的数据结构,您的 jrxml 只需稍作更改即可工作。

创建一个包含您的错误的 bean。

public class ErrorBean {
private String severity;
private String messages;
public String getSeverity() {
return severity;
}
public void setSeverity(String severity) {
this.severity = severity;
}
public String getMessages() {
return messages;
}
public void setMessages(String messages) {
this.messages = messages;
}
}

用数据填充你的 bean

//Pass our two LinkList to our new ErrorBean
List<ErrorBean> errorBeans = new ArrayList<ErrorBean>();
for (int i = 0; i < severity.size(); i++) {
ErrorBean eb = new ErrorBean();
eb.setSeverity(severity.get(i));
if (messages.size()>i){
eb.setMessages(messages.get(i));
}
errorBeans.add(eb);
}

errorBeans 传递给 JRBeanCollectionDataSource 中的 JasperReport

Map<String,Object> parameters = new HashMap<String,Object>();
jPrint = JasperFillManager.fillReport(jasperreport,parameters,new JRBeanCollectionDataSource(errorBeans));

现在你的设置在 jrxml 中唯一需要的小修改是定义正确的字段类(从 java.util.List 切换到 java.lang .字符串)

<field name="severity" class="java.lang.String">
<fieldDescription><![CDATA[severity]]></fieldDescription>
</field>
<field name="messages" class="java.lang.String">
<fieldDescription><![CDATA[messages]]></fieldDescription>
</field>

关于java - 如何使用列表在 JasperReports 中生成动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34736221/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com