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  package org.displaytag.export;
13  
14  import org.apache.commons.lang.StringUtils;
15  import org.displaytag.model.TableModel;
16  
17  
18  /**
19   * Export view for excel exporting.
20   * @author Fabrizio Giustina
21   * @version $Revision: 1081 $ ($Author: fgiust $)
22   */
23  public class ExcelView extends BaseExportView
24  {
25  
26      /**
27       * @see org.displaytag.export.BaseExportView#setParameters(TableModel, boolean, boolean, boolean)
28       */
29      public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
30          boolean decorateValues)
31      {
32          super.setParameters(tableModel, exportFullList, includeHeader, decorateValues);
33      }
34  
35      /**
36       * @see org.displaytag.export.ExportView#getMimeType()
37       * @return "application/vnd.ms-excel"
38       */
39      public String getMimeType()
40      {
41          return "application/vnd.ms-excel"; //$NON-NLS-1$
42      }
43  
44      /**
45       * @see org.displaytag.export.BaseExportView#getRowEnd()
46       */
47      protected String getRowEnd()
48      {
49          return "\n"; //$NON-NLS-1$
50      }
51  
52      /**
53       * @see org.displaytag.export.BaseExportView#getCellEnd()
54       */
55      protected String getCellEnd()
56      {
57          return "\t"; //$NON-NLS-1$
58      }
59  
60      /**
61       * @see org.displaytag.export.BaseExportView#getAlwaysAppendCellEnd()
62       * @return false
63       */
64      protected boolean getAlwaysAppendCellEnd()
65      {
66          return false;
67      }
68  
69      /**
70       * @see org.displaytag.export.BaseExportView#getAlwaysAppendRowEnd()
71       * @return false
72       */
73      protected boolean getAlwaysAppendRowEnd()
74      {
75          return false;
76      }
77  
78      /**
79       * Escaping for excel format.
80       * <ul>
81       * <li>Quotes inside quoted strings are escaped with a double quote</li>
82       * <li>Fields are surrounded by " (should be optional, but sometimes you get a "Sylk error" without those)</li>
83       * </ul>
84       * @see org.displaytag.export.BaseExportView#escapeColumnValue(java.lang.Object)
85       */
86      protected String escapeColumnValue(Object value)
87      {
88          if (value != null)
89          {
90              // quotes around fields are needed to avoid occasional "Sylk format invalid" messages from excel
91              return "\"" //$NON-NLS-1$
92                  + StringUtils.replace(StringUtils.trim(value.toString()), "\"", "\"\"") //$NON-NLS-1$ //$NON-NLS-2$ 
93                  + "\""; //$NON-NLS-1$ 
94          }
95  
96          return null;
97      }
98  
99  }