Jasper报告如何将数据从JRResultSetDataSource设置为Table组件 [英] Jasper Reports how to set data from JRResultSetDataSource into Table component

查看:85
本文介绍了Jasper报告如何将数据从JRResultSetDataSource设置为Table组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用1个表创建一个简单的Jasper报表,该表应使用JRResultSetDataSource对象由Java代码填充.但是,我无法将这些值传递给Table组件.

I am trying to create a simple Jasper report with 1 Table which should be populated by java code using JRResultSetDataSource object. However, I am not able to pass the values to the Table component.

我不是在寻找参数列表或JRBeanCollectionDataSource作为解决方案.

I am not looking for Parameter list or JRBeanCollectionDataSource as a solution.

我的报告jrxml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-03-27T17:04:35 -->
<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="tab1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8a63304f-05d6-485c-82e7-5591159f68fb">
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Empty Dataset1" uuid="be9c1aa9-031c-4232-a9f2-cdcb0a5bd17d">
        <field name="MONTH" class="java.lang.Integer"/>
        <field name="YEAR" class="java.lang.Integer"/>
        <field name="COUNT" class="java.lang.Integer"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="MONTH" class="java.lang.Integer"/>
    <field name="YEAR" class="java.lang.Integer"/>
    <field name="COUNT" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="360" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="200" height="200" uuid="684e26d7-94cf-49cc-874d-104e29444537">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                    <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
                </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" whenNoDataType="Blank">
                    <datasetRun subDataset="Empty Dataset1" uuid="0baea769-1082-4618-9eee-520f59145c07">
                        <connectionExpression><![CDATA[]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="60" uuid="ba2a25da-f872-4a99-8703-9e6e15fc988a">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="60" height="30" uuid="a4bbf819-044c-47ff-bff0-627859b51444"/>
                                <text><![CDATA[MONTH]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="60" height="30" uuid="91cd6b4f-f09b-4c08-8fe7-0645468bd1cc"/>
                                <textFieldExpression><![CDATA[$F{MONTH}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="70" uuid="80e850bb-b112-4b53-aa44-f9fca76710b4">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="70" height="30" uuid="86e7d6fb-ee79-4c11-8e3d-99b59832b978"/>
                                <text><![CDATA[YEAR]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="70" height="30" uuid="39a27fc4-79d6-4ff8-aae0-ff67ac43bd09"/>
                                <textFieldExpression><![CDATA[$F{YEAR}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="70" uuid="57949a99-6a7c-4911-b01c-1db16d3963bd">
                        <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
                        <jr:tableHeader style="Table_TH" height="30" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="0" width="70" height="30" uuid="cddf1b17-b113-4cc5-a95c-395f59d96181"/>
                                <text><![CDATA[COUNT]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/>
                        <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/>
                        <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="70" height="30" uuid="fefdadb9-d47e-45b5-9aa4-346c53ce0441"/>
                                <textFieldExpression><![CDATA[$F{COUNT}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
        <band height="50"/>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

我的Java代码段:

resultSet = statement.executeQuery(query);

if(resultSet != null && reportStream !=null)
{               
JRResultSetDataSource jrr = new JRResultSetDataSource(resultSet);
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream,new HashMap(),jrr);
}

到目前为止,如果我不使用表格"组件,则可以在报表上列出数据,而只需将字段"放在报表上即可. 但是我无法找到一种方法将数据推入Table组件内部,因为它具有单独的数据源和相应的Fields组件.

So far I am able to list the data on the report if I dont use the Table component, instead just put the Fields on the report. But I am not able to find a way to push the data inside the Table component, as it has a seperate datasource and a corresponding Fields component.

这种使用数据源填充表数据的方法是否可能? 有人可以给我一个例子,还是给我一个例子.

Is this way of populating table data using a datasource even possible? Can someone give me an example, or point me to an example.

谢谢,
维沙伦杜

Thanks,
Vishalendu

推荐答案

您需要执行以下操作:

在JRXML中

<subDataset name="Empty Dataset1" ...>
...
</subDataset>
<parameter name="TableDataSource" class="net.sf.jasperreports.engine.JRDataSource"/>
...
<jr:table ...>
    <datasetRun subDataset="Empty Dataset1" ...>
        <dataSourceExpression><![CDATA[$P{TableDataSource}]]></dataSourceExpression>
    </datasetRun>
...

在Java中

JRResultSetDataSource jrr = new JRResultSetDataSource(resultSet);
Map params = new HashMap();
params.put("TableDataSource", jrr);
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, params, new JREmptyDataSource());

这篇关于Jasper报告如何将数据从JRResultSetDataSource设置为Table组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆