/** * */ 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)); }
@Override public void exportElement(JExcelApiExporterContext exporterContext, JRGenericPrintElement element, JRExporterGridCell gridCell, int colIndex, int rowIndex, int emptyCols, int yCutsRow, JRGridLayout layout) { try { JExcelApiExporter exporter = (JExcelApiExporter) exporterContext .getExporter(); JasperReportsContext reportsContext = exporterContext .getJasperReportsContext(); JRPrintImage printImage = getImage(reportsContext, element); exporter.exportImage(printImage, gridCell, colIndex, rowIndex, emptyCols, yCutsRow, layout); } catch (Exception e) { throw new RuntimeException(e); } }
@Override protected JExcelApiExporter createExporter(JasperReportsConfiguration jContext, JRExportProgressMonitor monitor) { JExcelApiExporter exp = new JExcelApiExporter(jContext); SimpleJxlReportConfiguration rconf = new SimpleJxlReportConfiguration(); setupReportConfiguration(rconf, monitor); exp.setConfiguration(rconf); return exp; }
@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(); }
/** * generate a ByteArrayOutputStream from given JasperPrint object for the Excel report * * @param jasperPrint transform to excel report * @return reporting ByteArrayOutputStream * @throws ReportingException when the JasperPrint null * @throws JRException */ public ByteArrayOutputStream generateExcelReport(JasperPrint jasperPrint) throws ReportingException, JRException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); if (jasperPrint == null) { throw new ReportingException("jasperPrint null, can't convert to excel report"); } try { // Remove the pageHeader from pages except starting page jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.1", "pageHeader"); // Remove the column headers except the first one jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.2", "columnHeader"); // Remove the pageFooter from all the pages jasperPrint.setProperty("net.sf.jasperreports.export.xls.exclude.origin.band.2", "pageFooter"); // set the JXL parameters to generate Excel report JExcelApiExporter jExcelApiExporter = new JExcelApiExporter(); jExcelApiExporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint); jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); jExcelApiExporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, outputStream); jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_IGNORE_CELL_BORDER,Boolean.TRUE); jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE); jExcelApiExporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); jExcelApiExporter.setParameter(JExcelApiExporterParameter.OFFSET_X,0); jExcelApiExporter.setParameter(JExcelApiExporterParameter.OFFSET_Y,0 ); jExcelApiExporter.exportReport(); } catch (JRException e) { throw new JRException("Error occurred exporting Excel report ", e); } return outputStream; }
@Override public GenericElementHandler getHandler(String elementName, String exporterKey) { if (WmsMapPrintElement.WMS_MAP_ELEMENT_NAME.equals(elementName)) { if (JRGraphics2DExporter.GRAPHICS2D_EXPORTER_KEY .equals(exporterKey)) { return WmsMapElementGraphics2DHandler.getInstance(); } if (JRHtmlExporter.HTML_EXPORTER_KEY.equals(exporterKey) || JRXhtmlExporter.XHTML_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementHtmlHandler.getInstance(); } else if (JRPdfExporter.PDF_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementPdfHandler.getInstance(); } else if (JRXlsExporter.XLS_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementXlsHandler.getInstance(); } else if (JExcelApiExporter.JXL_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementJExcelApiHandler.getInstance(); } // else // if(JExcelApiMetadataExporter.JXL_METADATA_EXPORTER_KEY.equals(exporterKey)) // { // return MapElementJExcelApiMetadataHandler.getInstance(); // } else if (JRXlsxExporter.XLSX_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementXlsxHandler.getInstance(); } else if (JRDocxExporter.DOCX_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementDocxHandler.getInstance(); } else if (JRPptxExporter.PPTX_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementPptxHandler.getInstance(); } else if (JRRtfExporter.RTF_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementRtfHandler.getInstance(); } else if (JROdtExporter.ODT_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementOdtHandler.getInstance(); } else if (JROdsExporter.ODS_EXPORTER_KEY.equals(exporterKey)) { return WmsMapElementOdsHandler.getInstance(); } } return null; }