void exportToExcel(JasperPrint jasperPrint) throws Exception { javax.swing.JFileChooser jfc = new javax.swing.JFileChooser("reportsample/"); jfc.setDialogTitle("Send Report to Excel"); jfc.setFileFilter( new javax.swing.filechooser.FileFilter() { public boolean accept(java.io.File file) { String filename = file.getName(); return (filename.toLowerCase().endsWith(".xls") || file.isDirectory() || filename.toLowerCase().endsWith(".jrxml")); } public String getDescription() { return "Laporan *.xls"; } }); jfc.setMultiSelectionEnabled(false); jfc.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG); if (jfc.showSaveDialog(null) == javax.swing.JOptionPane.OK_OPTION) { JExcelApiExporter exporter = new JExcelApiExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter( JRExporterParameter.OUTPUT_FILE_NAME, changeFileExtension(jfc.getSelectedFile().getPath(), "xls")); exporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporter.setParameter(JExcelApiExporterParameter.IS_FONT_SIZE_FIX_ENABLED, Boolean.TRUE); exporter.exportReport(); } }
@SuppressWarnings({"rawtypes", "unchecked"}) private byte[] gerarRelatorioJasperXLS(JasperReport nomeJasper, Map parametros, JRDataSource ds) throws JRException, IOException { JasperPrint jasperPrint = JasperFillManager.fillReport(nomeJasper, parametros, ds); JExcelApiExporter xlsExporter = new JExcelApiExporter(); ByteArrayOutputStream xlsReport = new ByteArrayOutputStream(); xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport); xlsExporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE); xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); xlsExporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.TRUE); xlsExporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); xlsExporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE); xlsExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); // Parametro que remove campos em branco quando um campo ocupa mais de uma linha e os outros não xlsExporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE); xlsExporter.exportReport(); xlsReport.close(); return xlsReport.toByteArray(); }
public void jxl() 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() + ".jxl.xls"); JExcelApiExporter exporter = new JExcelApiExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); }
public void jxl() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/FirstJasper.jrprint"); JasperPrint jasperPrint = (JasperPrint) JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); Map dateFormats = new HashMap(); dateFormats.put("EEE, MMM d, yyyy", "ddd, mmm d, yyyy"); JExcelApiExporter exporter = new JExcelApiExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.FORMAT_PATTERNS_MAP, dateFormats); exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); }
public static void exportReportPlainXls(JasperPrint jp, String path) throws JRException, FileNotFoundException { // JRXlsExporter exporter = new JRXlsExporter(); JExcelApiExporter exporter = new JExcelApiExporter(); File outputFile = new File(path); File parentFile = outputFile.getParentFile(); if (parentFile != null) { parentFile.mkdirs(); } FileOutputStream fos = new FileOutputStream(outputFile); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos); exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.setParameter( JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporter.exportReport(); logger.debug("Report exported: " + path); }
public void render( String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { // some containers call filters on EVERY request, even forwarded ones, // so let it know that it came from the control servlet if (request == null) { throw new ViewHandlerException( "The HttpServletRequest object was null, how did that happen?"); } if (UtilValidate.isEmpty(page)) { throw new ViewHandlerException("View page was null or empty, but must be specified"); } if (UtilValidate.isEmpty(info)) { Debug.logInfo( "View info string was null or empty, (optionally used to specify an Entity that is mapped to the Entity Engine datasource that the report will use).", module); } // tell the ContextFilter we are forwarding request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, Boolean.valueOf(true)); Delegator delegator = (Delegator) request.getAttribute("delegator"); if (delegator == null) { throw new ViewHandlerException("The delegator object was null, how did that happen?"); } try { JasperReport report = (JasperReport) jasperReportsCompiledCache.get(page); if (report == null) { synchronized (this) { report = (JasperReport) jasperReportsCompiledCache.get(page); if (report == null) { InputStream is = context.getResourceAsStream(page); report = JasperCompileManager.compileReport(is); jasperReportsCompiledCache.put(page, report); } } } response.setContentType("application/xls"); Map parameters = (Map) request.getAttribute("jrParameters"); if (parameters == null) { parameters = UtilHttp.getParameterMap(request); } JRDataSource jrDataSource = (JRDataSource) request.getAttribute("jrDataSource"); JasperPrint jp = null; if (jrDataSource == null) { String datasourceName = delegator.getEntityHelperName(info); if (UtilValidate.isNotEmpty(datasourceName)) { Debug.logInfo( "Filling report with connection from datasource: " + datasourceName, module); jp = JasperFillManager.fillReport( report, parameters, ConnectionFactory.getConnection(datasourceName)); } else { Debug.logInfo("Filling report with an empty JR datasource", module); jp = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource()); } } else { Debug.logInfo("Filling report with a passed in jrDataSource", module); jp = JasperFillManager.fillReport(report, parameters, jrDataSource); } if (jp.getPages().size() < 1) { throw new ViewHandlerException("Report is Empty (no results?)"); } else { Debug.logInfo("Got report, there are " + jp.getPages().size() + " pages.", module); } JExcelApiExporter exporter = new JExcelApiExporter(); exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jp); exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, response.getOutputStream()); exporter.exportReport(); } catch (IOException ie) { throw new ViewHandlerException("IO Error in report", ie); } catch (java.sql.SQLException e) { throw new ViewHandlerException("Database error while running report", e); } catch (Exception e) { throw new ViewHandlerException("Error in report", e); // } catch (ServletException se) { // throw new ViewHandlerException("Error in region", se.getRootCause()); } }