1 package org.displaytag.export.excel;
2
3 import java.io.OutputStream;
4 import java.util.Calendar;
5 import java.util.Date;
6 import java.util.Iterator;
7
8 import javax.servlet.jsp.JspException;
9
10 import org.apache.commons.lang.ObjectUtils;
11 import org.apache.commons.lang.StringEscapeUtils;
12 import org.apache.commons.lang.StringUtils;
13 import org.apache.poi.hssf.usermodel.HSSFCell;
14 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
15 import org.apache.poi.hssf.usermodel.HSSFFont;
16 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
17 import org.apache.poi.hssf.usermodel.HSSFRow;
18 import org.apache.poi.hssf.usermodel.HSSFSheet;
19 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
20 import org.apache.poi.hssf.util.HSSFColor;
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.Column;
26 import org.displaytag.model.ColumnIterator;
27 import org.displaytag.model.HeaderCell;
28 import org.displaytag.model.Row;
29 import org.displaytag.model.RowIterator;
30 import org.displaytag.model.TableModel;
31
32
33
34
35
36
37
38
39 public class ExcelHssfView implements BinaryExportView
40 {
41
42
43
44
45 private TableModel model;
46
47
48
49
50 private boolean exportFull;
51
52
53
54
55 private boolean header;
56
57
58
59
60 private boolean decorated;
61
62
63
64
65 public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
66 boolean decorateValues)
67 {
68 this.model = tableModel;
69 this.exportFull = exportFullList;
70 this.header = includeHeader;
71 this.decorated = decorateValues;
72 }
73
74
75
76
77
78 public String getMimeType()
79 {
80 return "application/vnd.ms-excel";
81 }
82
83
84
85
86 public void doExport(OutputStream out) throws JspException
87 {
88 try
89 {
90 HSSFWorkbook wb = new HSSFWorkbook();
91 HSSFSheet sheet = wb.createSheet("-");
92
93 int rowNum = 0;
94 int colNum = 0;
95
96 if (this.header)
97 {
98
99 HSSFRow xlsRow = sheet.createRow(rowNum++);
100
101 HSSFCellStyle headerStyle = wb.createCellStyle();
102 headerStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
103 headerStyle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
104 HSSFFont bold = wb.createFont();
105 bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
106 bold.setColor(HSSFColor.WHITE.index);
107 headerStyle.setFont(bold);
108
109 Iterator iterator = this.model.getHeaderCellList().iterator();
110
111 while (iterator.hasNext())
112 {
113 HeaderCell headerCell = (HeaderCell) iterator.next();
114
115 String columnHeader = headerCell.getTitle();
116
117 if (columnHeader == null)
118 {
119 columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
120 }
121
122 HSSFCell cell = xlsRow.createCell(colNum++);
123 cell.setCellValue(new HSSFRichTextString(columnHeader));
124 cell.setCellStyle(headerStyle);
125 }
126 }
127
128
129 RowIterator rowIterator = this.model.getRowIterator(this.exportFull);
130
131
132 while (rowIterator.hasNext())
133 {
134 Row row = rowIterator.next();
135 HSSFRow xlsRow = sheet.createRow(rowNum++);
136 colNum = 0;
137
138
139 ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());
140
141 while (columnIterator.hasNext())
142 {
143 Column column = columnIterator.nextColumn();
144
145
146 Object value = column.getValue(this.decorated);
147
148 HSSFCell cell = xlsRow.createCell(colNum++);
149
150 writeCell(value, cell);
151 }
152 }
153
154
155 int colCount = 0;
156 while (colCount <= colNum)
157 {
158 sheet.autoSizeColumn((short) colCount++);
159 }
160
161 wb.write(out);
162 }
163 catch (Exception e)
164 {
165 throw new ExcelGenerationException(e);
166 }
167 }
168
169
170
171
172
173
174 protected void writeCell(Object value, HSSFCell cell)
175 {
176 if (value instanceof Number)
177 {
178 Number num = (Number) value;
179 cell.setCellValue(num.doubleValue());
180 }
181 else if (value instanceof Date)
182 {
183 cell.setCellValue((Date) value);
184 }
185 else if (value instanceof Calendar)
186 {
187 cell.setCellValue((Calendar) value);
188 }
189 else
190 {
191 cell.setCellValue(new HSSFRichTextString(escapeColumnValue(value)));
192 }
193 }
194
195
196
197
198
199
200
201 protected String escapeColumnValue(Object rawValue)
202 {
203 if (rawValue == null)
204 {
205 return null;
206 }
207 String returnString = ObjectUtils.toString(rawValue);
208
209 returnString = StringEscapeUtils.escapeJava(StringUtils.trimToEmpty(returnString));
210
211 returnString = StringUtils.replace(StringUtils.trim(returnString), "\\t", " ");
212
213 returnString = StringUtils.replace(StringUtils.trim(returnString), "\\r", " ");
214
215 returnString = StringEscapeUtils.unescapeJava(returnString);
216 return returnString;
217 }
218
219
220
221
222
223
224 static class ExcelGenerationException extends BaseNestableJspTagException
225 {
226
227
228
229
230 private static final long serialVersionUID = 899149338534L;
231
232
233
234
235
236 public ExcelGenerationException(Throwable cause)
237 {
238 super(ExcelHssfView.class, Messages.getString("ExcelView.errorexporting"), cause);
239 }
240
241
242
243
244 public SeverityEnum getSeverity()
245 {
246 return SeverityEnum.ERROR;
247 }
248 }
249
250 }