JasperReports + iReport

摘要:JasperReports + iReport

Eclipse + Tomcat 設定

http://youthhng.pixnet.net/blog/post/35658541-%5Bjsp%5D-jsp%E7%92%B0%E5%A2%83%E5%AE%89%E8%A3%9D---eclipse-%2B-tomcat

 

JasperReport原理:

http://www.itc.ntnu.edu.tw/download/column/1000405_JasperReports.pdf

iReport參考:

http://enetq.blog.51cto.com/479739/800135

 

iReport 後端餵資料程式(參考)

String id=request.getParameter("id");
  String hql = "from Users as users where 1=1"; 
  hql=hql+String.format(" and ID=%d", Integer.parseInt(id));
  UsersDao dao = new UsersDao();
  List<Users> users = (ArrayList<Users>) dao.query(hql);  
  JRDataSource ds = new JRBeanCollectionDataSource(users);
  String sourceFileName = new File(this.getServletContext().getRealPath(
    "/Report/UsersReport.jasper")).getPath();
  System.out.println(sourceFileName);
  
  Map<String, Object> params = new HashMap<String, Object>();
 
  JasperPrint jasperPrint = JasperFillManager.fillReport(
    sourceFileName, params, ds);
  
  ServletOutputStream servletOutputStream=response.getOutputStream();
  
  JRPdfExporter exporter=new JRPdfExporter();
  exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT,jasperPrint);
  exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM,servletOutputStream);
  
  //response.setHeader("Content-Disposition","attachment;filename=users.pdf");
  response.setHeader("Content-Disposition","inline");
  response.setContentType("application/pdf");
  
  exporter.exportReport();