Alireza Fattahi Alireza Fattahi - 5 months ago 78
Java Question

Export JasperReports in HTML format

The code below gets a

byte[]
result, which works for PDF and XLSX. For HTML, an exception is raised.

JasperPrint jasperPrint = JasperFillManager.fillReport(report,
params, dataSource != null ? new JRMapArrayDataSource(
dataSource) : new JREmptyDataSource());

ByteArrayOutputStream out = new ByteArrayOutputStream();
@SuppressWarnings("rawtypes")
Exporter exporter;
switch (format) {
case PDF:
exporter = new JRPdfExporter();
break;
case XLSX:
exporter = new JRXlsxExporter();
break;
case HTML:
exporter = new HtmlExporter();
break;
default:
throw new ReportException("Unknown export format");
}
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.exportReport();
return out.toByteArray();


The exception for HTML is at
exporter.exportReport();
line which says

java.lang.ClassCastException:

net.sf.jasperreports.export.SimpleOutputStreamExporterOutput cannot be cast to net.sf.jasperreports.export.HtmlExporterOutput
at net.sf.jasperreports.engine.export.HtmlExporter.exportReport(HtmlExporter.java:232)


The error is the same for v6.0 and v5.6. This used to work in v5.0 (some of the classes were deprecated in v5.6).

How do you export a report in various formats, including HTML?

Answer

For HTML and other formats:

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXmlExporter;
import net.sf.jasperreports.export.Exporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

public byte[] export(final JasperPrint print) throws JRException {
    final Exporter exporter;
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    boolean html = false;

    switch (getReportFormat()) {
        case HTML:
            exporter = new HtmlExporter();
            exporter.setExporterOutput(new SimpleHtmlExporterOutput(out));
            html = true;
            break;

        case CSV:
            exporter = new JRCsvExporter();
            break;

        case XML:
            exporter = new JRXmlExporter();
            break;

        case XLSX:
            exporter = new JRXlsxExporter();
            break;

        case PDF:
            exporter = new JRPdfExporter();
            break;

        default:
            throw new JRException("Unknown report format: " + getReportFormat().toString());
    }

    if (!html) {
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
    }

    exporter.setExporterInput(new SimpleExporterInput(print));
    exporter.exportReport();

    return out.toByteArray();
}

Call it using:

JasperPrint print = JasperFillManager.fillReport(report, parameters, dataSource);
byte report[] = export(print);