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

0

Good morning, I have a small problem. I am trying to pass lista as data source to my sub report. I already have the main report which is also populated by a list. My question is, how do I pass a lista as data source to 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.

    
asked by anonymous 20.05.2015 / 14:16

1 answer

2

Hello, it's not that complicated. For the list you create a parameter in code and create one to send the directory of your subreport. So, at the time you for your subreport in the main report, and you set it up in the "Expression" field (the field that says where the subreport is / is), you pass the parameter to say where the subreport is , in the field "Data Source Expression" you send your list as follows: "new JRBeanCollectionDataSource (ListDocument)". However, you will have to set the parameter in your main report, both for list and directory, remembering that the parameter has to be identical to the one you created in code! I hope I have helped, doubt just call! Hugs!

    
24.06.2015 / 22:33