日期:2014-05-16  浏览次数:20933 次

使用 jasperreport 来打印报表
		SessionUser user=SessionHelper.getSessionUser();
		userid=user.getId();
		List<StoreOrder> storeOrders=settlementService.printStoreOrders(userid);
		if (storeOrders!=null &&storeOrders.size()>0) {
			for (int i = 0; i < storeOrders.size(); i++) {
				StoreOrder storeOrder=storeOrders.get(i);
				Order order=storeOrder.getOrder();
				ordermoney=ordermoney.add(order.getAmount());
				paymoney=paymoney.add(storeOrder.getMallMoney());
				freight=freight.add(order.getFreight());
			}
			List<ViewReimbursement> vList=settlementService.loadReimbursements(userid);
			if (vList!=null &&vList.size()>0) {
				for (int i = 0; i < vList.size(); i++) {
					ViewReimbursement v=vList.get(i);
					ReimbursementItem reimbursementItem=v.getReimbursement();
					realBackMoney=realBackMoney +reimbursementItem.getRealBackMoney();
				}
			}
			settlement=ordermoney.add(freight).subtract(paymoney).subtract(new BigDecimal(realBackMoney));
		}
		// 设置页面参数
				Map paraMap = new HashMap();
				// 设置明细list
				JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(storeOrders);
				
				
				try {
					InputStream is = this.getClass().getClassLoader().getResourceAsStream("/Settlement.jrxml");
					JasperDesign jasperDesign = JRXmlLoader.load(is);
					JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
					paraMap.put("ordermoney",ordermoney);
					paraMap.put("paymoney",paymoney);
					paraMap.put("settlement",settlement);
					paraMap.put("realBackMoney", realBackMoney);
					paraMap.put("freight", freight);
		            // 写入报表
		            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, paraMap, ds);
		            
		            
		            HttpServletResponse resp = ServletActionContext.getResponse();
		            JRExporter exporter = new JRPdfExporter();
					resp.reset();
					resp.setContentType("application/pdf;UTF-8");
					resp.setHeader("Content-Disposition", "attachment; filename=\""+ URLEncoder.encode("settlementList", "UTF-8") + ".pdf\"");
					
					OutputStream ouputStream = resp.getOutputStream();
					
					// 使用JRPdfExproter导出器导出pdf
					exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
					exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");     
					exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
					exporter.exportReport();
					
					ouputStream.flush();
					ouputStream.close();
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	
?