01: package dinamica;
02:
03: import java.io.ByteArrayOutputStream;
04: import javax.servlet.ServletOutputStream;
05: import org.apache.poi.hssf.usermodel.*;
06:
07: /**
08: * Base class to produce Excel output modules (hand made reports)
09: * based on the Apache POI v3 open source component.<br>
10: * This super class provides several common utility methods, including
11: * methods to retrieve images from URLs (local or remotes), which can be
12: * used to insert charts into the document by reusing a server-side chart Action,
13: * or to insert another dinamically generated image, like a BarCode. The method to retrieve
14: * via HTTP is session sensitive, meaning that it can reuse the same session ID, which is
15: * a requirement when accessing local resources that are session-sensitive, like chart Actions.
16: * <br><br>
17: * In order to reuse this class, you must override the method print().
18: * <br><br>
19: * (c) 2007 Martin Cordova<br>
20: * This code is released under the LGPL license<br>
21: * Dinamica Framework - http://www.martincordova.com
22: * @author Martin Cordova (dinamica@martincordova.com)
23: * */
24: public class AbstractExcelOutput extends GenericOutput {
25:
26: /**
27: * Create excel document using Apache POI, after assembling the document
28: * send it to the browser.
29: */
30: public void print(GenericTransaction data) throws Throwable {
31: //create excel document
32: HSSFWorkbook wb = createWorkbook(data);
33:
34: //store document into memory
35: ByteArrayOutputStream buf = new ByteArrayOutputStream();
36: wb.write(buf);
37:
38: //send output to browser
39: getResponse().setContentType("application/vnd.ms-excel");
40: getResponse().setHeader("Content-Disposition",
41: getAttachmentString());
42: getResponse().setContentLength(buf.size());
43: ServletOutputStream out = getResponse().getOutputStream();
44: buf.writeTo(out);
45: out.close();
46: }
47:
48: /**
49: * Sets the value of the Content-Disposition response header, by default
50: * it will be [attachment; filename="data.xls";], but can be overriden
51: * @return Content-Disposition response header value
52: */
53: String getAttachmentString() {
54: return "attachment; filename=\"data.xls\";";
55: }
56:
57: /**
58: * Create excel document as a workbook (Apache POI specific class). This method
59: * should be overriden by subclasses
60: * @return workbook ready to print via servlet output stream
61: * @throws Throwable
62: */
63: public HSSFWorkbook createWorkbook(GenericTransaction data)
64: throws Throwable {
65: HSSFWorkbook wb = new HSSFWorkbook();
66: return wb;
67: }
68:
69: }
|