java – 以HTML格式导出JasperReports

前端之家收集整理的这篇文章主要介绍了java – 以HTML格式导出JasperReports前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的代码得到一个byte []结果,适用于PDF和XLSX.对于 HTML,引发了异常.
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();

HTML的例外是exporter.exportReport();这条线说

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)

v6.0和v5.6的错误相同.这曾经在v5.0中工作(某些类在v5.6中已弃用).

如何以各种格式导出报告,包括HTML?

解决方法

对于HTML和其他格式:
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();
}

用它来调用

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

猜你在找的Java相关文章