参数化时未生成Jasper报告

时间:2013-12-02 17:43:46

标签: java jasper-reports

我正在尝试通过传递参数来生成Jasper报告。程序运行和组合没有错误但没有创建文件。在没有参数的报告中使用它也可以正常工作。

计划:

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_reports","root", "root");
System.out.println("Loading Report Designs");
InputStream input = new FileInputStream(new File("parameterized_report.jrxml"));
JasperDesign jasperDesign = JRXmlLoader.load(input);

System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

System.out.println("Creating JasperPrint Object");
Map parameters = new HashMap();
parameters.put("yearParam","2010");

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);

//Exporting the report
OutputStream output = new FileOutputStream(new File("Final.pdf"));

JasperExportManager.exportReportToPdfStream(jasperPrint, output);

System.out.println("Report Generation Complete");
conn.close();

贾斯帕报告:

<?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="parameterized_report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="37387775-86c0-41fc-ba9c-de01d6b29053">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="yearParam" class="java.lang.String">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[$F{year}]]></defaultValueExpression>
    </parameter>
    <queryString language="SQL">
        <![CDATA[select * from yearly_spending where year=$P{yearParam}]]>
    </queryString>
    <field name="company" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="year" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="spending" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="92" y="41" width="100" height="20" uuid="aad73e02-6885-4f31-a63a-7726e85e2bdb"/>
                <text><![CDATA[COMPANY ]]></text>
            </staticText>
            <staticText>
                <reportElement x="203" y="41" width="100" height="20" uuid="3d81629b-faa4-4e0e-8db2-7f21732e0a94"/>
                <text><![CDATA[YEAR]]></text>
            </staticText>
            <staticText>
                <reportElement x="319" y="41" width="100" height="20" uuid="26d9ea75-79f1-4da0-a7aa-e37c18422de3"/>
                <text><![CDATA[SPENDING]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement x="92" y="0" width="100" height="20" uuid="dd81332b-d8d5-43ce-9d79-d4ce0b26df0d"/>
                <textFieldExpression><![CDATA[$F{company}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="203" y="0" width="100" height="20" uuid="68c786ad-49cb-44ea-ad20-463c498fe54d"/>
                <textFieldExpression><![CDATA[$F{year}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="319" y="0" width="100" height="20" uuid="5d3469e8-009d-43b0-8ecf-f89461706b7a"/>
                <textFieldExpression><![CDATA[$F{company}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="190" splitType="Stretch">
            <barChart>
                <chart>
                    <reportElement x="82" y="35" width="346" height="147" uuid="a481305f-8ab2-4944-85a8-9ec810d6eda6"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA[$F{company}]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{year}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{spending}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <barPlot>
                    <plot/>
                    <itemLabel/>
                    <categoryAxisFormat>
                        <axisFormat/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                </barPlot>
            </barChart>
        </band>
    </summary>
</jasperReport>

1 个答案:

答案 0 :(得分:0)

第一件事年份列在查询读取字段中是整数,因此您应该将参数更改为整数并将任何年份(例如2013)作为默认值

您可以将查询更改为: -

     select * 
     from yearly_spending 
     where (year=$P{yearParam} or $P{yearParam} is null)

此查询将为您提供所有结果,而无需选择任何参数。