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 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  	 * Logger for this class
46  	 */
47  	private static final Log logger = LogFactory.getLog(ReportExporter.class);
48  
49  	/**
50  	 * The path to the file must exist.
51  	 * @param jp
52  	 * @param path
53  	 * @throws JRException
54  	 * @throws FileNotFoundException
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 //		JRXlsExporter exporter = new JRXlsExporter();
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 }