从Java代码运行报表时,为什么子数据集为空白? [英] Why is my subdataset blank when I run report from Java code?

查看:53
本文介绍了从Java代码运行报表时,为什么子数据集为空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Jaspersoft Studio创建了一个表格设计.当我在Java中使用此jrxml时,未显示任何表

I created a table design using Jaspersoft Studio. When I am using this jrxml in Java no table is displayed

我将Jaspersoft Studio用作Eclipse中的插件来生成jrxml.预览表格时,该表格会显示在JSS中,但是当我使用Java代码将其导出到pdf/xls/docx时,不会加载该表格.没有任何数据的空文件.

I am using Jaspersoft Studio as a plugin in Eclipse to generate jrxml. While previewing the table gets displayed in JSS, but when I am using Java code to export it into pdf/xls/docx its not getting loaded. Empty file without any data.

这是我的JRXML文件.

This is my JRXML file.

<?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="Blank_A4_Landscape" pageWidth="802" pageHeight="555" orientation="Landscape" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="f391983e-5ad3-4a64-b310-fc7f3426e3f0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>
    <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>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#E3F1FF"/>
        </conditionalStyle>
    </style>
    <subDataset name="table" uuid="9fa2cf87-1e9b-4673-a13c-55f4b337d4db">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>
        <queryString language="SQL">
            <![CDATA[select * from CODE]]>
        </queryString>
        <field name="NAME" class="java.lang.String"/>
        <field name="MOBILE" class="java.lang.String"/>
        <field name="COMPANY_NAME" class="java.lang.String"/>
        <field name="SITE_ID" class="java.lang.String"/>
        <field name="SITE_NAME" class="java.lang.String"/>
        <field name="IN_TIME" class="java.lang.String"/>
        <field name="OUT_TIME" class="java.lang.String"/>
        <field name="CREATE_ON" class="java.lang.String"/>
    </subDataset>
    <queryString language="SQL">
        <![CDATA[select * from CODE]]>
    </queryString>
    <field name="NAME" class="java.lang.String"/>
    <field name="MOBILE" class="java.lang.String"/>
    <field name="COMPANY_NAME" class="java.lang.String"/>
    <field name="SITE_ID" class="java.lang.String"/>
    <field name="SITE_NAME" class="java.lang.String"/>
    <field name="IN_TIME" class="java.lang.String"/>
    <field name="OUT_TIME" class="java.lang.String"/>
    <field name="CREATE_ON" class="java.lang.String"/>
    <title>
        <band height="50" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="20" width="100" height="30" forecolor="#007BFF" uuid="46c45769-ac34-41af-bdb4-c45729f333b9"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Call Logs]]></text>
            </staticText>
            <textField pattern="MMM d, yyyy h:mm:ss a">
                <reportElement x="450" y="20" width="330" height="30" forecolor="#007BFF" uuid="7a3c6c9d-bf5b-487d-8e13-720ecda75b61"/>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Report Generated on:  "+new java.util.Date()]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="243" splitType="Stretch">
            <printWhenExpression><![CDATA[$V{REPORT_COUNT} != 0]]></printWhenExpression>
            <componentElement>
                <reportElement x="41" y="24" width="720" height="200" uuid="24f1ba3d-2de9-4a32-9f9f-75d592f91359">
                    <printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
                </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="table" uuid="f76f4c9b-3345-4d0e-8cad-dd8a75913665">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="104" uuid="229daaed-80a6-42c5-9ade-2f0c75d1963e">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="cfa9503a-bbfe-41fb-96a6-46c633a691fc"/>
                                <text><![CDATA[NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="d0395260-510c-44dd-88c0-3c5ab97a6f65"/>
                                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="94785e2c-8fa8-4402-8506-4f234bf064bd">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="03b5ef55-113a-485c-9d09-519dcc9d6152"/>
                                <text><![CDATA[MOBILE]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="dba6ea8d-23d1-4af3-bf64-475a5266b85f"/>
                                <textFieldExpression><![CDATA[$F{MOBILE}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="8151f7e4-e41b-430b-8730-b5499a6d5d1f">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="2321d1fa-4e3f-4d3f-8727-bae188bafd7b"/>
                                <text><![CDATA[COMPANY_NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="d9d2d45e-98cc-4f52-8b3d-4d2829d5df34"/>
                                <textFieldExpression><![CDATA[$F{COMPANY_NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="1c6cc322-6497-437a-8945-c1c3f08287b0">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="2720a693-f022-4498-93e3-4d0aa7b37982"/>
                                <text><![CDATA[SITE_NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="c34d16ae-244f-4b73-8e54-71655a5fc231"/>
                                <textFieldExpression><![CDATA[$F{SITE_NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="105" uuid="7b35eec3-38c2-4b76-aa58-d7cb1dc0925c">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="105" height="30" uuid="29e866cb-fecc-4854-abdb-4e131883b68e"/>
                                <text><![CDATA[IN_TIME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="105" height="30" uuid="d3b432d0-95e4-4d4a-b6a8-4984f3c95164"/>
                                <textFieldExpression><![CDATA[$F{IN_TIME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="104" uuid="ba443c16-d86f-483c-9e79-035cfc42ce40">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="a6d98a0b-d280-475a-8b7e-7e88778c5dcd"/>
                                <text><![CDATA[OUT_TIME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="9fa9cd8c-da08-4871-9cb1-6655d0a14e34"/>
                                <textFieldExpression><![CDATA[$F{OUT_TIME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="104" uuid="3aac9567-fd44-4954-8542-60806e43e960">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="81d7af3f-bfd5-4bda-8c4c-645d44c718b9"/>
                                <text><![CDATA[CREATE_ON]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="502689db-da95-4dba-bdd9-fc066be71100"/>
                                <textFieldExpression><![CDATA[$F{CREATE_ON}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

这是我的Java代码:

And this is my Java code:

public class PDFReportGenerator {
    public static String ACTUAL_REPORT_PATH = "E:\\path\\Blank_A4_Landscape.jrxml";
    public static String PDF_PATH = "E:\\path\\Blank_A4_Landscape.pdf";

    public static void main(String[] args) throws JRException, ClassNotFoundException, SQLException{
        generateReport();
    }

    static void generateReport() {
        Connection conn = null;
        ResultSet rs = null;
        JasperReport jasperReport = null;
        JasperPrint print = null;
        String query ="SELECT * FROM CODE";
        try {   
            if(conn==null) {
                try {
                    conn= OracleConnection.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }   
        CallableStatement cstmt = conn.prepareCall(query);
        rs=cstmt.executeQuery();
        JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);      
        jasperReport = JasperCompileManager.compileReport(ACTUAL_REPORT_PATH);
        Map <String, Object> parameters = new HashMap <String, Object>();
        print = JasperFillManager.fillReport(jasperReport, parameters, resultSetDataSource);
        JasperExportManager.exportReportToPdfFile(print, PDF_PATH);
        } catch(Exception e) {
            System.out.println(e);}
    }
}

推荐答案

由于您的 subDataset 执行了查询,并且您正在向其传递数据库连接.

Since your subDataset executes a query and you are passing a database connection to it.

<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>

在Java中,您需要将此连接传递给 fillReport ,而不是您的 JRResultSetDataSource

In java you need to pass this connection to fillReport instead of your JRResultSetDataSource

print = JasperFillManager.fillReport(jasperReport,parameters, conn);

JRResultSetDataSource 保留为主数据源的另一种解决方案是为子报表创建另一个数据源,并将其作为参数传递,从而相应地修改jrxml中的 datasetRun 表达式,因此使用此数据源代替报表连接.

Another solution to keep the JRResultSetDataSource as main datasource would be to create another datasource for the subreport and pass this as a parameter modifying the datasetRun expression in jrxml accordingly, hence using this datasource instead of report connection.

为澄清起见,它在IDE中有效,因为在IDE中,您需要在运行报告之前选择连接,<属性名称="com.jaspersoft.studio.data.defaultdataadapter" value ="Oracle"/> ,但是当直接从Java运行时,这些设置无效/无法加载.

To clarify, it works in IDE because in IDE you are selecting the connection before you run the report, <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>, but these settings are not valid/loaded when running directly from java.

这篇关于从Java代码运行报表时,为什么子数据集为空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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