以HTML格式导出JasperReports

时间:2015-01-05 12:32:23

标签: java jasper-reports

以下代码获得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?

3 个答案:

答案 0 :(得分:12)

对于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);

答案 1 :(得分:0)

所有格式的动态报告实施

要包括的Maven不符合项如下

<!-- dynamic/jasper reports -->
    <dependency>
        <groupId>net.sourceforge.dynamicreports</groupId>
        <artifactId>dynamicreports-core</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.birt.runtime.3_7_1</groupId>
        <artifactId>com.lowagie.text</artifactId>
        <version>2.1.7</version>
    </dependency>

XHTML代码:

<h:commandLink id="csv" onclick="PF('data').hide();"
    action="#{dashboardInfoBean.downloadCsv}">
    <h:graphicImage name="images/img_trans.gif"
        styleClass="ico csvImg" />
</h:commandLink>
<h:commandLink id="pdf" onclick="PF('data').hide();"
    action="#{dashboardInfoBean.downloadPdf}">
    <h:graphicImage name="images/img_trans.gif"
        styleClass="ico pdfImg" />
</h:commandLink>
<h:commandLink id="excel" onclick="PF('data').hide();"
    action="#{dashboardInfoBean.downloadExcel}">
    <h:graphicImage name="images/img_trans.gif"
        styleClass="ico xlsImg" />
</h:commandLink>
<h:commandLink id="xml" onclick="PF('data').hide();"
    action="#{dashboardInfoBean.downloadXml}">
    <h:graphicImage name="images/img_trans.gif"
        styleClass="ico xmlImg" />
</h:commandLink>
<h:commandLink id="jasper" onclick="PF('data').hide();"
    action="#{dashboardInfoBean.downloadJasperReport}">
    <h:graphicImage name="images/img_trans.gif"
        styleClass="ico xmlImg" />
</h:commandLink>        

Java代码:

 //set datasource for creating the report
        report.setDataSource(dataSource);
        JasperPrint jasperPrint = report.toJasperPrint();
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
        ServletOutputStream servletOutputStream = response.getOutputStream();

        if(downloadFormat.equalsIgnoreCase("PDF")){
            response.setContentType("application/pdf");  
            response.addHeader("Content-disposition", "attachment; filename=report.pdf");
            JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
        } else if(downloadFormat.equalsIgnoreCase("XML")){
            //response.setContentType("application/pdf");  
            response.addHeader("Content-disposition", "attachment; filename=report.xml");
            JasperExportManager.exportReportToXmlStream(jasperPrint, servletOutputStream);
        } else if(downloadFormat.equalsIgnoreCase("CSV")){
            response.setContentType("text/plain");  
            response.addHeader("Content-disposition", "attachment; filename=report.csv");
            JRCsvExporter exporter = new JRCsvExporter();  
            exporter.setParameter(JRCsvExporterParameter.JASPER_PRINT,  
                    jasperPrint);  
            exporter.setParameter(JRCsvExporterParameter.OUTPUT_STREAM,  
                    servletOutputStream);  
            exporter.setParameter(JRExporterParameter.IGNORE_PAGE_MARGINS,  
                    Boolean.TRUE);  
            exporter.exportReport();  
        } else if(downloadFormat.equalsIgnoreCase("XLS")){
            response.setContentType("application/vnd.ms-excel");  
            response.addHeader("Content-disposition", "attachment; filename=report.xls");
            JExcelApiExporter exporterXLS = new JExcelApiExporter();  
            exporterXLS.setParameter(  
                JRXlsExporterParameter.JASPER_PRINT,  
                jasperPrint);  
            exporterXLS.setParameter(  
                JRXlsExporterParameter.IS_DETECT_CELL_TYPE,  
                Boolean.TRUE);  
            exporterXLS.setParameter(  
                JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,  
                Boolean.FALSE);  
            exporterXLS.setParameter(  
                JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,  
                Boolean.TRUE);  
            exporterXLS.setParameter(  
                JRXlsExporterParameter  
                    .IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS,  
                Boolean.TRUE);  
            // exporterXLS.setParameter(  
            // JRXlsExporterParameter.IS_IGNORE_CELL_BORDER,  
            // Boolean.TRUE);  
            exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,  
                    servletOutputStream);  
            exporterXLS.exportReport();  
        }
        FacesContext.getCurrentInstance().responseComplete();

答案 2 :(得分:-3)

试试这个

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();
            exporter.setExporterOutput(new SimpleWriterExporterOutput(out));
            break;
        case CSV:
            exporter = new JRCsvExporter();
            exporter.setExporterOutput(new SimpleWriterExporterOutput(out));
            break;
        case XLSX:
            exporter = new JRXlsxExporter();
            exporter.setExporterOutput(new SimpleWriterExporterOutput(out));
            break;
        case HTML:
             exporter = new HtmlExporter();
            exporter.setExporterOutput(new SimpleWriterExporterOutput(out));
            break;
        default:
            throw new ReportException("Unknown export format");
        }
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.exportReport();