1

I want to merge more than one sub-report into a master report using jasper report and spring boot in java, but not able to merge the sub-report into master report.while the below code works for me, if there is only one report.

report[0] = "/reports/subreport1.jrxml";
report[1] = "/reports/master1.jrxml";
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(productService.report());
for(int i =0 ;i<=1;i++)
{
    inputStream[i] = this.getClass().getResourceAsStream(report[i]);
    jasperReport[i] = JasperCompileManager.compileReport(inputStream[i]);
    jasperPrint[i] = JasperFillManager.fillReport(jasperReport[i], null, dataSource);
    HtmlExporter exporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint[i]));
    exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
    exporter.exportReport();
}

getting error as "java.io.StreamCorruptedException : invalid stream header: 3C3F786D"

Deepak kumar
  • 19
  • 1
  • 3

1 Answers1

0

I have got my solution for this, here initially i was not able to merge the reports because, i was getting my data from database but, i have write the code for getting the data from list. so here is the updated code.

First Autowire the dependency of DataSource as:

@Autowired
private DataSource dataSource; 

And my above modified Code is:

public void report(HttpServletResponse response) throws Exception {

        response.setContentType("text/html");
        String[] report = new String[2];
        InputStream[] inputStream = new InputStream[2];
        JasperReport[] jasperReport = new JasperReport[2];

        List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();

        report[0] = "/reports/master1.jrxml";
        report[1] = "/reports/subreport.jrxml";

        for(int i =0 ;i<=1;i++)
        {
            inputStream[i] = this.getClass().getResourceAsStream(report[i]);
            jasperReport[i] = JasperCompileManager.compileReport(inputStream[i]);
            JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport[i], null, dataSource.getConnection());
            jasperPrintList.add(jasperPrint1);
            HtmlExporter exporter = new HtmlExporter();
            exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
            exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
            exporter.exportReport();
        }

Major modification in my code is during passing of parameter in fillReport in place of "datasource" there is "datasource.getConnection()".

Deepak kumar
  • 19
  • 1
  • 3