使用示例:
public class ExporterReport {
private byte[] report = null;
private String reportName = "myreport";
protected Collection<? extends Object> getData() {
//TODO: return your collection
}
protected String getReportPath() {
return "/report/myreport.jasper";
}
public final void buildReportXLS() {
Map<String, Object> params = new HashMap<String, Object>();
try {
params.put("text", "sametext");
String reportUrl = getReportPath();
Collection<? extends Object> collection = getData();
if (!collection.isEmpty()) {
URL urlJasper = FacesContext.getCurrentInstance().getExternalContext().getResource(reportUrl);
JRDataSource data = new JRBeanCollectionDataSource(collection);
JasperPrint jasperPrint = null;
try {
jasperPrint = JasperFillManager.fillReport(urlJasper.getPath(), params, data);
if (jasperPrint != null) {
/**
* 1- export to PDF sample
*/
//JasperExportManager.exportReportToPdfFile(jasperPrint, "C://sample_report.pdf");
/**
* 2- export to HTML Sample
*/
//JasperExportManager.exportReportToHtmlFile(jasperPrint,"C://sample_report.html");
/**
* 3- export to Excel sheet
*/
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS,Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE);
exporter.exportReport();
xlsReport.flush();
xlsReport.close();
this.setReport(xlsReport.toByteArray());
}
} catch (JRException e) {
e.printStackTrace();
}
if (log.isInfoEnabled()) {
log.info("Report build successful", reportName);
}
} catch (Exception e) {
log.error("has a error {0} in {1}: {2} [{3}]", e, reportName, new Date(), e.getMessage(), "information");
}
}
public void downloadReportXLS() {
if (this.getReport() != null) {
this.downloadFile(this.getReport(), reportName + ".xls");
}
}
private void downloadFile(byte[] bytes, String fileName) {
try {
String contentType = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
if (fileName.lastIndexOf('.') != -1) {
contentType = URLConnection.getFileNameMap().getContentTypeFor(fileName);
}
if (StringUtil.isStringEmpty(contentType)) {
contentType = "application/octet-stream";
}
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentLength(bytes.length);
ServletOutputStream os = response.getOutputStream();
os.write(bytes);
os.flush();
os.close();
facesContext.responseComplete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在您的 xhtml 中,您可以输入以下内容:
<h:commandButton value="Export to sheet" actionListener="#{exportReport.buildReportXLS()}" action="#{exportReport.downloadReportXLS()}" />