Java – export jasperreports in HTML format
•
Java
The following code gets a byte [] result, which is applicable to PDF and xlsx An exception was thrown for 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 to HTML is exporter exportReport(); This line 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)
v6. 0 and V5 The error of 6 is the same This was in V5 0 (some classes are deprecated in V5.6)
How do I export reports in various formats, including HTML?
Solution
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(); }
Use it to call:
JasperPrint print = JasperFillManager.fillReport(report,parameters,dataSource); byte report[] = export(print);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码