View Javadoc

1   /**
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
12  
13  package org.displaytag.export.excel;
14  
15  import java.io.IOException;
16  import java.io.OutputStream;
17  
18  import javax.servlet.jsp.JspException;
19  
20  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21  import org.displaytag.Messages;
22  import org.displaytag.exception.BaseNestableJspTagException;
23  import org.displaytag.exception.SeverityEnum;
24  import org.displaytag.export.BinaryExportView;
25  import org.displaytag.model.TableModel;
26  import org.displaytag.render.HssfTableWriter;
27  
28  /**
29   * Excel exporter using POI.
30   * 
31   * @author Jorge L. Barroso
32   * @version $Revision$ ($Author$)
33   */
34  public class DefaultHssfExportView implements BinaryExportView
35  {
36      /**
37       * TableModel to render.
38       */
39      private TableModel model;
40      
41      /**
42       * @see org.displaytag.export.BinaryExportView#doExport(java.io.OutputStream)
43       */
44      public void doExport(OutputStream out) throws IOException, JspException
45      {
46          try
47          {
48              HSSFWorkbook wb = new HSSFWorkbook();
49              new HssfTableWriter(wb).writeTable(this.model, "-1");
50              wb.write(out);
51          }
52          catch (Exception e)
53          {
54              throw new HssfGenerationException(e);
55          }
56      }
57  
58      /** 
59       * @see org.displaytag.export.ExportView#setParameters(org.displaytag.model.TableModel, boolean, boolean, boolean)
60       */
61      public void setParameters(TableModel model, boolean exportFullList,
62              boolean includeHeader, boolean decorateValues)
63      {
64          this.model = model;
65      }
66  
67      /**
68       * @see org.displaytag.export.BaseExportView#getMimeType()
69       * @return "application/vnd.ms-excel"
70       */
71      public String getMimeType()
72      {
73          return "application/vnd.ms-excel"; //$NON-NLS-1$
74      }
75      
76      /**
77       * Wraps POI-generated exceptions.
78       * @author Fabrizio Giustina
79       * @version $Revision$ ($Author$)
80       */
81      static class HssfGenerationException extends BaseNestableJspTagException
82      {
83          /**
84           * D1597A17A6.
85           */
86          private static final long serialVersionUID = 899149338534L;
87  
88          /**
89           * Instantiate a new PdfGenerationException with a fixed message and the given cause.
90           * @param cause Previous exception
91           */
92          public HssfGenerationException(Throwable cause)
93          {
94              super(DefaultHssfExportView.class, Messages.getString("DefaultHssfExportView.errorexporting"), cause); //$NON-NLS-1$
95          }
96  
97          /**
98           * @see org.displaytag.exception.BaseNestableJspTagException#getSeverity()
99           */
100         public SeverityEnum getSeverity()
101         {
102             return SeverityEnum.ERROR;
103         }
104     }
105 }