实现效果:点击导出后弹出下载框
入口:
public ActionForward printVoucher(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws SystemException { HttpSession session = request.getSession(); Hotelbookheader hbh = (Hotelbookheader)session.getAttribute("adminHotelbookheaderDetails"); String type = request.getParameter("type"); log.info("Test output !"); byte[] voucher = null; DynaActionForm updateBookingForm = (DynaActionForm)form; if(type != null && "agent".equalsIgnoreCase(type)) { hbh = (Hotelbookheader)session.getAttribute("HotelbookheaderDetails"); voucher = this.initVoucherPDF(hbh,session,response,updateBookingForm); if(voucher != null) { this.toPdfbytes(response, voucher, "voucher.pdf"); } request.setAttribute(MyConstants.MARKETS_KEY, "Save voucher success"); return mapping.findForward("admindetails"); } hbh.setVoucherRemarks(updateBookingForm.getString("voucherRemarks")); hbh.setIsAllowAgentReprint(new Byte(updateBookingForm.getString("isAllowAgentReprint"))); hbh.setRemarks(updateBookingForm.getString("voucherRemarks")); String voucherno = updateBookingForm.getString("voucherno"); String agentxonumber = updateBookingForm.getString("agentxonumber"); AmendBookLogUtils logUtils = new AmendBookLogUtils(request); logUtils.log("Voucher","Voucher Issued", hbh.getBookingno(), ""); //生成pdf if("dataOnly".equals(updateBookingForm.getString("isPdf"))) { voucher = this.initBlankVoucherPDF(hbh,session,response,updateBookingForm); } if("withBackground".equals(updateBookingForm.getString("isPdf"))){ voucher = this.initVoucherPDF(hbh,session,response,updateBookingForm); } if(voucher != null) { this.toPdfbytes(response, voucher, "voucher.pdf"); hbh.setStatus(MyConstants.Guarantee); } hotelbookheaderFacade.update(hbh); request.setAttribute(MyConstants.MESSAGE_KEY, "Save voucher success"); return mapping.findForward("admindetails"); }
private void toPdfbytes(HttpServletResponse response,byte[] bb,String filename) throws SystemException { response.reset(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); response.setContentLength(bb.length); try { ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bb, 0, bb.length); ouputStream.flush(); ouputStream.close(); } catch (IOException e) { e.printStackTrace(); throw new SystemException(e.getMessage()); } }
关键地方:
private byte[] initBlankVoucherPDF(Hotelbookheader hbh,HttpSession session,HttpServletResponse response,DynaActionForm updateBookingForm) throws SystemException { String path = "WebRoot/images"; if(session != null) path = session.getServletContext().getRealPath("images"); log.info(path); Map readMap = updateBookingForm.getMap(); List<Map<Object, Object>> mapList = new ArrayList<Map<Object,Object>>(); Map<Object, Object> map = new HashMap<Object, Object>(); map.put("neHotelLogo",path + "\\voucher\\NE_hotel_logo.jpg"); map.put("neVoucherLogo",path + "\\voucher\\booking.jpg"); map.put("roomMessage", roomMessage); map.put("extraBed", "Nil"); //构造两个子报表 map.put("passengerList", paxList(hbh,map)); map.put("items", itemsList(hbh,updateBookingForm)); //mapList是fillReport时的数据源 new JRBeanCollectionDataSource(mapList) mapList.add(map); try { byte[] bb = JasperReportUtil.createPdfByXmlName("ServiceVoucher", mapList, session); return bb; } catch (JRException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
数据源是new JRBeanCollectionDataSource(mapList) ,参数是一个List,List中封装了一个Map,基本数据都放在这个Map中
子报表数据分别放入对应的Map中,再将这些Map封装为一个List,传入数据源List的Map,如 map.put("passengerList", paxList(hbh,map))
子报表的数据封装:
private List itemsList(Hotelbookheader hbh,DynaActionForm updateBookingForm) { Set priceDetailsSet = hbh.getHotelbookpricedetails(); List<HotelRoomPriceVO> list = AdminUpdateOrderUtils.initAdminUpdatePriceListVO(updateBookingForm,priceDetailsSet); int num1 = 0; int num2 = 0; int num3 = 0; for(int i =0;i<list.size();i++){ HotelRoomPriceVO vv = list.get(i); if (vv.getAdditionalitem1paxno() != null && !vv.getAdditionalitem1paxno().equals("")) { num1+=Integer.parseInt(vv.getAdditionalitem1paxno()); } if (vv.getAdditionalitem2paxno() != null && !vv.getAdditionalitem2paxno().equals("")) { num2+=Integer.parseInt(vv.getAdditionalitem2paxno()); } if (vv.getAdditionalitem3paxno() != null && !vv.getAdditionalitem3paxno().equals("")) { num3+=Integer.parseInt(vv.getAdditionalitem3paxno()); } List lt = new ArrayList(); for(int i =0;i<3;i++){ HashMap m = new HashMap(); if(i==0){ m.put("name", list.get(0).getAdditionalitemname1()); m.put("num", num1); }else if(i == 1){ m.put("name", list.get(0).getAdditionalitemname2()); m.put("num", num2); }else{ m.put("name", list.get(0).getAdditionalitemname3()); m.put("num", num3); } lt.add(m); } return lt; }
public static List<Map<Object, Object>> paxList(Hotelbookheader hbh,Map<Object, Object> map) { List<Map<Object,Object>> mapList = new ArrayList<Map<Object, Object>>(); String roomtype = ""; String roomcount = ""; Set paxDetailsSet = hbh.getHotelbookpaxdetails(); for (Iterator iter1 = paxDetailsSet.iterator(); iter1.hasNext();) { Map<Object, Object> paxMap = new HashMap<Object,Object>(); Hotelbookpaxdetail pax = (Hotelbookpaxdetail) iter1.next(); roomtype = pax.getRoomtype().trim(); paxMap.put("gender", pax.getGender() == null ? "" : pax.getGender()); paxMap.put("paxname", pax.getGivenname()+"/" + pax.getSurname()); //一排显示两个 if(iter1.hasNext()) { Hotelbookpaxdetail pax1 = (Hotelbookpaxdetail) iter1.next(); paxMap.put("gender1", pax1.getGender() == null ? "" : pax1.getGender()); paxMap.put("paxname1", pax1.getGivenname()+"/" + pax1.getSurname()); } mapList.add(paxMap); } return mapList; }
真正导出的部分:
导出Excel只需将JRPdfExporter exporter = new JRPdfExporter(); 换为JExcelApiExporter exporter = new JExcelApiExporter();即可!
public static byte[] createPdfByXmlName(String reportName, List<Map<Object,Object>> mapList,HttpSession session) throws JRException, IOException { log.warn("enter createPdfByXmlName"); String path = "WebRoot/reports"; if (session != null) { path = session.getServletContext().getRealPath("reports"); } JasperReport jasperReport; JasperPrint jasperPrint; JRPdfExporter exporter = new JRPdfExporter(); ByteArrayOutputStream oStream = new ByteArrayOutputStream(); try { File file = new File(path + "/" + reportName + ".jasper"); if (!file.exists()) { JasperCompileManager.compileReportToFile(path + "/"+ reportName + ".jrxml",path + "/"+ reportName + ".jasper"); log.info(reportName + ".jrxml"); } jasperReport = (JasperReport) JRLoader.loadObject(file); Map tempMap = new HashMap(); tempMap.put("SUBREPORT_DIR", path + "/"); jasperPrint = JasperFillManager.fillReport(jasperReport, tempMap, new JRBeanCollectionDataSource(mapList)); exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING,"UTF-8"); exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, oStream); exporter.exportReport(); byte[] bytes = oStream.toByteArray(); oStream.close(); return bytes; }catch(JRException e){ e.printStackTrace(); throw new JRException(e); } }
Ireprot中:
上图指定子报表路径!
此处的$F{items}即为数据源List的Map中封装的子报表的List,与上面对应
另:此处必须使用JRBeanCollectionDataSource的全路径,否则会报错!
注意此处items字段的类型为java.util.ArrayList,如图:
注意地方:
子报表大小刚刚放下数据即可,不可留太多空白,子报表页面的大小就是它在父报表中所占的大小!
详细子报表还可参考网上的这个例子:
http://hi.baidu.com/q2881818/blog/item/765cefb3d79e13aed9335ab6.html
几个很有价值的子报表文章:
iReport 中父报表给子报表传参数
http://wenku.baidu.com/view/48c139ece009581b6bd9eb88.html
iReport 4.1 报表制作,子报表,实例解析
http://blog.csdn.net/q326527970/article/details/7049047
ireport 的一些技巧
http://azrael6619.iteye.com/blog/753877