View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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  	 * Logger for this class
51  	 */
52  	private static final Log logger = LogFactory.getLog(ReportExporter.class);
53  
54  	/**
55  	 * The path to the file must exist.
56  	 * @param jp
57  	 * @param path
58  	 * @throws JRException
59  	 * @throws FileNotFoundException
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 //		JRXlsExporter exporter = new JRXlsExporter();
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 }