Экспорт JasperReports в формате HTML
код ниже получает 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 ответов
для 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);
реализация динамического отчета для всех типов формата
Maven depeendecies должны быть включены, как показано ниже
<!-- 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();
попробуй такое
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();