The code below gets a byte[]
result, which works for PDF and XLSX. For HTML, an exception is raised.
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
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).
How do you export a report in various formats, including HTML?
Navigate to http://[localhost]:8480/jasperserver-pro/. Log into Standalone as sysadmin. Right-click the report you wish to export, then click Export.
At JasperReports levelIn <jasperserver-root>/WEB-IN/classes you can find the file jasperreports. properties. This is the file that you need to modify to set some properties to the value you want for all reports running in JasperReports Server. Similarly you can set some properties at iReport level.
For HTML and other formats:
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);
Dynamic report implementation for all types of format
Maven depeendecies to be included is as below
<!-- 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 :
<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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With