⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 struts+jasperreport.txt

📁 struts+jasperreport的使用。iReport是jasperReport的ide工具。介绍了如何使用。
💻 TXT
字号:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{   
  
        return prepareReport(mapping, form, request, response);   
  
    }   
  
 public ActionForward prepareReport(ActionMapping mapping, ActionForm form,    
            HttpServletRequest request, HttpServletResponse response) throws Exception {   
  
        String expTo = request.getParameter("ext");  // 传过来的要导出的报表文件后缀   
  
…… ……   
  
…… ……   
  
  Map map = new HashMap(); // 准备 map   
     prepareMapForReport(map);   
  
        List list = prepareListForReport();  // 准备 list   
        if(list == null || list.size() == 0){   
            throw new Exception();   
        }   
  
 //        String fileName = "/reports/new_report1.jasper";    
        String fileName = getJasperFileName();   
  
 。。。。 。。。   
  
// 导出为 PDF   
  
   if (expTo.equals("pdf")) {   
            // pdf   
            try {   
                JasperReport jasperReport = (JasperReport) JRLoader.loadObject(servlet.getServletContext().getRealPath(fileName));   
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JRBeanCollectionDataSource(list));   
                ByteArrayOutputStream baos = new ByteArrayOutputStream();   
                JasperExportManager.exportReportToPdfStream(jasperPrint, baos);   
                    
                response.reset();   
                response.setContentType("application/pdf");   
                response.addHeader("Content-Disposition", "attachment;filename=aaa.pdf");   
                response.setContentLength(baos.size());   
                    
                ServletOutputStream out = response.getOutputStream();   
                baos.writeTo(out);   
                out.flush();   
  
            } catch (Exception e) {   
  
            }   
        }else if(expTo.equals("html") || expTo.equals("htm")){       
      //// 导出为  html   
            try {   
                OutputStream outputStream = response.getOutputStream();   
                JasperReport jasperReport = (JasperReport) JRLoader.loadObject(servlet.getServletContext().getRealPath(fileName));   
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JRBeanCollectionDataSource(list));   
  
                JRHtmlExporter exporter = new JRHtmlExporter();   
  
                exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);   
                exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);   
                exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GB2312");   
                exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);   
                exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");   
                exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE);   
                exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, null);   
                exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, "pt");    
  
                exporter.exportReport();   
  
                outputStream.flush();   
                outputStream.close();   
            } catch (Exception e) {   
                e.printStackTrace();   
  
            }   
        }else if(expTo.equals("excel") ){        
            // // 导出为 excel   
            try {   
                JasperReport jasperReport = (JasperReport) JRLoader.loadObject(servlet.getServletContext().getRealPath(fileName));   
  
                JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JRBeanCollectionDataSource(list));   
  
                ServletOutputStream ouputStream = response.getOutputStream();   
  
                JRXlsExporter exp = new JRXlsExporter();   
                ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();   
  
                exp.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);   
                exp.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);   
                exp.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);   
                exp.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE);   
                exp.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);   
                exp.exportReport();   
  
                byte[] bytes = xlsReport.toByteArray();   
                xlsReport.close();   
  
                response.reset();   
                response.setContentType("application/vnd.ms-excel");   
                response.setHeader("Content-Disposition", "inline; filename=\"file.xls\"");   
                response.setContentLength(bytes.length);   
                ouputStream.write(bytes, 0, bytes.length);   
  
                ouputStream.flush();   
                ouputStream.close();   
  
            } catch (Exception e) {   
  
            }   
        }else if(expTo.equals("rtf") ){      
            //// 导出为 rtf   
               
            JasperReport  jasperReport = (JasperReport) JRLoader.loadObject(servlet.getServletContext().getRealPath(fileName));   
  
   JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, new JRBeanCollectionDataSource(list));   
            
         ServletOutputStream ouputStream = response.getOutputStream();   
            
            JRRtfExporter exporter = new JRRtfExporter();   
      
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);   
//    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);   
    exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "report.rtf");   
    exporter.exportReport();   
       
//                byte[] bytes = ouputStream.toByteArray();   
                ouputStream.close();   
        }   
  
  
        return null;  // 注意这里需要是 null 。。。  一般 在struts1 里面 是类似 return mapping.findForward("。。。") ;    
    }   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -