传递ArrayList报告会在每个项目中显示逗号

我通过参数将List of String传递给JasperReports报告。

String jasperFileName = "C:\\TestReportProcess.jasper"; Map params = new HashMap(); params.put("List", getMyListOfString()); JasperPrint jprint = (JasperPrint) asperFillManager.fillReport(jasperFileName, params, new JREmptyDataSource()); 

当报告开始时,每个项目都显示逗号

 item 1, item 2, item 3, item 4, etc etc 

怎么能避免呢?

我的碧玉报告xml

               

这是我简单的xml报告,只有一个参数java.util.Arraylist

您可以通过多种方式传递List

  1. 传递List作为参数并在subDataset中使用此参数(在dataSourceExpression中
  2. JRBeanCollectionDataSource的帮助下将List作为JRDataSource传递
  3. List转换为String对象并将逗号替换为回车符( \ n ),或者在String.replace方法的帮助下删除逗号。

该示例显示了这两种方法

Java代码

我们使用List填充listOfItems参数,并使用JRBeanCollectionDataSource填充报告。

 JRDataSource dataSource = new JRBeanCollectionDataSource(Arrays.asList("item 1", "item 2", "item 3", "item 4", "item 5", "item 6")); Map params = new HashMap<>(); params.put("listOfItems", Arrays.asList("Title 1", "Title 2", "Title 3")); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource); 

报告模板

数据源包含6个项目(元素),用于在详细信息带(主数据集)中显示。

参数listOfItems包含在Table组件的subDataset帮助下在Title band中显示的3个元素的列表。

摘要带用于显示如何仅使用一个textField元素显示ListlistOfItems参数)中的数据。

fieldDescription帮助我们获得字段的值。 在_THIS关键字的帮助下,我们得到String值。

             <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"></property> </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"></reportelement> <textfieldexpression><![CDATA[$F{name}]]></textfieldexpression> </textfield> </jr:detailcell> </jr:column> </jr:table> </componentelement> </band>                       

List.toString方法的用法给出如下结果: [val1, val2] – 用逗号分隔并用方括号括起来的值。 String.replace方法的使用(这个方法的几个串行调用)给我们很好的结果。

输出结果

JRPdfExporter的帮助下生成的pdf文件如下所示:

生成pdf