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 java.io.File;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35
36 import net.sf.jasperreports.engine.JRException;
37 import net.sf.jasperreports.engine.JRExporterParameter;
38 import net.sf.jasperreports.engine.JasperPrint;
39 import net.sf.jasperreports.engine.export.JExcelApiExporter;
40 import net.sf.jasperreports.engine.export.JRHtmlExporter;
41 import net.sf.jasperreports.engine.export.JRPdfExporter;
42 import net.sf.jasperreports.engine.export.JRXlsExporter;
43 import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48 public class ReportExporter {
49
50
51
52 private static final Log logger = LogFactory.getLog(ReportExporter.class);
53
54
55
56
57
58
59
60
61 public static void exportReport(JasperPrint jp, String path) throws JRException, FileNotFoundException{
62 logger.debug("Exporing report to: " + path);
63 JRPdfExporter exporter = new JRPdfExporter();
64
65 File outputFile = new File(path);
66 File parentFile = outputFile.getParentFile();
67 if (parentFile != null)
68 parentFile.mkdirs();
69 FileOutputStream fos = new FileOutputStream(outputFile);
70
71 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
72 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
73
74 exporter.exportReport();
75
76 logger.debug("Report exported: " + path);
77 }
78
79 public static void exportReportXls(JasperPrint jp, String path) throws JRException, FileNotFoundException{
80 JRXlsExporter exporter = new JRXlsExporter();
81
82 File outputFile = new File(path);
83 File parentFile = outputFile.getParentFile();
84 if (parentFile != null)
85 parentFile.mkdirs();
86 FileOutputStream fos = new FileOutputStream(outputFile);
87
88 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
89 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
90 exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,Boolean.TRUE);
91 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
92 exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, Boolean.FALSE);
93
94 exporter.exportReport();
95
96 logger.debug("XLS Report exported: " + path);
97 }
98
99 public static void exportReportHtml(JasperPrint jp, String path) throws JRException, FileNotFoundException{
100 JRHtmlExporter exporter = new JRHtmlExporter();
101
102 File outputFile = new File(path);
103 File parentFile = outputFile.getParentFile();
104 if (parentFile != null)
105 parentFile.mkdirs();
106 FileOutputStream fos = new FileOutputStream(outputFile);
107
108 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
109 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
110 exporter.exportReport();
111
112 logger.debug("HTML Report exported: " + path);
113 }
114
115 public static void exportReportPlainXls(JasperPrint jp, String path) throws JRException, FileNotFoundException{
116
117 JExcelApiExporter exporter = new JExcelApiExporter();
118
119 File outputFile = new File(path);
120 File parentFile = outputFile.getParentFile();
121 if (parentFile != null)
122 parentFile.mkdirs();
123 FileOutputStream fos = new FileOutputStream(outputFile);
124
125 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
126 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
127 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
128 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
129 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
130 exporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
131
132
133 exporter.exportReport();
134
135 logger.debug("Report exported: " + path);
136
137 }
138
139 }