简体   繁体   中英

How to pass multiple parameters to reports and export to PDF in JasperReports from Java

I'm trying to use Jasper Reports to help with reporting in my application. I will have to display my reports in HTML (JSP) and would also need to be able to export the reports to PDF from within my web page.
Most of my reports require multiple parameters, and I can't figure out how to pass them from my Servlet (if I have to pass them there) to the report.

PS: I'm not using frameworks of any sort, it would be great if you could suggest a framework free implementation.

you can use HashMap like

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("parameterName","value");
JasperPrint print = JasperFillManager.fillReport(report,map,con);

This example will help you, it doesn't require any framework. It exports the report as a PDF. And you can use map as Anil had already explained

HashMap<String, Object> map = new HashMap<String, Object>();
map.put("parameterName","value");
map.put("parameterName2","value2");
map.put("parameterName3","value3");
JasperPrint print = JasperFillManager.fillReport(report,map,con);

to pass multiple parameters.

//Preparing data - change this as per your requirement
List<DataDTO> dataDTOList = new ArrayList<DataDTO>();
DataDTO dataDTO = new DataDTO; 
dataDTO.setFirstName("FirstName"); // in your Jasper field name is 'firstName' as type String
dataDTO.setLastName("LastName"); // in your Jasper field name is 'lastName' as type String
dataDTOList.add(dataDTO);

// Adding data
JRDataSource jrdatasource = new JRBeanCollectionDataSource(dataDTOList);

// Exporting report
File jasperFile = new File("C:/YourReport.jasper"); // change this
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new FileInputStream(jasperFile.getAbsolutePath()));             
Map parameters = new HashMap();
JRConcurrentSwapFile jrSwapFile = new JRConcurrentSwapFile("C:/PDFOutput/"),30,2);
JRSwapFileVirtualizer virtualizer = new JRSwapFileVirtualizer(2,jrSwapFile,true);
parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);
JasperPrint jpPrintObj = JasperFillManager.fillReport(jasperReport,parameters,jrdatasource);
JasperExportManager.exportReportToPdfFile(jpPrintObj,"C:/PDFOutput/");`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM