我想用动态表创建Jasper报告。 例如:
我尝试使用列表选项,但是我的表不可见,并且页面数正在增加。
<subDataset name="tableDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37"> <queryString language="json"> <![CDATA[tableData]]> </queryString> <field name="description" class="java.lang.String"> <fieldDescription><![CDATA[description]]></fieldDescription> </field> </subDataset> <subDataset name="listDataSet" uuid="c72d8dbd-d63e-411b-862d-a0d6e1e25d37"> <queryString language="json"> <![CDATA[listData]]> </queryString> <field name="tableData" class="java.lang.String"> <fieldDescription><![CDATA[tableData]]></fieldDescription> </field> </subDataset> <queryString language="json"> <![CDATA[mainDataSet]]> </queryString> <field name="listData" class="java.lang.String"> <fieldDescription><![CDATA[listData]]></fieldDescription> </field> <componentElement> <reportElement x="-1" y="170" width="830" height="150" uuid="ab0272db-d740-42c9-94e5-238a2cc4a63e"/> <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical"> <datasetRun subDataset="listDataSet" uuid="f80306fb-80fc-405b-b90d-f9fe6b6ee2e1"> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("listData")]]></dataSourceExpression> </datasetRun> <jr:listContents height="150" width="830"> <componentElement> <reportElement positionType="Float" x="0" y="67" width="825" height="76" uuid="6a8e89c4-d4ea-4d8f-b932-8fbb24d00725"/> <jr:table> <datasetRun subDataset="tableDataSet" uuid="6ab7d349-34c8-4db7-bf3c-4e638ca36f5c"> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression> </datasetRun> <jr:column width="100" uuid="aae649c4-6a69-485f-8a0d-87022df793ee"> <jr:columnHeader style="colBotLeftLine" height="30"> <staticText> <reportElement x="0" y="0" width="100" height="30" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/> <textElement> <font isBold="true"/> </textElement> <text><![CDATA[DESCRIPTION]]></text> </staticText> </jr:columnHeader> <jr:columnFooter style="footerBotLeftLine" height="20"> <staticText> <reportElement x="0" y="0" width="100" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/> <textElement> <font isBold="true"/> </textElement> <text><![CDATA[Grand Total]]></text> </staticText> </jr:columnFooter> <jr:detailCell height="20" rowSpan="1"> <textField> <reportElement x="0" y="0" width="100" height="20" uuid="734834e8-5896-4355-9454-6b188e86bfff"/> <textFieldExpression><![CDATA[$F{description}]]></textFieldExpression> </textField> </jr:detailCell> </jr:column> </jr:table> </componentElement> </jr:listContents> </jr:list> </componentElement>
杰森数据。
{mainDataSet : [ {CompanyName : "XYZ", listData: [ {tableId:1, tableName:"Table One", tableData: [ {description:"Table One Row Data One "}, {description:"Table One Row Data Two"} ] }, {tableId:2, tableName:"Table Two", tableData: [ {description:"Table Two Row Data One "}, {description:"Table Two Row Data Two"} ] } ] } ] }
mainDataSet是我的报表数据集,listData是列表数据,tableData也是我要在表中显示的数据。 注意 如果我删除
mainDataSet
listData
tableData
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression>
并把
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
然后,使用空值创建两个表。
对主报表的查询将其保留为空白(并保留mainDataSet节点)
<queryString language="json"><![CDATA[]]></queryString>
将CompanyName字段定义为
CompanyName
<field name="CompanyName" class="java.lang.String"> <fieldDescription><![CDATA[mainDataSet[0].CompanyName]]></fieldDescription> </field>
该dataSourceExpression传递给jr:list
dataSourceExpression
jr:list
<datasetRun subDataset="listDataSet" uuid="71276e30-7777-44ae-b6d9-2087a4c51ca3"> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("mainDataSet.listData")]]></dataSourceExpression> </datasetRun>
因此指向 mainDataSet.listData
mainDataSet.listData
在dataSourceExpression传递表(我们现在在listData)
<datasetRun subDataset="tableDataSet" uuid="5bca90cb-1473-4ff6-82fc-1da5ae4fb44c"> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("tableData")]]></dataSourceExpression> </datasetRun>