Java 传递ArrayList报告会显示每个项目的逗号
我通过参数将字符串列表传递给JasperReports报告Java 传递ArrayList报告会显示每个项目的逗号,java,arraylist,jasper-reports,Java,Arraylist,Jasper Reports,我通过参数将字符串列表传递给JasperReports报告 String jasperFileName = "C:\\TestReportProcess.jasper"; Map<String, Object> params = new HashMap<String, Object>(); params.put("List", getMyListOfString()); JasperPrint jprint = (JasperPrint) asperFillManager
String jasperFileName = "C:\\TestReportProcess.jasper";
Map<String, Object> params = new HashMap<String, Object>();
params.put("List", getMyListOfString());
JasperPrint jprint = (JasperPrint) asperFillManager.fillReport(jasperFileName, params, new JREmptyDataSource());
怎样才能避免呢
我的jasper报告xml
<parameter name="List" class="java.util.ArrayList" isForPrompting="false"/>
<detail>
<band height="280" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="">
<reportElement x="0" y="13" width="550" height="45" uuid="f907894e-e9f1-418b-9ab8-1db276b8482e"/>
<textElement>
<font fontName="Antique Olive Compact"/>
</textElement>
<textFieldExpression><![CDATA[$P{List}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
这是我的简单xml报告,只有一个参数java.util.Arraylist您可以通过多种方式传递列表
:
列表
作为参数传递,并在子数据集中(在dataSourceExpression中)使用此参数列表
列表
转换为字符串对象,并将逗号替换为回车符(\n),或借助String.replace方法删除逗号List
填充listOfItems参数,并使用JRBeanCollectionDataSource填充报告
jrdasource dataSource=新的JRBeanCollectionDataSource(Arrays.asList(“第1项”、“第2项”、“第3项”、“第4项”、“第5项”、“第6项”);
Map params=新的HashMap();
参数put(“列表项”,数组.asList(“标题1”,“标题2”,“标题3”);
JasperPrint JasperPrint=JasperFillManager.fillReport(jasperReport,参数,数据源);
报告模板
数据源包含6个项目(元素),用于在详细信息栏(主数据集)中显示
参数listOfItems包含3个元素的列表,这些元素借助于表组件的子数据集显示在标题栏中
摘要栏用于显示如何仅使用一个textField元素显示来自列表的数据(listOfItems参数)
fieldDescription帮助我们获取字段的值。借助_这个关键字,我们可以得到字符串值
方法的使用会产生如下结果:[val1,val2]
-用逗号分隔并用方括号括起来的值。String.replace方法(该方法的几个串行调用)的使用给了我们很好的结果
输出结果
在JRPdfExporter的帮助下生成的pdf文件如下所示:
您应该发布您的jrxml文件Alex,该文件使用jrxml更新,可能重复&&您应该将转换列表传递到数据源您可以使用列表
<parameter name="List" class="java.util.ArrayList" isForPrompting="false"/>
<detail>
<band height="280" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="">
<reportElement x="0" y="13" width="550" height="45" uuid="f907894e-e9f1-418b-9ab8-1db276b8482e"/>
<textElement>
<font fontName="Antique Olive Compact"/>
</textElement>
<textFieldExpression><![CDATA[$P{List}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
JRDataSource dataSource = new JRBeanCollectionDataSource(Arrays.asList("item 1", "item 2", "item 3", "item 4", "item 5", "item 6"));
Map<String, Object> params = new HashMap<>();
params.put("listOfItems", Arrays.asList("Title 1", "Title 2", "Title 3"));
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
<?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" name="Passing List of String" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<subDataset name="listDataset">
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
</subDataset>
<parameter name="listOfItems" class="java.util.List"/>
<field name="item" class="java.lang.String">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
<title>
<band height="27">
<componentElement>
<reportElement x="340" y="0" width="200" height="15">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
</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="listDataset">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{listOfItems})]]></dataSourceExpression>
</datasetRun>
<jr:column width="200">
<jr:detailCell height="15">
<textField>
<reportElement x="0" y="0" width="200" height="15"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</title>
<detail>
<band height="15" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="15"/>
<textFieldExpression><![CDATA[$F{item}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="60" splitType="Stretch">
<textField>
<reportElement x="0" y="15" width="100" height="15" />
<textFieldExpression><![CDATA[$P{listOfItems}.toString().replace(",", " ").replace("[", "").replace("]", "")]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="300" y="40" width="100" height="15"/>
<textFieldExpression><![CDATA[$P{listOfItems}.toString().replace(", ", "\n").replace("[", "").replace("]", "")]]></textFieldExpression>
</textField>
</band>
</summary>
</jasperReport>