Search in sources :

Example 6 with JRXlsExporter

use of net.sf.jasperreports.engine.export.JRXlsExporter in project netxms by netxms.

the class FileSystemReportManager method renderResult.

@SuppressWarnings("rawtypes")
@Override
public File renderResult(UUID reportId, UUID jobId, ReportRenderFormat format) {
    final File outputDirectory = getOutputDirectory(reportId);
    final File dataFile = new File(outputDirectory, jobId.toString() + ".jrprint");
    File outputFile = new File(outputDirectory, jobId.toString() + "." + System.currentTimeMillis() + ".render");
    JRAbstractExporter exporter = null;
    switch(format) {
        case PDF:
            exporter = new JRPdfExporter();
            break;
        case XLS:
            exporter = new JRXlsExporter();
            final JasperReport jasperReport = loadReport(reportId);
            if (jasperReport != null) {
                exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES, new String[] { prepareXlsSheetName(jasperReport.getName()) });
            }
            exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, false);
            exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, false);
            // Arrange cell spacing and remove paging gaps
            exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, true);
            exporter.setParameter(JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN, true);
            exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, true);
            exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, false);
            exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, true);
            // Arrange graphics
            exporter.setParameter(JRXlsExporterParameter.IS_IMAGE_BORDER_FIX_ENABLED, true);
            exporter.setParameter(JRXlsExporterParameter.IS_FONT_SIZE_FIX_ENABLED, true);
            exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, false);
            break;
        default:
            break;
    }
    exporter.setParameter(JRExporterParameter.INPUT_FILE, dataFile);
    try {
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, new FileOutputStream(outputFile));
        exporter.exportReport();
    } catch (Exception e) {
        log.error("Failed to render report", e);
        outputFile.delete();
        outputFile = null;
    }
    return outputFile;
}
Also used : JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) JRPdfExporter(net.sf.jasperreports.engine.export.JRPdfExporter) JarFile(java.util.jar.JarFile) SQLException(java.sql.SQLException)

Example 7 with JRXlsExporter

use of net.sf.jasperreports.engine.export.JRXlsExporter in project tutorials by eugenp.

the class JasperReportsXlsExporter method export.

/**
 * Generates a ByteArrayOutputStream from the provided JasperReport using
 * the {@link JRXlsExporter}. After that, the generated bytes array is
 * written in the {@link HttpServletResponse}
 *
 * @param jp
 *            The generated JasperReport.
 * @param fileName
 *            The fileName of the exported JasperReport
 * @param response
 *            The HttpServletResponse where generated report has been
 *            written
 * @throws JRException
 *             during JasperReport export.
 * @throws IOException
 *             when writes the ByteArrayOutputStream into the
 *             HttpServletResponse
 */
@Override
public void export(JasperPrint jp, String fileName, HttpServletResponse response) throws JRException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Create a JRXlsExporter instance
    JRXlsExporter exporter = new JRXlsExporter();
    // Here we assign the parameters jp and baos to the exporter
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
    // Excel specific parameters
    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);
    // Retrieve the exported report in PDF format
    exporter.exportReport();
    // Specifies the response header
    response.setHeader("Content-Disposition", "inline; filename=" + fileName);
    // Make sure to set the correct content type
    // Each format has its own content type
    response.setContentType("application/vnd.ms-excel");
    response.setContentLength(baos.size());
    // Retrieve the output stream
    ServletOutputStream outputStream = response.getOutputStream();
    // Write to the output stream
    baos.writeTo(outputStream);
    // Flush the stream
    outputStream.flush();
}
Also used : JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) ServletOutputStream(javax.servlet.ServletOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 8 with JRXlsExporter

use of net.sf.jasperreports.engine.export.JRXlsExporter in project dhis2-core by dhis2.

the class JRExportUtils method export.

/**
 * Export the provided JasperPrint the format given by type.
 *
 * @param type the type to export to. XLS, PDF and HTML are supported.
 * @param out the OutputStream to export to.
 * @param jasperPrint the JasperPrint to export.
 * @throws JRException on export failure.
 */
public static void export(String type, OutputStream out, JasperPrint jasperPrint) throws JRException {
    if (TYPE_XLS.equals(type)) {
        SimpleXlsReportConfiguration config = new SimpleXlsReportConfiguration();
        config.setDetectCellType(true);
        config.setRemoveEmptySpaceBetweenRows(true);
        config.setRemoveEmptySpaceBetweenRows(true);
        config.setCollapseRowSpan(true);
        config.setWhitePageBackground(false);
        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
        exporter.setConfiguration(config);
        exporter.exportReport();
    } else if (TYPE_PDF.equals(type)) {
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
        exporter.exportReport();
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JRPdfExporter(net.sf.jasperreports.engine.export.JRPdfExporter)

Aggregations

JRXlsExporter (net.sf.jasperreports.engine.export.JRXlsExporter)8 SimpleExporterInput (net.sf.jasperreports.export.SimpleExporterInput)5 SimpleOutputStreamExporterOutput (net.sf.jasperreports.export.SimpleOutputStreamExporterOutput)5 SimpleXlsReportConfiguration (net.sf.jasperreports.export.SimpleXlsReportConfiguration)5 FileOutputStream (java.io.FileOutputStream)4 HashMap (java.util.HashMap)3 JasperPrint (net.sf.jasperreports.engine.JasperPrint)3 JasperReport (net.sf.jasperreports.engine.JasperReport)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 JRMapCollectionDataSource (net.sf.jasperreports.engine.data.JRMapCollectionDataSource)2 JRPdfExporter (net.sf.jasperreports.engine.export.JRPdfExporter)2 JRRtfExporter (net.sf.jasperreports.engine.export.JRRtfExporter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 JarFile (java.util.jar.JarFile)1 Preferences (java.util.prefs.Preferences)1 FXML (javafx.fxml.FXML)1