iReport列印(支援多個模版交叉列印)
阿新 • • 發佈:2018-11-13
//根據實際情況從資料庫中獲取資料 List<User> userList = new ArrayList<>(); User printBean = null; JasperPrint jasperPrint = null; List<JasperPrint> jasperPrintList = new ArrayList<>(); for (int i = 0; i < userList.size(); i++) { printBean = userList.get(i); HashMap<String, Object> params = new HashMap<>();// 建立引數表 if (i == 0) { jasperPrint = getJasperPrint(printBean ,"d:/test/test1.jasper", params);// 模版1 jasperPrintList.add(jasperPrint); }else if(i == 1){ jasperPrint = getJasperPrint(printBean ,"d:/test/test2.jasper", params);// 模版2 jasperPrintList.add(jasperPrint); }else if(i == 2){ jasperPrint = getJasperPrint(printBean ,"d:/test/test3.jasper", params);// 模版3 jasperPrintList.add(jasperPrint); }else{ jasperPrint = getJasperPrint(printBean ,"d:/test/test1.jasper", params);// 模版1 jasperPrintList.add(jasperPrint); } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); JRPdfExporter exporter = new JRPdfExporter(); exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos)); exporter.exportReport(); byte[] bytes = baos.toByteArray(); // 寫出檔案的型別 response.setContentType("application/pdf;charset=UTF-8"); baos.close(); outputStream.write(bytes); outputStream.flush(); private JasperPrint getJasperPrint(User user, String url, Map<String, Object> params) { JasperPrint jasperPrint = null; JRDataSource source = null; try { source = new JRBeanCollectionDataSource(Arrays.asList(user)); jasperPrint = JasperFillManager.fillReport(url, params, source); return jasperPrint; } catch (JRException e) { logger.error("列印異常",e); } return null; }
注:關鍵程式碼就上面這些,具體細節看實際業務