【问题标题】:JasperReport PDF output in Java SwingJava Swing 中的 JasperReport PDF 输出
【发布时间】:2012-02-11 07:13:46
【问题描述】:

在我当前的项目中,我正在处理一些 Jasper 报告。我有一份报告,其中有 2 个子报告。我正在将其导出为 pdf 文件。如果我导出没有任何子报表的普通碧玉报表,则 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();
    }
}

谁能告诉我我错过了哪里或者我必须做什么才能使报告工作。

谢谢

【问题讨论】:

    标签: java pdf jasper-reports


    【解决方案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();
    

    谢谢大家

    【讨论】:

      【解决方案2】:

      您可以从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 文件中的 sn-p:

      <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>
      

      【讨论】:

      • 嗨,Alex,我可以从以下代码中获取输出。非常感谢您的帮助:请在我的回答中查看
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      相关资源
      最近更新 更多