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

安稳与你 提交于 2019-12-24 03:03:41

问题


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.

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


回答1:


//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/");`



回答2:


you can use HashMap like

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



回答3:


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.



来源:https://stackoverflow.com/questions/8709199/how-to-pass-multiple-parameters-to-reports-and-export-to-pdf-in-jasperreports-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!