java 以 HTML 格式导出 JasperReports

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27779612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:22:30  来源:igfitidea点击:

Export JasperReports in HTML format

javajasper-reports

提问by Alireza Fattahi

The code below gets a byte[]result, which works for PDF and XLSX. For HTML, an exception is raised.

下面的代码得到一个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();

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

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)

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).

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

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

您如何以各种格式(包括 HTML)导出报告?

回答by Alireza Fattahi

For HTML and other formats:

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

Call it using:

调用它使用:

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

回答by Sanketik Gote

Dynamic report implementation for all types of format

所有类型格式的动态报告实现

Maven depeendecies to be included is as below

要包含的 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 Code :

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 Code :

代码:

 //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();

回答by Sasank

Try this

试试这个

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();