iReport打印(支持多个模版交叉打印)

版权声明:未经允许,不得转载 https://blog.csdn.net/dhj199181/article/details/83112142
      //根据实际情况从数据库中获取数据
      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;
  }

注:关键代码就上面这些,具体细节看实际业务

猜你喜欢

转载自blog.csdn.net/dhj199181/article/details/83112142
今日推荐