/** * */ public void xls() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/ScriptletReport.jrprint"); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls"); JRXlsExporter exporter = new JRXlsExporter(); 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/ScriptletReport.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 ods() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/ScriptletReport.jrprint"); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".ods"); JROdsExporter exporter = new JROdsExporter(); 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("ODS creation time : " + (System.currentTimeMillis() - start)); }
/** * */ public void xlsx() throws JRException { long start = System.currentTimeMillis(); File sourceFile = new File("build/reports/ScriptletReport.jrprint"); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xlsx"); JRXlsxExporter exporter = new JRXlsxExporter(); 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("XLSX creation time : " + (System.currentTimeMillis() - start)); }
private void exportToXLS() { log.debug("exporting to XLS"); xlsExporter = new JRXlsExporter(); setExportParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); setExportParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); xlsExporter.setParameters(getExportParameters()); try { start = System.currentTimeMillis(); xlsExporter.exportReport(); log.info("export running time (msec): " + (System.currentTimeMillis() - start)); } catch (JRException jre) { jre.printStackTrace(); log.error(jre.getMessage()); } }
@Test public void renderAsXlsWithExporterParameters() throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(); Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>(); SimpleProgressMonitor monitor = new SimpleProgressMonitor(); exporterParameters.put(JRXlsExporterParameter.PROGRESS_MONITOR, monitor); JasperReportsUtils.renderAsXls(getReport(), getParameters(), getData(), os, exporterParameters); byte[] output = os.toByteArray(); assertXlsOutputCorrect(output); assertTrue(monitor.isInvoked()); }
private void exportReportToExcelStream(JasperPrint jasperPrint, OutputStream os) throws JRException{ JRXlsExporter exp = new JRXlsExporter(); exp.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exp.setParameter(JRExporterParameter.OUTPUT_STREAM, os); exp.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE); exp.setParameter(JRXlsExporterParameter.OFFSET_X, 0); exp.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, Boolean.FALSE); exp.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, true); exp.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, false); exp.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, false); exp.setParameter(JRXlsExporterParameter.MAXIMUM_ROWS_PER_SHEET,Integer.decode("65000")); exp.exportReport(); }
private static JRExporter getJREXporter(final String extension) { if ("pdf".equalsIgnoreCase(extension)) { JRPdfExporter exporter = new JRPdfExporter(); // exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();"); return exporter; } else if ("html".equalsIgnoreCase(extension)) { return new JRHtmlExporter(); } else if ("xls".equalsIgnoreCase(extension)) { JRXlsExporter exporterXLS = new JRXlsExporter(); exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporterXLS.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporterXLS.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); return exporterXLS; } else if ("txt".equalsIgnoreCase(extension)) { return new JRTextExporter(); } else if ("csv".equalsIgnoreCase(extension)) { return new JRCsvExporter(); } else if ("docx".equalsIgnoreCase(extension)) { return new JRDocxExporter(); } return null; }
@Override public byte[] generateExcel(List<ExcelSheetReportData> excelSheetsReportData) throws Exception { if (excelSheetsReportData == null || excelSheetsReportData.size() == 0) { throw new Exception("There are no data to make report."); } String[] sheetNamesArray = new String[excelSheetsReportData.size()]; List<JasperPrint> jasperPrints = new ArrayList<JasperPrint>(); int i = 0; for (ExcelSheetReportData excelSheetReportData : excelSheetsReportData) { sheetNamesArray[i] = excelSheetReportData.getSheetName(); i++; JRDataSource reportDataSource = new JRMapCollectionDataSource( excelSheetReportData.getSheetData()); JasperPrint jasperPrint = null; if (excelSheetReportData.getSheetReportLocation() != null && !excelSheetReportData.getSheetReportLocation() .equals("")) { jasperPrint = JasperFillManager.fillReport( excelSheetReportData.getSheetReportLocation(), excelSheetReportData.getSheetParameters(), reportDataSource); } else { jasperPrint = JasperFillManager.fillReport( excelSheetReportData.getSheetReport(), excelSheetReportData.getSheetParameters(), reportDataSource); } jasperPrints.add(jasperPrint); } JasperPrint firstJasperPrint = jasperPrints.get(0); if (jasperPrints.size() > 1) { for (i = 1; i < jasperPrints.size(); i++) { List<JRPrintPage> additionalPages = new ArrayList<JRPrintPage>( jasperPrints.get(i).getPages()); int fistJasperPrintPages = firstJasperPrint.getPages().size(); for (int count = 0; count < additionalPages.size(); count++) { firstJasperPrint.addPage(fistJasperPrintPages, additionalPages.get(count)); } } } JRExporter exporter = new JExcelApiExporter(); exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, firstJasperPrint); exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); exporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter( JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES, sheetNamesArray); exporter.setParameter(JExcelApiExporterParameter.IS_COLLAPSE_ROW_SPAN, Boolean.TRUE); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(32768); exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, outputStream); // exporter.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME, // "C:/development/workspaces/jasper/report1.xls"); exporter.exportReport(); return outputStream.toByteArray(); }
public void testRenderAsXlsWithExporterParameters() throws Exception { ByteArrayOutputStream os = new ByteArrayOutputStream(); Map<JRExporterParameter, Object> exporterParameters = new HashMap<JRExporterParameter, Object>(); SimpleProgressMonitor monitor = new SimpleProgressMonitor(); exporterParameters.put(JRXlsExporterParameter.PROGRESS_MONITOR, monitor); JasperReportsUtils.renderAsXls(getReport(), getParameters(), getData(), os, exporterParameters); byte[] output = os.toByteArray(); assertXlsOutputCorrect(output); assertTrue(monitor.isInvoked()); }
public static byte[] getExcel(JasperPrint jasperPrint, Map<String, Boolean> xlsParameters) throws JRException { byte[] content = null; ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); JRXlsExporter exporter = new JRXlsExporter(); // System.out.println("XLS Parameters"); // System.out.println("--------------"); // System.out.println("isDetectCellType=" + xlsParameters.get(JasperUtil.IS_DETECT_CELL_TYPE)); // System.out.println("isWhitePageBackground=" + xlsParameters.get(JasperUtil.IS_WHITE_PAGE_BACKGROUND)); // System.out.println("isRemoveEmptySpaceBetweenRows=" + xlsParameters.get(JasperUtil.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS)); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, xlsParameters.get(JasperUtil.IS_WHITE_PAGE_BACKGROUND)); exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, xlsParameters.get(JasperUtil.IS_DETECT_CELL_TYPE)); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, xlsParameters.get(JasperUtil.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS)); content = getBytes(exporter, baos, jasperPrint); } finally { if (baos != null) { try { baos.flush(); baos.close(); } catch (Exception e) { e.printStackTrace(); } } } return content; }