从子报表生成PDF

时间:2013-12-18 04:38:40

标签: java jasper-reports

我在jasper中有一个这样的主要报告

<?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="Allergy" language="groovy" pageWidth="595" pageHeight="842" columnWidth="503" leftMargin="72" rightMargin="20" topMargin="20" bottomMargin="20" isTitleNewPage="true" isSummaryNewPage="true" isSummaryWithPageHeaderAndFooter="true" isFloatColumnFooter="true" isIgnorePagination="true" > <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="723"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["D:\\FTP\\JRXML\\PatientConsultantReport\\"]]></defaultValueExpression>
    </parameter>
    <queryString language="SQL">
        <![CDATA[select *  from VIEW_tbPatientAllergy]]>
    </queryString>
    <field name="PatientAllergyIDP" class="java.lang.Integer"/>
    <field name="CitizenIDF" class="java.lang.Integer"/>
    <field name="AllergyNameIDF" class="java.lang.Integer"/>
    <field name="AllergyReactionIDF" class="java.lang.Integer"/>
    <field name="Severity" class="java.lang.Integer"/>
    <field name="OnsetDate" class="java.lang.String"/>
    <field name="Status" class="java.lang.Integer"/>
    <field name="Remarks" class="java.lang.String"/>
    <field name="IsDirect" class="java.lang.Boolean"/>
    <field name="EncounterIDF" class="java.lang.Integer"/>
    <field name="Created" class="java.sql.Timestamp"/>
    <field name="CreatedByIDF" class="java.lang.Integer"/>
    <field name="LastModified" class="java.sql.Timestamp"/>
    <field name="LastModifiedByIDF" class="java.lang.Integer"/>
    <field name="AllergyName" class="java.lang.String"/>
    <field name="AllergyReaction" class="java.lang.String"/>
    <field name="AllergyType" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="40" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="5" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="29" splitType="Stretch"/>
    </columnHeader>
    <detail>


        <band height="50">

            <subreport>

                <reportElement  x="-66" y="0" width="582" height="39"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["D:\\FTP\\JRXML\\PatientConsultantReport\\OrderSet.jasper"]]></subreportExpression>

            </subreport>

        </band>
    </detail>
    <columnFooter>
        <band height="36" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="27" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

像这样的子报道

    <?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="OrderSet" pageWidth="595" pageHeight="842" columnWidth="503" leftMargin="72" rightMargin="20" topMargin="20" bottomMargin="20" >
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[select * from view_tbPatientOrderSet]]>
    </queryString>
    <field name="PatientOrderSetIDP" class="java.lang.Integer"/>
    <field name="EncounterIDF" class="java.lang.Integer"/>
    <field name="ServiceMapIDF" class="java.lang.Integer"/>
    <field name="ServiceIDF" class="java.lang.Integer"/>
    <field name="OrderDate" class="java.lang.String"/>
    <field name="Quantity" class="java.math.BigDecimal"/>
    <field name="ReferToIDF" class="java.lang.Integer"/>
    <field name="IsExternal" class="java.lang.Boolean"/>
    <field name="ExternalCareProviderIDF" class="java.lang.Integer"/>
    <field name="ExternalCareProvider" class="java.lang.String"/>
    <field name="OrderStatus" class="java.lang.Boolean"/>
    <field name="EncounterServiceIDF" class="java.lang.Integer"/>
    <field name="Remarks" class="java.lang.String"/>
    <field name="Created" class="java.sql.Timestamp"/>
    <field name="CreatedByIDF" class="java.lang.Integer"/>
    <field name="LastModified" class="java.sql.Timestamp"/>
    <field name="LastModifiedByIDF" class="java.lang.Integer"/>
    <field name="ServiceName" class="java.lang.String"/>
    <field name="FirstName" class="java.lang.String"/>
    <field name="MiddleName" class="java.lang.String"/>
    <field name="FamilyName" class="java.lang.String"/>
    <field name="PatientProfileIDF" class="java.lang.Integer"/>
    <field name="PriceListIDP" class="java.lang.Integer"/>
    <field name="Rate" class="java.math.BigDecimal"/>
    <field name="CitizenIDF" class="java.lang.Integer"/>
    <field name="EnrolmentID" class="java.lang.String"/>
    <field name="CitizenName" class="java.lang.String"/>
    <field name="ServiceCategory" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement  x="4" y="0" width="65" height="20"/>
                <textElement verticalAlignment="Middle">
                    <font size="15" isBold="true"/>
                </textElement>
                <text><![CDATA[Order Set]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="30" splitType="Stretch">
            <line>
                <reportElement  x="3" y="27" width="457" height="1"/>
            </line>
            <staticText>
                <reportElement  x="3" y="5" width="52" height="20"/>
                <textElement/>
                <text><![CDATA[Order Date]]></text>
            </staticText>
            <staticText>
                <reportElement  x="73" y="5" width="68" height="20"/>
                <textElement/>
                <text><![CDATA[Service Name]]></text>
            </staticText>
            <staticText>
                <reportElement  x="157" y="5" width="41" height="20"/>
                <textElement/>
                <text><![CDATA[Quantity]]></text>
            </staticText>
            <staticText>
                <reportElement  x="231" y="5" width="50" height="20"/>
                <textElement/>
                <text><![CDATA[IsExternal]]></text>
            </staticText>
            <staticText>
                <reportElement  x="301" y="5" width="85" height="20"/>
                <textElement/>
                <text><![CDATA[Care Professional]]></text>
            </staticText>
            <staticText>
                <reportElement  x="415" y="5" width="44" height="20"/>
                <textElement/>
                <text><![CDATA[Remarks]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="25" splitType="Stretch">
            <textField>
                <reportElement  x="3" y="3" width="66" height="18"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{OrderDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement  x="73" y="3" width="84" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ServiceName}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement  x="167" y="3" width="58" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Quantity}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement  x="231" y="3" width="67" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{IsExternal}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement  x="301" y="3" width="115" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[($F{FirstName}!=null?$F{FirstName}:"")+" "+($F{MiddleName}!=null?$F{MiddleName}:"")+" "+($F{FamilyName}!=null?$F{FamilyName}:"")]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement  x="415" y="3" width="60" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{Remarks}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="23" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="18" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

我想从 Java 代码

生成pdf
String jrxmlFileLocation = "D:\\OrderSet.jrxml";
String jasperFileLocation = "D:\\OrderSet.jasper";
String outputPDFFile = "D:\\VisitsSummaryReport.pdf";
JasperCompileManager.compileReportToFile(jrxmlFileLocation, jasperFileLocation);
JasperCompileManager.compileReportToFile("D:\\Final_PatinetConsultation.jrxml", "D:\\Final_PatinetConsultation.jasper");

它已成功编译以及如何从此报告生成pdf文件,请在此代码中帮助我。

我看到这个链接但不明白 enter link description here

我使用像这样的结果集

String condition = "data = "+data;
PreparedStatement psOrderSet = conn.prepareCall("{call Sp_tbPatientOrderSet_GetAll(?)}",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
psOrderSet.setString(1,condition);
ResultSet rsOrderSet = psOrderSet.executeQuery();
rsOrderSet.last();
int lengthOrderSet=rsOrderSet.getRow();
rsOrderSet.beforeFirst();

如何将ResultSet传递给子报表以填充数据。

1 个答案:

答案 0 :(得分:4)

使用JasperReport类型添加到主报告参数中的参数:

<parameter name="OrderSet" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false"/>

在主报表中 - 在subreportExpression中指向OrderSet参数

<subreport>

    <reportElement  x="-66" y="0" width="582" height="39"/>
    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    <subreportExpression><![CDATA[$P{OrderSet}]]></subreportExpression>

</subreport>

然后汇编和pdf导出:

String jrxmlFileLocation = "D:\\OrderSet.jrxml";
String outputPDFFile = "D:\\VisitsSummaryReport.pdf";

// Compile your main report
JasperReport jasperReport = JasperCompileManager.compileReport(
      "D:\\Final_PatinetConsultation.jrxml");
// Compile your subReport report
JasperReport jasperSubReport = JasperCompileManager.compileReport(
      jrxmlFileLocation);
//Initialize parameters
Map parameters = new HashMap();
//Add subreport as parameter for main report
parameters.put("OrderSet", jasperSubReport); 
// Convert to JasperPrint - you may fill parameters here also if any
// and provide java.sql.Connection
JasperPrint document = JasperFillManager.fillReport(jasperReport, parameters, connection);
// Export to pdf 
JasperExportManager.exportReportToPdfFile(document, outputPdfFile);

这应该适合你。