Java 如何在jasperreport中添加子报表dinamyc

Java 如何在jasperreport中添加子报表dinamyc,java,jasper-reports,subreport,Java,Jasper Reports,Subreport,可以用两个子报表创建一个报表,并将所有文件放在我的数据库中吗?那么,我怎样才能在主报告中填写子报告?我需要在临时文件中保存子报告,并传递目录?或者有办法在jasperPrint中添加子报告,然后他找到子自动 这是我的代码: try (Connection conexao = dataSource.getConnection()){ FinanceiroTransacao transacao = financeiroTransacaoService.findGatewayIdTra

可以用两个子报表创建一个报表,并将所有文件放在我的数据库中吗?那么,我怎样才能在主报告中填写子报告?我需要在临时文件中保存子报告,并传递目录?或者有办法在jasperPrint中添加子报告,然后他找到子自动

这是我的代码:

try (Connection conexao = dataSource.getConnection()){
        FinanceiroTransacao transacao = financeiroTransacaoService.findGatewayIdTransacaoByBoletoAndCartorio(boletoId, cns);
        JasperReport jasperReport;
        jasperReport = JasperCompileManager.compileReport(JRXmlLoader.load(new ByteArrayInputStream(relatorioBase.getConteudo())));
        Map<String, Object> params = new HashMap<>();
        params.put("pedidoId", pedidoId);
        params.put("boletoId", boletoId);
        params.put("cartorio",cns);
        params.put("totalTransacao", transacao.getValorCredito());
        params.put("idTransferencia", transacao.getGatewayPagamentoTransacaoId());
        params.forEach((s, o) -> log.debug("paramentro {} , valor {}", s, o));

        subReports.forEach(subReport -> {
           //HERE I NEED TO add the sub reports 
        });
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, conexao);

        JRPdfExporter pdfExporter = new JRPdfExporter();
        pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        ByteArrayOutputStream pdfReportStream = new ByteArrayOutputStream();
        pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfReportStream));
        pdfExporter.exportReport();
        String retorno = Base64.getEncoder().encodeToString(pdfReportStream.toByteArray());
        pdfReportStream.close();
        return retorno;
    }catch (Exception e){
        e.printStackTrace();
        throw new RuntimeException("erro: "+ e.getMessage());
    }
try(Connection conexao=dataSource.getConnection()){
FinanceiroTransacao transacao=FinanceiroTransacao服务。通过Boleto和Cartorio(boletoId,cns)查找GatewayId transacao;
贾斯佩雷波特;
jasperReport=JasperCompileManager.compileReport(jrxmloader.load(新的ByteArrayInputStream(relatorioBase.getConteudo()));
Map params=新的HashMap();
参数put(“pedidoId”,pedidoId);
参数put(“boletoId”,boletoId);
参数put(“cartorio”,cns);
参数put(“totalTransacao”,transacao.getValorCredito());
参数put(“idTransferencia”,transacao.getGatewayPagamentoTransacaoId());
forEach((s,o)->log.debug(“paramentro{},valor{},s,o));
子报表.forEach(子报表->{
//在这里,我需要添加子报告
});
JasperPrint JasperPrint=JasperFillManager.fillReport(jaspereport,params,conexao);
JRPdfExporter PDFEEXPORTER=新的JRPdfExporter();
setExporterInput(新的SimpleExporterInput(jasperPrint));
ByteArrayOutputStream pdfReportStream=新建ByteArrayOutputStream();
setExporterOutput(新的SimpleOutputStreamExportRoutPut(pdfReportStream));
pdfExporter.exportReport();
字符串returno=Base64.getEncoder().encodeToString(pdfReportStream.toByteArray());
pdfReportStream.close();
返回号;
}捕获(例外e){
e、 printStackTrace();
抛出新的运行时异常(“erro:+e.getMessage());
}

首先要做的是在jaspert报表中创建一个参数:“SUBREPORT\u DIR”,它将作为子报表的位置,因此可以在报表的XML中执行此操作

</subreport>
   <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
   <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "NameSubrport.jasper"]]></subreportExpression>
</subreport>
(请记住
.getResource
,它用于在运行时获取项目中报告所在文件夹的正确路径)


这样对我来说很有用。我希望这有帮助

我的HD中没有任何文件,我所有的报告都在我的DB(Postgresql)中,因此我如何传递没有名称的子报告?可能类似的问题:
Resource r = ApplicationContextProvider.getApplicationContext().getResource("/resources/reports");
String reportPath = r.getFile().getAbsolutePath();
params.put("SUBREPORT_DIR", reportPath + File.separator);