jasperreport以html格式导出

@RequestMapping(value="/report", method = RequestMethod.GET) 
   public void getReport(@RequestParam(value="id", required=false, defaultValue="") String id,@RequestParam(value="page", required=false, defaultValue="0") String pg,
   
     HttpServletRequest request,HttpServletResponse response) throws IOException
   {
     ServletContext context=request.getServletContext();
     String root_path=context.getRealPath("/");
     response.setContentType("text/html");
     response.setCharacterEncoding("UTF-8");
   PrintWriter out = response.getWriter();
   try
   {
    
    if(getJasperFile(id, context).size()<=Integer.parseInt(pg)){
     return ;
    }
    List<File> lFiles=getJasperFile(id, context);
    //Collections.reverse(lFiles);
    File reportFile = lFiles.get(Integer.parseInt(pg));
    if (!reportFile.exists())
     return ;
     //throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
 
    JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile(reportFile.getPath());
    Map parameters =getParameter(id);
    ArrayList dataList=null;
    if(reportFile.getName().startsWith("jianRGBG")||reportFile.getName().startsWith("jianCYJLB")||reportFile.getName().startsWith("jianJYBZSQB")||reportFile.getName().startsWith("jianPZSQB")){
     dataList=getFeature(id);
    }else{
     dataList=getObject(id,true,false);
    }
       JRBeanCollectionDataSource beanColDataSource =new JRBeanCollectionDataSource(dataList);

    //字报告单目录
       parameters.put("SUBREPORT_DIR", root_path+"/WEB-INF/reports/2014/");
    JasperPrint jasperPrint =
     JasperFillManager.fillReport(
      jasperReport,
      parameters,
      beanColDataSource
      );
    JRXhtmlExporter exporter=new JRXhtmlExporter();
    request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    exporter.setParameter(JRHtmlExporterParameter.FRAMES_AS_NESTED_TABLES,false);
    exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
    exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,request.getContextPath()+"/servlets/image?image=");
    //exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT,"pt");
    exporter.exportReport();
   }
   catch (JRException e)
   {
    /*out.println("<html>");
    out.println("<head>");
    out.println("<title>JasperReports - Web Application Sample</title>");
    out.println("<link rel=stylesheet type=text/css href=../stylesheet.css title=Style>");
    out.println("</head>");
    
    out.println("<body bgcolor=white>");
 
    out.println("<span class=bnew>JasperReports encountered this error :</span>");
    out.println("<pre>");
 
    e.printStackTrace(out);
 
    out.println("</pre>");
 
    out.println("</body>");
    out.println("</html>");*/
    logger.error("预览报告单异常:"+ContextUtil.getTrace(e));
   }finally{
    out.flush();
    out.close();
   }
   }

输出到指定的打印机

@RequestMapping(value="/ptreport", method = RequestMethod.GET) 
   public @ResponseBody Object getPrint_Report(@RequestParam(value="id", required=false, defaultValue="") String id,
     @RequestParam(value="num", required=false, defaultValue="1") String num ,HttpServletRequest request,HttpServletResponse response) throws IOException, JRException
   {
     ServletContext context=request.getServletContext();
     List<File> array=getJasperFile(id, context);
     String root_path=context.getRealPath("/");
     boolean isSuccess=false;
     boolean isAll = false;
     String enable = ContextUtil.getProperty().getProperty("splitReport","0");
     if (!num.equals("3") && enable.equals("1"))
      isAll = true;
     for (int i = 0; i < array.size(); i++) {
      if (isAll && i!=Integer.parseInt(num)-1)
       continue;
      File reportFile = array.get(i);
      if(getStation().getStaType().equals("环保")&&getStation().getName().contains("威宁")){
       Integer n=Integer.parseInt(num)-1;
       if(i!=n){
        continue;
       }
      }
      //河南 合格全打,不合格只打报告单
    if(ContextUtil.getProperty().getProperty("lwType").equals("4")||ContextUtil.getProperty().getProperty("lwType").equals("5")){
     Integer n=Integer.parseInt(num)-1;
       if(i!=n){
        continue;
       }
    }
    if(reportFile.getName().startsWith("jianRGBG")||reportFile.getName().startsWith("jianCYJLB")||reportFile.getName().startsWith("jianJYBZSQB")||reportFile.getName().startsWith("jianPZSQB")){
     Integer n=Integer.parseInt(num)-1;
       if(i!=n){
        continue;
       }
    }
    if (!reportFile.exists()){
     throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
    }
    JasperReport jasperReport = (JasperReport)JRLoader.loadObjectFromFile(reportFile.getPath());
    Map parameters =getParameter(id);
    parameters.put("SUBREPORT_DIR", root_path+"/WEB-INF/reports/2014/");
    ArrayList dataList=null;
    if(reportFile.getName().startsWith("jianRGBG")||reportFile.getName().startsWith("jianCYJLB")||reportFile.getName().startsWith("jianJYBZSQB")||reportFile.getName().startsWith("jianPZSQB")){
     dataList=getFeature(id);
    }else{
     dataList=getObject(id,getStation().getIftd(),true);
    }
       JRBeanCollectionDataSource beanColDataSource =new JRBeanCollectionDataSource(dataList); 
    JasperPrint jasperPrint =JasperFillManager.fillReport(
      jasperReport,
      parameters,
      beanColDataSource
      );
    
    if(getStation().getStaType().equals("环保")){
     isSuccess=ContextUtil.directPrintByPrintName(jasperPrint, getStation().getHbdyj());
    }else{
     String printName="";
     if(ContextUtil.getProperty().getProperty("anywherePrint").equals("1")&&ContextUtil.getProperty().containsKey(HttpUtil.getIp(request))){
      printName=ContextUtil.getProperty().getProperty(HttpUtil.getIp(request));
     }else{
      printName=getStation().getAjdyj();
     }
     if(reportFile.getName().startsWith("jianRGBG")||reportFile.getName().startsWith("jianCYJLB")||reportFile.getName().startsWith("jianJYBZSQB")||reportFile.getName().startsWith("jianPZSQB")){
      if(ContextUtil.getProperty().getProperty("featureReport").equals("1")){
       isSuccess=ContextUtil.directPrintByPrintName(jasperPrint, printName);
      }else{
       isSuccess=ContextUtil.directPrintByPrintName(jasperPrint, printName);
      }
     }else{
      CarTestResult result=(CarTestResult) dataList.get(0);
      if(result.getJylb().contains("等级")||result.getJylb().contains("二级")){
       isSuccess=ContextUtil.directPrintByPrintName(jasperPrint, getStation().getZjdyj());
      }else{
       isSuccess=ContextUtil.directPrintByPrintName(jasperPrint, printName);
      }
     }
    }
    try {
     Thread.sleep(2000);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     logger.error(ContextUtil.getTrace(e));
    }
    }
    Map<String, Object> map = new HashMap<String, Object>();
       if(isSuccess){
        map.put("success", ""+isSuccess); 
       }else{
        map.put("success", ""+isSuccess); 
       }
       return map;
   }


注意:本文归作者所有,未经作者允许,不得转载