【问题标题】:How to convert a , separated String to a table layout?如何将分隔的字符串转换为表格布局?
【发布时间】:2015-11-15 14:05:31
【问题描述】:

我有一个包含值的字段:

CM45024,CM45025,CM45026

我想使用子报表将其分成多个项目。我的 jrxml 来源是:

<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 个或更多项,因为它取决于数据。而不是在报告中包含一项:

Item No.    Item ID.
   1        CM45024,CM45025,CM45026

我想这样展示它:

Item No.    Item ID.
   1        CM45024
   2        CM45025
   3        CM45026

我正在使用 TIBCO Jaspersoft® Studio Professional - JasperReport 6.1.1 的可视化设计器。

【问题讨论】:

  • 是的,不确定是否将此代码放在源代码中以及我需要添加的组件中。我对 jrxml 很陌生,只知道基本的东西。你在线吗?我面临的一些问题: 1. 如果我添加 SubReport 元素,我不知道如何将其配置为指向 $F{docIdNoGRN} 2.
  • 运行时出现错误:[STDOUT] net.sf.jasperreports.engine.design.JRValidationException:报告设计无效:1. 未找到字段:_THIS
  • 我已经在答案中粘贴的子报表代码(您可以复制过去),如果您的jasper报表版本为5.0或更高版本,它将起作用(您只能在子报表中引用_THIS。
  • 您在主报告中放置的 标记(详细信息带),请务必指向 your_subreport.jasper .jasper 文件(绝对路径),并且您已遵守 your_subreport。 jrxml,在我的示例中,我使用参数传递绝对路径 $P{SUBREPORT_DIR},但您也可以在 subreportExpression 中对其进行硬编码

标签: jasper-reports subreport


【解决方案1】:

假设您的$F{docIdNoGRN} 包含字符串“CM45024,CM45025,CM45026”,这就是使用子报告实现的方法

  1. 生成将传递给子报表的数据源

    <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(",") 你的字段 String 得到一个 Array 转换为 List 然后传入 JRBeanCollectionDataSource

  1. 设置子报表,引用我们的字符串的技巧是使用 _THIS 作为字段值

示例:(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)。

输出将是

1 CM45024
2 CM45025
3 CM45026

注意:如果您不想生成子报表,也可以使用 jr:table 组件来实现这一点(只需像上面那样定义表数据源)

【讨论】:

  • 这是一个真正的死灵,我知道 - 但这显然是互联网上唯一易于理解的答案。这将如何与列表和表格一起使用?我是 Jasper Reports 的新手,无法正常工作。
猜你喜欢
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多