gpt4 book ai didi

java - 以 HTML 格式导出 JasperReports

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:42 24 4
gpt4 key购买 nike

下面的代码得到一个 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 的异常(exception)是 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 - 以 HTML 格式导出 JasperReports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27779612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com