View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/servlets/ExcelReportServlet.java,v 1.7 2005/11/30 20:31:18 hkollmann Exp $
3    * $Revision: 1.7 $
4    * $Date: 2005/11/30 20:31:18 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.servlets;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import org.dbforms.servlets.reports.AbstractLineReportServlet;
29  import org.dbforms.util.ParseUtil;
30  import org.dbforms.util.MessageResourcesInternal;
31  import org.dbforms.util.MessageResources;
32  import java.io.OutputStream;
33  import java.util.Calendar;
34  import java.util.Date;
35  
36  import org.apache.poi.hssf.usermodel.HSSFCell;
37  import org.apache.poi.hssf.usermodel.HSSFRow;
38  import org.apache.poi.hssf.usermodel.HSSFSheet;
39  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
40  
41  /***
42   * This servlet generates a Microsoft Excel xls file using POI. Data is read
43   * from the current dbForm, a Collection or a ResultSetVector The template file
44   * is in the reports directory with a .xr extension it consists of one line of
45   * text, which is list of comma separated field names usage: with a simple goto
46   * button: &lt;db:gotoButton destTable="web_parts" destination="
47   * /reports/Artikel"/&gt; or for one record: &lt;db:gotoButton
48   * destTable="web_parts" keyToDestPos="currentRow"
49   * destination="/reports/Artikel" /&gt; Servlet mapping must be set to handle
50   * all /reports by this servlet!!! &lt;servlet/&gt;
51   * &lt;servlet-name/&gt;startreport&lt;/servlet-name/&gt;
52   * &lt;display-name/&gt;startreport&lt;/display-name/&gt;
53   * &lt;servlet-class/&gt;org.dbforms.StartReportServlet&lt;/servlet-class/&gt;
54   * &lt;/servlet&gt; &lt;servlet-mapping/&gt;
55   * &lt;servlet-name/&gt;startreport&lt;/servlet-name/&gt;
56   * &lt;url-pattern/&gt;/reports/&lt;/url-pattern/&gt; &lt;/servlet-mapping&gt;
57   * Parameters filename=xyz.xls name the output file sheetname=first_page name
58   * the worksheet Support for grabbing data from a Collection or an existing
59   * ResultSetVector set session variable "jasper.input" to use a Collection
60   * object set session variable "jasper.rsv" to use a ResultSetVector object ex
61   * &ltc:set var="jasper.rsv" value="${rsv_xxxxx}" scope="session" /&gt Note:
62   * Setting column headings does not work.
63   * 
64   * @author Neal Katz
65   */
66  public class ExcelReportServlet extends AbstractLineReportServlet {
67  
68  	private static final String SHEETNAMEPARAM = "sheetname";
69  
70  	private HSSFWorkbook wb;
71  
72  	private HSSFSheet sheet;
73  
74  	private short rowCnt = 0;
75  
76  	protected String getMimeType() {
77  		return "application/msexcel";
78  	}
79  
80  	protected String getFileExtension() {
81  		return ".xls";
82  	}
83  
84  	protected void openStream(OutputStream out)  throws Exception  {
85  	}
86  
87  	protected void closeStream(OutputStream out) throws Exception {
88  		wb.write(out);
89  	}
90  
91  	
92  	protected void writeHeader(String[] header) throws Exception {
93  		HSSFRow row = sheet.createRow(rowCnt++);
94  		for (int i = 0; i < header.length; i++) {
95  			HSSFCell cell = row.createCell((short) i);
96  			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
97  			cell.setCellValue(header[i]);
98  		}
99  	}
100 
101 	protected void writeData(Object[] data) throws Exception {
102 		HSSFRow row = sheet.createRow( rowCnt++);
103 		for (int i = 0; i < data.length; i++) {
104 			if (data[i] != null) {
105 				// for null values we just skip the cell
106 				HSSFCell cell = row.createCell((short) i);
107 				if (data[i] instanceof Number) {
108 					cell.setCellValue(((Number) data[i]).doubleValue());
109 				} else if (data[i] instanceof Date) {
110 					cell.setCellValue((Date) data[i]);
111 				} else if (data[i] instanceof Calendar) {
112 					cell.setCellValue((Calendar) data[i]);
113 				} else {
114 					cell.setEncoding(HSSFCell.ENCODING_UTF_16);
115 					cell.setCellValue(data[i].toString());
116 				}
117 			}
118 		}
119 	}
120 
121 	protected void process(HttpServletRequest request,
122 			HttpServletResponse response) {
123 		String sheetname = ParseUtil.getParameter(request, SHEETNAMEPARAM,
124 				MessageResourcesInternal.getMessage("dbforms.new_worksheet",
125 						MessageResources.getLocale(request)));
126 		wb = new HSSFWorkbook();
127 		sheet = wb.createSheet(sheetname);
128 		rowCnt = 0;
129 		super.process(request, response);
130 	}
131 
132 }