JasperReports multi-page report with different con

2019-03-28 05:33发布

I'm evaluating JasperReport and iReport, a requirement is the possibility to produce a multiple page report in which every page contains a different report.

Example:
Page 1 contains an actual invoice for a customer
Page 2 contains the invoices list for the customer
Page 3 contains a graph of amount of invoices by year
Page 4 contains only fixed text (say operator instructions ...)

Is it possible to create such a unique report instead of creating four standalone report and then merge the pdfs.

Thank a lot.

Francesco

4条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-28 06:05

Yes you can by creating a dataSource and Parameters map for each sub report in your main report,

Data source contains the list that will be displayed as a table in the report

Parameters map contains keys and values of textfields in the report

The good news is that you can include all parameters of all your pages in each Parameters map then in report processing each page will extract its parameters and forgets others :)

Example :

List<Map<String, Object>> ParamList = new ArrayList<Map<String, Object>>();
List<JRDataSource> SourceList = new ArrayList<JRDataSource>();

Map<String, Object> params = new HashMap<String, Object>();
params.put("Page1_param1", "value1_1");
params.put("Page1_param2", "value1_2");
params.put("Page1_param3", "value1_3");
..
params.put("Page2_param1", "value2_1");
params.put("Page2_param2", "value2_2");
params.put("Page2_param3", "value2_3");
..
params.put("Page3_param1", "value3_1");
params.put("Page3_param2", "value3_2");
params.put("Page3_param3", "value3_3");
..
..
List listResult_1 = //select table sql for example
List listResult_2 = //select table sql for example
List listResult_3 = //select table sql for example

JRDataSource dataSource_1 = new ListOfArrayDataSource(
listResult_1, new String[] {"LastName", "FirstName", "address"});

JRDataSource dataSource_2 = new ListOfArrayDataSource(
listResult_2, new String[] {"LastName", "FirstName", "address"});

JRDataSource dataSource_3 = new ListOfArrayDataSource(
listResult_3, new String[] {"LastName", "FirstName", "address"});

// Yes i know ! :D , we put the same params list then as i said befor every sub report will take its own parameters values, so don't worry about this task ;)
ParamList.add(params);
ParamList.add(params);
ParamList.add(params);

SourceList.add(dataSource_1);
SourceList.add(dataSource_2);
SourceList.add(dataSource_3);

File reportFile = // the jrxml file template of the report

// We can use also a list of reportFile, so that every page uses his own template :D


CreateReport(jasperReport, ParamList, SourceList);

}

Now we create each sub report and append it to the main report :

Public void CreateReport(File reportFile, List<Map<String, Object>> ParamList,  List<JRDataSource> SourceList){

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
    Map<String, Object> parameters = paramList.get(0);
    JRDataSource datasource = datasourceList.get(0);
    jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, datasource);

    if(paramList.size()>1){
        for(int i=1; i < paramList.size(); i++)
        {
            JasperPrint jasperPrint_next = JasperFillManager.fillReport(jasperReport, paramList.get(i), datasourceList.get(i));
             List pages = jasperPrint_next.getPages();
             for (int j = 0; j < pages.size(); j++) {
               JRPrintPage object = (JRPrintPage) pages.get(j);
               jasperPrint.addPage(object);
             }
        }
    }

}
查看更多
beautiful°
3楼-- · 2019-03-28 06:08

I tried something diffrent.

I used Ireport 4.1.3 and if you right click on detail1 section you can add another detail section.

Add a beak page and it's done.

Hope it helps :) Regards

查看更多
贪生不怕死
4楼-- · 2019-03-28 06:08

Yes, it is possible. You can create the whole report as a composition of four separate subreports. This will enable their reusability and separation of concerns.

查看更多
做自己的国王
5楼-- · 2019-03-28 06:16

Yes, you can even include reports that has no relation to the customer "if that makes sense".

Also, to make things even more entertaining, after completing this report, you can put it in another parent report, then you will get the same report for multiple customers.

查看更多
登录 后发表回答