You need:
1.) Eclipse
2.) iReport - http://community.jaspersoft.com/project/ireport-designer
Steps:
1.) First nosotros quest to utilization a model for our report. For instance nosotros convey a Student model:
public degree Student { somebody String name; somebody int age; somebody String address; }
2.) We quest to utilization the study inward iReport.
a.) Create a datasource:
Factory class=com.czetsuya.jasper.edition.StudentReportBean
Static method=createBeanCollection
3.) Drag a listing element to the study designer too laid upwardly the information origin to our newly created one. You should come across something like:
4.) Create the StudentReportBean class, the ane that generates the data.
public degree StudentReportBean { world StudentReportBean() { } world static List<student> createBeanCollection() { List<student> students = novel ArrayList<student>(); Student a = novel Student(); a.setName("Tifa Lockheart"); a.setAge(19); a.setAddress("Final Fantasy VII"); students.add(a); Student b = novel Student(); b.setName("Rinoa Heartilly"); b.setAge(19); b.setAddress("Final Fantasy VIII"); students.add(b); furnish students; } }
5.) And inward our instance a managed edible bean that gets trigger from a ascendancy push clit inward UI.
public void run() throws JRException, IOException { Collection<student> students = StudentReportBean.createBeanCollection(); JRBeanCollectionDataSource beanCollectionDataSource = novel JRBeanCollectionDataSource( students, false); String reportPath = FacesContext.getCurrentInstance().getExternalContext() .getRealPath("/WEB-INF/classes/StudentReport.jasper"); JasperReport jasperReport; jasperReport = (JasperReport) JRLoader.loadObjectFromFile(reportPath); Map<string object=""> params = novel HashMap<string object="">(); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, beanCollectionDataSource); HttpServletResponse reply = (HttpServletResponse) FacesContext.getCurrentInstance() .getExternalContext().getResponse(); ServletOutputStream out = response.getOutputStream(); response.setContentType("application/pdf"); response.setHeader("Content-disposition", "attachment; filename=student-report.pdf"); JasperExportManager.exportReportToPdfStream(jasperPrint, out); out.flush(); out.close(); }
6.) You should live on able to download the generated study from a browser.
*Note: You tin download the projection from my google code account:
https://code.google.com/p/czetsuya/source/browse/#svn%2Ftrunk%2Fjasper-demo
0 komentar:
Please comment if there are any that need to be asked.