1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package ar.com.fdvs.dj.test;
31
32 import net.sf.jasperreports.engine.JRException;
33 import net.sf.jasperreports.engine.JRExporterParameter;
34 import net.sf.jasperreports.engine.JasperPrint;
35 import net.sf.jasperreports.engine.export.*;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import java.io.File;
40 import java.io.FileNotFoundException;
41 import java.io.FileOutputStream;
42
43 public class ReportExporter {
44
45
46
47 private static final Log logger = LogFactory.getLog(ReportExporter.class);
48
49
50
51
52
53
54
55
56 public static void exportReport(JasperPrint jp, String path) throws JRException, FileNotFoundException{
57 logger.debug("Exporing report to: " + path);
58 JRPdfExporter exporter = new JRPdfExporter();
59
60 File outputFile = new File(path);
61 File parentFile = outputFile.getParentFile();
62 if (parentFile != null)
63 parentFile.mkdirs();
64 FileOutputStream fos = new FileOutputStream(outputFile);
65
66 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
67 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
68
69 exporter.exportReport();
70
71 logger.debug("Report exported: " + path);
72 }
73
74 public static void exportReportXls(JasperPrint jp, String path) throws JRException, FileNotFoundException{
75 JRXlsExporter exporter = new JRXlsExporter();
76
77 File outputFile = new File(path);
78 File parentFile = outputFile.getParentFile();
79 if (parentFile != null)
80 parentFile.mkdirs();
81 FileOutputStream fos = new FileOutputStream(outputFile);
82
83 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
84 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
85 exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);
86 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
87 exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.FALSE);
88
89 exporter.exportReport();
90
91 logger.debug("XLS Report exported: " + path);
92 }
93
94 public static void exportReportHtml(JasperPrint jp, String path) throws JRException, FileNotFoundException{
95 JRHtmlExporter exporter = new JRHtmlExporter();
96
97 File outputFile = new File(path);
98 File parentFile = outputFile.getParentFile();
99 if (parentFile != null)
100 parentFile.mkdirs();
101 FileOutputStream fos = new FileOutputStream(outputFile);
102
103 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
104 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
105 exporter.exportReport();
106
107 logger.debug("HTML Report exported: " + path);
108 }
109
110 public static void exportReportPlainXls(JasperPrint jp, String path) throws JRException, FileNotFoundException{
111
112 JExcelApiExporter exporter = new JExcelApiExporter();
113
114 File outputFile = new File(path);
115 File parentFile = outputFile.getParentFile();
116 if (parentFile != null)
117 parentFile.mkdirs();
118 FileOutputStream fos = new FileOutputStream(outputFile);
119
120 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
121 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
122 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
123 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
124 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
125 exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
126
127
128 exporter.exportReport();
129
130 logger.debug("Report exported: " + path);
131
132 }
133
134 }