如何将分离的String转换为表格布局? [英] How to convert a , separated String to a table layout?
问题描述
我有一个值为的字段:
CM45024,CM45025,CM45026
我想使用子报告将其分成多个项目。我的jrxml源是:
I want to separate this into multiple items using subreport. My jrxml source is:
<field name="docIdNoGRN" class="java.lang.String">
<fieldDescription><![CDATA[docIdNoGRN]]></fieldDescription>
</field>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement key="textField-53" x="311" y="1" width="88" height="15" uuid="2e040fd0-8fae-46e8-a845-fba421922992"/>
<textElement textAlignment="Center">
<font size="10"/>
</textElement>
<textFieldExpression><![CDATA[($F{docIdNoGRN} != null && $F{docIdNoGRN}.toString().length() > 0) ? $F{docIdNoGRN} : " "]]>
</textFieldExpression>
</textField>
该字段可以有3个或更多项,因为它取决于数据。而不是在报告中有一个项目:
The field can have 3 or more items as it depends on the data. Instead of having one item in the report:
Item No. Item ID.
1 CM45024,CM45025,CM45026
我想这样显示:
Item No. Item ID.
1 CM45024
2 CM45025
3 CM45026
我正在使用TIBCO Jaspersoft®StudioProfessional - JasperReport的可视化设计器6.1.1。
Im using TIBCO Jaspersoft® Studio Professional - Visual Designer for JasperReport 6.1.1.
推荐答案
这是如何使用子报表实现的,假设你的 $ F {docIdNoGRN}
包含字符串CM45024,CM45025,CM45026
This is how it can be achieved using subreport, assuming that your $F{docIdNoGRN}
contains the String "CM45024,CM45025,CM45026"
-
生成将传递给子报告的数据源
Generate the data source that will be passed to the subreport
<subreport>
<reportElement x="163" y="15" width="200" height="100" uuid="9d6660e0-094e-4df3-9acb-74c031350b10"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(java.util.Arrays.asList($F{docIdNoGRN}.split(",")))]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "your_subreport.jasper"]]></subreportExpression>
</subreport>
如你所见,我 split(,)
你的字段字符串
获得转换的数组
到列表
然后传入 JRBeanCollectionDataSource
As you can see I split(",")
your field String
getting an Array
that is converted to List
and then passed in a JRBeanCollectionDataSource
- 设置子报表,引用我们的String的技巧是使用 _THIS 作为字段值
- Setup the subreport, the trick to reference our String is using _THIS as field value
示例:( your_subreport.jrxml ):
<?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="jTest_subreport2" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="23d7765f-250f-4632-94c6-bbd218db3d11">
<field name="_THIS" class="java.lang.String"/>
<detail>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20" uuid="716bb440-2692-4c58-a1b7-972aff240c67"/>
<textFieldExpression><![CDATA[$V{REPORT_COUNT}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="e5464783-d74a-4405-9997-ddb1531c6e42"/>
<textFieldExpression><![CDATA[$F{_THIS}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
我添加了 $ V {REPORT_COUNT}
实现数字(1,2,3)。
I have added the $V{REPORT_COUNT}
to achieve the numbers (1,2,3) .
输出
1 CM45024
2 CM45025
3 CM45026
1 CM45024
2 CM45025
3 CM45026
注意:你如果你想避免生成子报表(只需像上面那样定义表数据源),也可以使用jr:table组件来实现这个目的。
Note: You can also use the jr:table component to achieve this if you like to avoid generating a subreport (just define the table datasource as above)
这篇关于如何将分离的String转换为表格布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!