jasperreports how to export multiple jrmxl files t

2019-08-30 12:09发布

问题:

I have one jrxml file that I would like to populate with 3 different datasources. Then I would like to export to an XLS or XLXS file with the 3 reports concatenated together on one sheet.

String input = "C:\\location\\report1.jrxml";
JasperReport jreport = JasperCompileManager.compileReport(input);         

JasperPrint jprint1 = JasperFillManager.fillReport(jreport, new HashMap<String, Object>(), new JRBeanCollectionDataSource(getReportData(1)));
JasperPrint jprint2 = JasperFillManager.fillReport(jreport, new HashMap<String, Object>(), new JRBeanCollectionDataSource(getReportData(2)));
JasperPrint jprint3 = JasperFillManager.fillReport(jreport, new HashMap<String, Object>(), new JRBeanCollectionDataSource(getReportData(3)));

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

jprintlist.add(jprint1);
jprintlist.add(jprint2);
jprintlist.add(jprint3);

JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT_LIST, jprintlist);

String xlsFile = "C:\\location\\test.xlsx";

exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, xlsFile); 
exporter.exportReport();

The reports are all generating as I would expect, however in excel they are being generated on 3 different tabs, what I would like is to have them be built one on top of another. How can I merge them in this way?

回答1:

You could be to wrap the 3 reports as sub reports in a master report and run that one instead. In order to do so you would need to add the logic for which data source to access into the subreport's data source expression.

alternatively you could post process the excel sheet outside JasperReports.