Jasper报告中的多个文件

时间:2013-04-17 12:40:25

标签: java pdf jasper-reports

我正在使用此代码在Jasper中调用我的jrxml文件:

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;


public class Jasperreports {

    public static void main(String[] args) {
        Connection conn = null;
        String iphost = "myhostip";
        String dbsid = "DISAD";
        String username = "MYUSERNAME";
        String password = "mypassword";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection("jdbc:oracle:thin:@" + iphost + ":1521:" + dbsid, username, password);
            int count = 0;
            JasperReport jasperReport = JasperCompileManager.compileReport("Advice2.jrxml");
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), conn);
            JasperExportManager.exportReportToPdfFile(jasperPrint, "sample" + count + ".pdf");
            count++;
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

我的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="Advice" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="253171d0-c1c3-465d-a40b-dd200cc3fca8">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="SQL">
        <![CDATA[select h.GROUP_DESC, b.foracid ACCOUNT_NUMBER , a.tran_id, b.ACCT_NAME ACCOUNT_NAME,j.NAME,COALESCE(i.EMAIL, n'No email Address')EMAIL, b.schm_code,e.SCHM_DESC, b.SOL_ID, d.SOL_DESC,
COALESCE(c.ADDRESS_LINE1, n'No Address')ADDRESS_LINE1,c.STATE, g.localetext, f.VALUE, f.categoryid, a.TRAN_DATE TRANSACTION_DATE,a.VALUE_DATE,a.TRAN_PARTICULAR TRAN_PARTICULARS,
a.REF_NUM CHQ_REFERENCE, A.TRAN_CRNCY_CODE CURRENCY, a.RATE,
max(case when a.PART_TRAN_TYPE = 'D' then a.TRAN_AMT else 0.00 end) DEBIT_AMOUNT,
max(case when a.PART_TRAN_TYPE = 'C' then a.TRAN_AMT else 0.00 end ) CREDIT_AMOUNT
from TBAADM.DAILY_TRAN_DETAIL_TABLE a, TBAADM.GENERAL_ACCT_MAST_TABLE b, crmuser.address c, tbaadm.service_outlet_table d,
TBAADM.GEN_SCHM_PARM_TABLE e, crmuser.categories f, crmuser.category_lang g, TBAADM.SOL_GROUP_CONTROL_TABLE h, CRMUSER.PHONEEMAIL i,
crmuser.accounts j
where a.acid = b.acid(+)
and a.CUST_ID = c.orgkey(+) and C.ADDRESSCATEGORY = 'Mailing'
and B.SOL_ID = D.SOL_ID(+)
and b.schm_code = E.SCHM_CODE(+)
and c.state = f.value(+)
and F.CATEGORYID = G.CATEGORYID
and b.foracid is not null
and b.acct_name is not null
and B.BANK_ID = H.SOL_GROUP_ID(+)
and B.CUST_ID = I.ORGKEY(+)
and B.CUST_ID = j.orgkey(+)
and rownum <=10
group by b.foracid, a.tran_date, a.value_date, a.tran_particular, a.ref_num, A.TRAN_CRNCY_CODE,
b.acct_name, b.schm_code, b.sol_id,COALESCE(c.ADDRESS_LINE1, n'No Address'),c.STATE, d.sol_desc, e.SCHM_DESC, f.value, f.categoryid,
g.localetext, a.tran_id, H.GROUP_DESC, COALESCE(I.EMAIL, n'No email Address'), a.RATE, j.NAME
order by B.FORACID asc]]>
    </queryString>
    <field name="GROUP_DESC" class="java.lang.String"/>
    <field name="ACCOUNT_NUMBER" class="java.lang.String"/>
    <field name="TRAN_ID" class="java.lang.String"/>
    <field name="ACCOUNT_NAME" class="java.lang.String"/>
    <field name="NAME" class="java.lang.String"/>
    <field name="EMAIL" class="java.lang.String"/>
    <field name="SCHM_CODE" class="java.lang.String"/>
    <field name="SCHM_DESC" class="java.lang.String"/>
    <field name="SOL_ID" class="java.lang.String"/>
    <field name="SOL_DESC" class="java.lang.String"/>
    <field name="ADDRESS_LINE1" class="java.lang.String"/>
    <field name="STATE" class="java.lang.String"/>
    <field name="LOCALETEXT" class="java.lang.String"/>
    <field name="VALUE" class="java.lang.String"/>
    <field name="CATEGORYID" class="java.math.BigDecimal"/>
    <field name="TRANSACTION_DATE" class="java.sql.Timestamp"/>
    <field name="VALUE_DATE" class="java.sql.Timestamp"/>
    <field name="TRAN_PARTICULARS" class="java.lang.String"/>
    <field name="CHQ_REFERENCE" class="java.lang.String"/>
    <field name="CURRENCY" class="java.lang.String"/>
    <field name="RATE" class="java.math.BigDecimal"/>
    <field name="DEBIT_AMOUNT" class="java.math.BigDecimal"/>
    <field name="CREDIT_AMOUNT" class="java.math.BigDecimal"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="28" splitType="Stretch">
            <staticText>
                <reportElement uuid="cf153a5b-1afb-441b-b840-f2a23274363d" x="136" y="8" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[Transaction Advices]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="7" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="301" splitType="Stretch">
            <textField>
                <reportElement uuid="35a120b1-e836-4415-9663-92ab52b751f5" x="0" y="0" width="212" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="4efa323d-f77b-45d1-943a-042dfd78abae" x="0" y="20" width="212" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ADDRESS_LINE1}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="d5a0d2b1-998f-4c2d-aa26-f83ca078f5f3" x="0" y="40" width="212" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{STATE}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="cca1db34-ad68-4d09-a28e-5af6b77bdd24" x="0" y="60" width="212" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{EMAIL}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="d92049c4-3cfe-4be8-beb4-240615dc0e8b" x="136" y="127" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[ACCOUNT_NAME]]></text>
            </staticText>
            <textField>
                <reportElement uuid="0a513b59-c441-405e-96b9-85d2404a82fc" x="256" y="127" width="249" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ACCOUNT_NAME}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="7128a15d-f84a-46cd-b2f0-202db8a556bf" x="136" y="147" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[SOL_ID]]></text>
            </staticText>
            <textField>
                <reportElement uuid="1e47e77d-3b0f-4018-ac81-2f8673c0d00e" x="256" y="147" width="249" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{SOL_ID}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="de11f45c-18ee-413e-8e8d-74da809e9e63" x="136" y="167" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[ACCOUNT_NUMBER]]></text>
            </staticText>
            <textField>
                <reportElement uuid="056f9770-a3a7-4a5a-8582-23a88cd19251" x="256" y="167" width="249" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ACCOUNT_NUMBER}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="2ddf4f57-0132-4864-b19c-773e69eff4a3" x="136" y="187" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[DEBIT_AMOUNT]]></text>
            </staticText>
            <textField>
                <reportElement uuid="67658e95-18e3-4877-b717-a0202c36d555" x="256" y="187" width="249" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{DEBIT_AMOUNT}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="6e7974d6-a2e6-4d30-8823-212976e132e6" x="136" y="207" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[CREDIT_AMOUNT]]></text>
            </staticText>
            <textField>
                <reportElement uuid="e34eb773-f0a6-4ee5-8f50-416cd1b95a16" x="256" y="207" width="249" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{CREDIT_AMOUNT}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="63292bb9-3ab7-44d1-9fd1-da9f53fcd9a3" x="136" y="228" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[TRAN_PARTICULARS]]></text>
            </staticText>
            <textField>
                <reportElement uuid="1a02a640-77b2-4120-b14b-b67c8dcfd2b7" x="256" y="227" width="249" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{TRAN_PARTICULARS}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

我收到Just One文件.pdf,包含所有记录。如何为每条记录生成一个文件,以便为每个报告生成不同的文件?

1 个答案:

答案 0 :(得分:1)

这只是JasperReports的标准行为。每个SQL查询返回的记录将重复一次详细信息带。

因此,如果您希望每个记录位于不同的PDF文件中,您有两个选择:

  1. 更改您的SQL查询,使其只返回一条记录,使用参数,以便您可以控制它。
  2. 在java中获取SQL查询的数据,并将该信息作为参数或使用java对象数据集传递给JasperReport。 This example已经很老了,但展示了如何做到这一点。
  3. 当然,您需要遍历记录,为每种方法生成每个记录一次调用。

    希望有所帮助