1 16 17 package org.springframework.ui.jasperreports; 18 19 import java.io.OutputStream ; 20 import java.io.Writer ; 21 import java.util.Collection ; 22 import java.util.Map ; 23 24 import net.sf.jasperreports.engine.JRDataSource; 25 import net.sf.jasperreports.engine.JRDataSourceProvider; 26 import net.sf.jasperreports.engine.JRException; 27 import net.sf.jasperreports.engine.JRExporter; 28 import net.sf.jasperreports.engine.JRExporterParameter; 29 import net.sf.jasperreports.engine.JasperFillManager; 30 import net.sf.jasperreports.engine.JasperPrint; 31 import net.sf.jasperreports.engine.JasperReport; 32 import net.sf.jasperreports.engine.data.JRBeanArrayDataSource; 33 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; 34 import net.sf.jasperreports.engine.export.JRCsvExporter; 35 import net.sf.jasperreports.engine.export.JRHtmlExporter; 36 import net.sf.jasperreports.engine.export.JRPdfExporter; 37 import net.sf.jasperreports.engine.export.JRXlsExporter; 38 39 47 public abstract class JasperReportsUtils { 48 49 62 public static JRDataSource convertReportData(Object value) throws IllegalArgumentException { 63 if (value instanceof JRDataSource) { 64 return (JRDataSource) value; 65 } 66 else if (value instanceof Collection ) { 67 return new JRBeanCollectionDataSource((Collection ) value); 68 } 69 else if (value instanceof Object []) { 70 return new JRBeanArrayDataSource((Object []) value); 71 } 72 else if (value instanceof JRDataSourceProvider) { 73 return null; 74 } 75 else { 76 throw new IllegalArgumentException ("Value [" + value + "] cannot be converted to a JRDataSource"); 77 } 78 } 79 80 91 public static void render(JRExporter exporter, JasperPrint print, Writer writer) 92 throws JRException { 93 exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 94 exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, writer); 95 exporter.exportReport(); 96 } 97 98 109 public static void render(JRExporter exporter, JasperPrint print, OutputStream outputStream) 110 throws JRException { 111 exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 112 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 113 exporter.exportReport(); 114 } 115 116 128 public static void renderAsCsv(JasperReport report, Map parameters, Object reportData, Writer writer) 129 throws JRException { 130 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 131 render(new JRCsvExporter(), print, writer); 132 } 133 134 147 public static void renderAsCsv(JasperReport report, Map parameters, Object reportData, Writer writer, 148 Map exporterParameters) throws JRException { 149 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 150 JRCsvExporter exporter = new JRCsvExporter(); 151 exporter.setParameters(exporterParameters); 152 render(exporter, print, writer); 153 } 154 155 167 public static void renderAsHtml(JasperReport report, Map parameters, Object reportData, Writer writer) 168 throws JRException { 169 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 170 render(new JRHtmlExporter(), print, writer); 171 } 172 173 186 public static void renderAsHtml(JasperReport report, Map parameters, Object reportData, Writer writer, 187 Map exporterParameters) throws JRException { 188 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 189 JRHtmlExporter exporter = new JRHtmlExporter(); 190 exporter.setParameters(exporterParameters); 191 render(exporter, print, writer); 192 } 193 194 206 public static void renderAsPdf(JasperReport report, Map parameters, Object reportData, OutputStream stream) 207 throws JRException { 208 209 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 210 render(new JRPdfExporter(), print, stream); 211 } 212 213 226 public static void renderAsPdf(JasperReport report, Map parameters, Object reportData, OutputStream stream, 227 Map exporterParameters) throws JRException { 228 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 229 JRPdfExporter exporter = new JRPdfExporter(); 230 exporter.setParameters(exporterParameters); 231 render(exporter, print, stream); 232 } 233 234 246 public static void renderAsXls(JasperReport report, Map parameters, Object reportData, OutputStream stream) 247 throws JRException { 248 249 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 250 render(new JRXlsExporter(), print, stream); 251 } 252 253 266 public static void renderAsXls(JasperReport report, Map parameters, Object reportData, OutputStream stream, 267 Map exporterParameters) throws JRException { 268 269 JasperPrint print = JasperFillManager.fillReport(report, parameters, convertReportData(reportData)); 270 JRXlsExporter exporter = new JRXlsExporter(); 271 exporter.setParameters(exporterParameters); 272 render(exporter, print, stream); 273 } 274 } 275 | Popular Tags |