How do I send a data source list to a sub report and call that sub report in my main report

Asked

Viewed 208 times

0

Good morning guys, I’m having a little problem. I’m trying to pass a lista as data source for my sub report. I already have the main report which is also filled in by a list. My question is this, how do I pass one lista as data source for my sub report and then call that sub report within my main report?

This is my method that generates the report and exports in pdf to a directory:

public boolean gerarRelatorio(List list, int numeroRelatorio, String nomePrestador, String dataSistema) {
        JasperReport report = null;
        InputStream image = this.getClass().getResourceAsStream("/br/com/xml/relatorio/LOGO.png");
        filtro.put("Image", image);
        try {
            InputStream inputStreamReal = getClass().getResourceAsStream("/br/com/xml/relatorio/Relatorio2.jrxml");
            report = JasperCompileManager.compileReport(inputStreamReal);
        } catch (JRException ex) {
            Logger.getLogger(frmPegaXml.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Erro ao pegar arquivos!", "ERRO!", JOptionPane.ERROR_MESSAGE);
        }
        try {

            JasperPrint print = JasperFillManager.fillReport(report, filtro, new JRBeanCollectionDataSource(list));
            JasperExportManager.exportReportToPdfFile(print,
                    "C:/Demonstrativos/" + dataSistema + "/" + nomePrestador + "_" + dataSistema + "_" + numeroRelatorio + ".pdf");

            /*Variaveis necessarias para salvar o Arquivo no Banco de dados*/
            caminho1 = "C:/Demonstrativos/" + dataSistema + "/";
            ext = ".zip";
            nomeArquivo1 = nomePrestador + "_" + dataSistema + "_" + numeroRelatorio;
            caminhoCompleto = caminho1 + nomeArquivo1 + ".pdf";

            codAP = Long.parseLong(codArquivoPrestador);
            relatoriosGerados = numeroRelatorio + 1;
            return true;
            /*Variaveis necessarias para salvar o Arquivo  no Banco de dados*/
        } catch (JRException ex) {
            Logger.getLogger(frmPegaXml.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Erro ao Gerar relatório, verifique se nenhum arquivo está aberto ou se o nome está correto!\n" + ex, "ERRO!", JOptionPane.ERROR_MESSAGE);
            return false;
        }

    }

I’m using Jaspersoft Studio, thank you so much.

1 answer

2


Hi, it’s not that complicated. For the list you create a parameter in code and create one to send the directory of your sub-report. So, at the time you put your sub-report in the main report and configure it in the "Expression" field (field that says where it is/which sub-report), you pass the parameter to tell where the sub-report is, in the "Data Source Expression" field You send your list as follows: "new Jrbeancollectiondatasource(parametroDaLista)". However, you will have to set the parameter in your main report, both for the list and for the directory, remembering that the parameter has to be identical to the one you created in code! I hope I helped, doubt just call! Hug!

  • The answer of the colleague above is correct, it is the best way to do it, just complement saying that it does not necessarily need to be a parameter, you can also pass a field of your main data source, as long as this is a list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.