public void rtf() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/DataSourceReport.jrprint"); JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".rtf"); JRRtfExporter exporter = new JRRtfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.exportReport(); System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); }
@SuppressWarnings({"rawtypes", "unchecked"}) private byte[] gerarRelatorioJasperDoc(JasperReport nomeJasper, Map parametros, JRDataSource ds) throws JRException, IOException { JasperPrint jasperPrint = JasperFillManager.fillReport(nomeJasper, parametros, ds); JRRtfExporter docExporter = new JRRtfExporter(); ByteArrayOutputStream docReport = new ByteArrayOutputStream(); docExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); docExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); docExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, docReport); docExporter.exportReport(); docReport.close(); return docReport.toByteArray(); }
/** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { System.out.println("Genetate Report"); // Get requested student id from user request // String studentId = request.getParameter("sid"); // Get requested report type from user request String reportType = request.getParameter("reportType"); // Setting Default report Type if (reportType == null || reportType.trim().length() == 0) { reportType = "pdf"; } // Get servlet Out put stream to displat report at client side ServletOutputStream sos = response.getOutputStream(); // Construct path for your .jrxml file ServletContext application = getServletContext(); String prefix = application.getRealPath("\\"); // String file = getInitParameter("reportTemplet"); String file = "AveTechEventReport.jrxml"; String path = prefix + file; System.out.println("Path : " + path); // Load that .jrxml file in Jasper Design JasperDesign jasperDesign = JRXmlLoader.load(path); // Compile that .jrxml file into .jasper file JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); /* Populate data from your database It must contain all the fields used in designing a .jrxml file Here we have used Java Bean Data Source in generating report. So populate ArrayList which contains your collection of your beans. */ JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(AdminDAO.getAllEventData(), false); // Filling the reports using your .jasper file and generated Java Bean Datasource JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, ds); // Response for generating PDF report if (reportType != null && reportType.equals("pdf")) { System.out.println("PDF"); response.setContentType("application/pdf"); JRPdfExporter expoterPDF = new JRPdfExporter(); expoterPDF.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); expoterPDF.setParameter(JRExporterParameter.OUTPUT_STREAM, sos); expoterPDF.exportReport(); } // Response for generating EXCEL report if (reportType != null && reportType.equals("excel")) { response.setContentType("application/vnd.ms-excel"); JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, sos); exporterXLS.exportReport(); } // Response for generating WORD report if (reportType != null && reportType.equals("word")) { response.setContentType("application/msword"); JRRtfExporter exporterRTF = new JRRtfExporter(); exporterRTF.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporterRTF.setParameter(JRExporterParameter.OUTPUT_STREAM, sos); exporterRTF.exportReport(); } // Closing all opened streams sos.flush(); sos.close(); } catch (Exception ex) { ex.printStackTrace(); } }