@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; }
public void store() { Preferences pref = IReportManager.getPreferences(); pref.putBoolean(JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE, jCheckBoxCreateCustomPalette.isSelected() ); pref.put(JExcelApiExporterParameter.PROPERTY_PASSWORD, jTextFieldPassword.getText()); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN, jCheckBoxCollapseRowSpan.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE, jCheckBoxAutoDetectCellType.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED, jCheckBoxFontSizeFixEnabled.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BORDER, jCheckBoxIgnoreCellBorder.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BACKGROUND, jCheckBoxIgnoreCellBackground.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_GRAPHICS, jCheckBoxIgnoreGraphics.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_IMAGE_BORDER_FIX_ENABLED, jCheckBoxImageBorderFixEnabled.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_ONE_PAGE_PER_SHEET, jCheckBoxOnePagePerSheet.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, jCheckBoxRemoveEmptySpaceBetweenColumns.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, jCheckBoxRemoveEmptySpaceBetweenRows.isSelected() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_WHITE_PAGE_BACKGROUND, jCheckBoxWhitePageBackground.isSelected() ); SpinnerNumberModel model = (SpinnerNumberModel)jSpinnerMaximumRowsPerSheet.getModel(); pref.putInt(JRXlsAbstractExporterParameter.PROPERTY_MAXIMUM_ROWS_PER_SHEET, model.getNumber().intValue()); pref.putBoolean(JRPropertiesUtil.PROPERTY_PREFIX + "export.xls.useSheetNames", jCheckBoxUseSheetNames.isSelected() ); pref.put(JRPropertiesUtil.PROPERTY_PREFIX + "export.xls.sheetNames", jTextArea1.getText().trim()); pref.put(JRXlsAbstractExporter.PROPERTY_FREEZE_COLUMN, jTextFieldColumn.getText() ); pref.putInt(JRXlsAbstractExporter.PROPERTY_FREEZE_ROW, ((SpinnerNumberModel)jSpinnerFreezeRow.getModel()).getNumber().intValue() ); pref.putBoolean(JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED, jCheckBoxFixFontSize.isSelected()); // if (jComboBoxColumnEdge.getSelectedIndex() > 0) // { // pref.put(JRXlsAbstractExporter.PROPERTY_FREEZE_COLUMN_EDGE, (String) ((Tag)jComboBoxColumnEdge.getSelectedItem()).getValue()); // } // else // { // pref.remove(JRXlsAbstractExporter.PROPERTY_FREEZE_COLUMN_EDGE); // } // if (jComboBoxRowEdge.getSelectedIndex() > 0) // { // pref.put(JRXlsAbstractExporter.PROPERTY_FREEZE_ROW_EDGE, (String) ((Tag)jComboBoxRowEdge.getSelectedItem()).getValue()); // System.out.println("Saved " + (String) ((Tag)jComboBoxRowEdge.getSelectedItem()).getValue()); // } // else // { // pref.remove(JRXlsAbstractExporter.PROPERTY_FREEZE_ROW_EDGE); // } }
private void configureXlsExporter(JRExporter exporter, SimpleJasperReportsContext context) { Preferences pref = IReportManager.getPreferences(); JRPropertiesUtil jrPropUtils = JRPropertiesUtil.getInstance(context); context.setValue( JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE , pref.getBoolean(JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE, jrPropUtils.getBooleanProperty(JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE))); String password = pref.get(JExcelApiExporterParameter.PROPERTY_PASSWORD, jrPropUtils.getProperty(JExcelApiExporterParameter.PROPERTY_PASSWORD)); if (password != null && password.length() > 0) { context.setValue( JExcelApiExporterParameter.PROPERTY_PASSWORD ,password); } context.setValue( JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN))); System.out.println("Setting detect cell type to:" + pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE))); System.out.println("Setting detect cell type to ("+JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE+"):" + jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE)); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BORDER , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BORDER, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BORDER))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BACKGROUND , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BACKGROUND, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BACKGROUND))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_IGNORE_GRAPHICS , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_GRAPHICS, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_GRAPHICS))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_IMAGE_BORDER_FIX_ENABLED , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IMAGE_BORDER_FIX_ENABLED, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_IMAGE_BORDER_FIX_ENABLED))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_ONE_PAGE_PER_SHEET , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_ONE_PAGE_PER_SHEET, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_ONE_PAGE_PER_SHEET))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_ROWS , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_ROWS))); context.setValue( JRXlsAbstractExporterParameter.PROPERTY_WHITE_PAGE_BACKGROUND , pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_WHITE_PAGE_BACKGROUND, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_WHITE_PAGE_BACKGROUND))); int maxRowsPerSheet = pref.getInt(JRXlsAbstractExporterParameter.PROPERTY_MAXIMUM_ROWS_PER_SHEET, jrPropUtils.getIntegerProperty(JRXlsAbstractExporterParameter.PROPERTY_MAXIMUM_ROWS_PER_SHEET)); if (maxRowsPerSheet > 0) { context.setValue( JRXlsAbstractExporterParameter.PROPERTY_MAXIMUM_ROWS_PER_SHEET, ""+ maxRowsPerSheet); } if (pref.getBoolean(JRPropertiesUtil.PROPERTY_PREFIX + "export.xls.useSheetNames", false)) { String sheetNames = pref.get(JRPropertiesUtil.PROPERTY_PREFIX + "export.xls.sheetNames", ""); exporter.setParameter( JRXlsAbstractExporterParameter.SHEET_NAMES, sheetNames.split("\n")); } // Add freeze options... String col = pref.get(JRXlsExporter.PROPERTY_FREEZE_COLUMN, jrPropUtils.getProperty(JRXlsExporter.PROPERTY_FREEZE_COLUMN)); if (col != null && col.length() > 0) { context.setValue( JRXlsExporter.PROPERTY_FREEZE_COLUMN, ""+col); } // String columnEdge = pref.get(JRXlsExporter.PROPERTY_FREEZE_COLUMN_EDGE, null); // if (columnEdge != null && columnEdge.length() > 0) // { // context.setValue( JRXlsExporter.PROPERTY_FREEZE_COLUMN_EDGE, columnEdge); // } // Add freeze options... int row = pref.getInt(JRXlsExporter.PROPERTY_FREEZE_ROW, jrPropUtils.getIntegerProperty(JRXlsExporter.PROPERTY_FREEZE_ROW,0)); if (row > 0) { context.setValue( JRXlsExporter.PROPERTY_FREEZE_ROW, ""+row); } // String rowEdge = pref.get( JRXlsExporter.PROPERTY_FREEZE_ROW_EDGE, null); // if (rowEdge != null && rowEdge.length() > 0) // { // context.setValue( JRXlsExporter.PROPERTY_FREEZE_ROW_EDGE, pref.get(JRXlsExporter.PROPERTY_FREEZE_ROW_EDGE, rowEdge)); // } }
public void load() { setInit(true); Preferences pref = IReportManager.getPreferences(); JRPropertiesUtil jrPropUtils = IRLocalJasperReportsContext.getUtilities(); jCheckBoxCreateCustomPalette.setSelected( pref.getBoolean(JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE, jrPropUtils.getBooleanProperty(JExcelApiExporterParameter.PROPERTY_CREATE_CUSTOM_PALETTE))); jTextFieldPassword.setText( pref.get(JExcelApiExporterParameter.PROPERTY_PASSWORD, jrPropUtils.getProperty(JExcelApiExporterParameter.PROPERTY_PASSWORD))); jCheckBoxCollapseRowSpan.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN))); jCheckBoxAutoDetectCellType.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_DETECT_CELL_TYPE, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN))); jCheckBoxFontSizeFixEnabled.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN))); jCheckBoxIgnoreCellBorder.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BORDER, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_COLLAPSE_ROW_SPAN))); jCheckBoxIgnoreCellBackground.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BACKGROUND, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_CELL_BACKGROUND))); jCheckBoxIgnoreGraphics.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_GRAPHICS, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_IGNORE_GRAPHICS))); jCheckBoxImageBorderFixEnabled.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_IMAGE_BORDER_FIX_ENABLED, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_IMAGE_BORDER_FIX_ENABLED))); jCheckBoxOnePagePerSheet.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_ONE_PAGE_PER_SHEET, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_ONE_PAGE_PER_SHEET))); jCheckBoxRemoveEmptySpaceBetweenColumns.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS))); jCheckBoxRemoveEmptySpaceBetweenRows.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_ROWS))); jCheckBoxWhitePageBackground.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_WHITE_PAGE_BACKGROUND, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_WHITE_PAGE_BACKGROUND))); SpinnerNumberModel model = (SpinnerNumberModel)jSpinnerMaximumRowsPerSheet.getModel(); model.setValue( pref.getInt(JRXlsAbstractExporterParameter.PROPERTY_MAXIMUM_ROWS_PER_SHEET, jrPropUtils.getIntegerProperty(JRXlsAbstractExporterParameter.PROPERTY_MAXIMUM_ROWS_PER_SHEET))); jCheckBoxUseSheetNames.setSelected( pref.getBoolean(JRPropertiesUtil.PROPERTY_PREFIX + "export.xls.useSheetNames", false)); // freeze pane... String columnIndex = jrPropUtils.getProperty(JRXlsAbstractExporter.PROPERTY_FREEZE_COLUMN); if (columnIndex == null) columnIndex = ""; jTextFieldColumn.setText( pref.get(JRXlsAbstractExporter.PROPERTY_FREEZE_COLUMN, columnIndex)); ((SpinnerNumberModel)jSpinnerFreezeRow.getModel()).setValue( pref.getInt(JRXlsAbstractExporter.PROPERTY_FREEZE_ROW, jrPropUtils.getIntegerProperty(JRXlsAbstractExporter.PROPERTY_FREEZE_ROW,0))); //com.jaspersoft.ireport.designer.utils.Misc.setComboBoxTag(true, pref.get(JRXlsAbstractExporter.PROPERTY_FREEZE_COLUMN_EDGE, jrPropUtils.getProperty(JRXlsAbstractExporter.PROPERTY_FREEZE_COLUMN_EDGE)), jComboBoxColumnEdge); //com.jaspersoft.ireport.designer.utils.Misc.setComboBoxTag(true, pref.get(JRXlsAbstractExporter.PROPERTY_FREEZE_ROW_EDGE, jrPropUtils.getProperty(JRXlsAbstractExporter.PROPERTY_FREEZE_ROW_EDGE)), jComboBoxRowEdge); jTextArea1.setText(pref.get(JRPropertiesUtil.PROPERTY_PREFIX + "export.xls.sheetNames", jrPropUtils.getProperty(JRXlsAbstractExporterParameter.PROPERTY_MAXIMUM_ROWS_PER_SHEET))); jCheckBoxFixFontSize.setSelected( pref.getBoolean(JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED, jrPropUtils.getBooleanProperty(JRXlsAbstractExporterParameter.PROPERTY_FONT_SIZE_FIX_ENABLED))); setInit(false); }