Java Swing中的JasperReport PDF输出

时间:2012-01-16 10:08:51

标签: java pdf jasper-reports

在我目前的项目中,我正在研究一些Jasper报告。我有一份报告,里面有2个子报告。我将其导出为pdf文件。如果我导出一个没有任何子报表的普通jasper报表,那么pdf文件工作正常,当我有子报表时,pdf文件为空。以下是我的代码:

static String reportPath = "D:/Netbeans Projects/Abc/mail_reports/";
public static void getReport() {
    try {
        String reportName = reportPath + "AuctionSale/AuctionSeller/AuctionSeller.jasper";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sale_date", "2012-01-10");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportName, params, DB.getConn());
        OutputStream output = new FileOutputStream(new File("C:/JasperReport.pdf")); 
        JasperExportManager.exportReportToPdfStream(jasperPrint, output); 
    } catch (Exception e) {
        e.printStackTrace();
    }
}

有人可以告诉我我在哪里错过了或者我有什么方法可以让报告有效。

由于

2 个答案:

答案 0 :(得分:1)

我可以从以下代码中获取打印件:

String s = Dashboard.mail_seller.getSelectedItem().toString();
            String selected[] = s.split(" -- ");
            String seller_id = selected[0].trim();
            String filename = sale_date + "_" + selected[1].replaceAll(" ", "_").trim() + ".pdf";
            String reportName = reportPath + "AuctionSale/AuctionSellerMail/AuctionSeller.jasper";
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("sale_date", sale_date);
            params.put("seller_id", seller_id);
            JasperPrint jasperPrint = JasperFillManager.fillReport(reportName, params, DB.getConn());
            OutputStream output = new FileOutputStream(new File("D:/Netbeans Projects/JDSons/mail_reports/"+filename+"")); 
            JasperExportManager.exportReportToPdfStream(jasperPrint, output); 
            output.flush();
            output.close();

谢谢你们

答案 1 :(得分:0)

this sample %jasperreports%\demo\samples\subreport 文件夹(来自JasperReports分发包)中的完整示例中可以看到,您可以将已编译的子报表作为参数传递。

示例java代码:

JasperReport subreport = (JasperReport) JRLoader.loadObjectFromFile("build/reports/ProductReport.jasper");

Map parameters = new HashMap();
parameters.put("ProductsSubreport", subreport);

JasperFillManager.fillReportToFile("build/reports/MasterReport.jasper", parameters, getDemoHsqldbConnection());

MasterReport jrxml文件的片段:

<parameter name="ProductsSubreport" class="net.sf.jasperreports.engine.JasperReport"/>
...
<detail>
    ...
        <subreport>
            <reportElement isPrintRepeatedValues="false" x="5" y="25" width="325" height="20" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
            <subreportParameter name="City">
                    <subreportParameterExpression><![CDATA[$F{City}]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <returnValue subreportVariable="PriceSum" toVariable="ProductTotalPrice" calculation="Sum"/>
            <subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{ProductsSubreport}]]></subreportExpression>
        </subreport>
        <subreport>
            <reportElement positionType="Float" x="335" y="25" width="175" height="20" isRemoveLineWhenBlank="true" backcolor="#99ccff"/>
            <subreportParameter name="City">
                    <subreportParameterExpression><![CDATA[$F{City}]]></subreportParameterExpression>
            </subreportParameter>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
            <returnValue subreportVariable="REPORT_COUNT" toVariable="CityAddressCount"/>
            <subreportExpression class="java.lang.String"><![CDATA["AddressReport.jasper"]]></subreportExpression>
        </subreport>
</detail>