KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > countries > web > CountriesExcelView


1 package org.springframework.samples.countries.web;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
8
9 import org.apache.poi.hssf.usermodel.HSSFCell;
10 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
11 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
12 import org.apache.poi.hssf.usermodel.HSSFFont;
13 import org.apache.poi.hssf.usermodel.HSSFSheet;
14 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
15
16 import org.springframework.beans.support.RefreshablePagedListHolder;
17 import org.springframework.beans.support.SortDefinition;
18 import org.springframework.context.NoSuchMessageException;
19 import org.springframework.samples.countries.Country;
20 import org.springframework.web.servlet.view.document.AbstractExcelView;
21
22 /**
23  * This view demonstrates how to send an Excel file with the Spring Framework
24  * using the jakarta's POI library.
25  *
26  * <p>Here create a document from scratch, but it is also possible to start from a template
27  * document. In this case, add an url property in the view definition like:<br>
28  * countries_excelView.url=/WEB-INF/views/excel/countries
29  *
30  * <p>Creating the directories, put an excel file '/WEB-INF/views/excel/countries.xls',
31  * it will be taken as a starting point.
32  *
33  * <p>You can also add in the same directory files like 'countries_en.xls', 'countries_fr.xls'
34  * and so on. Theses files will take precedence if the user's locale matches.
35  *
36  * @author Jean-Pierre Pawlak
37  * @author Juergen Hoeller
38  */

39 public class CountriesExcelView extends AbstractExcelView {
40
41     protected void buildExcelDocument(
42             Map JavaDoc model, HSSFWorkbook wb, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
43             throws NoSuchMessageException {
44
45         // We search the data to insert.
46
RefreshablePagedListHolder pgHolder = (RefreshablePagedListHolder) model.get("countries");
47
48         // As we use a from scratch document, we create a new sheet.
49
HSSFSheet sheet = wb.createSheet("Spring Countries");
50         // If we will use the first sheet from an existing document, replace by this:
51
// sheet = wb.getSheetAt(0);
52

53         // We simply put an error message on the first cell if no list is available
54
// Nevertheless, it should never be null as the controller verify it.
55
if (pgHolder == null) {
56             getCell(sheet, 0, 0).setCellValue(getMessageSourceAccessor().getMessage("nolist"));
57             return;
58         }
59
60         // We create a font for headers
61
HSSFFont f = wb.createFont();
62         // set font 1 to 12 point type
63
f.setFontHeightInPoints((short) 12);
64         // make it blue
65
f.setColor((short) 0xc);
66         // make it bold arial is the default font
67
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
68
69         // We create a style for headers
70
HSSFCellStyle cs = wb.createCellStyle();
71         cs.setFont(f);
72         cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
73
74         // The same for properties data
75
HSSFFont fp = wb.createFont();
76         fp.setColor((short) 0xc);
77         HSSFCellStyle csp = wb.createCellStyle();
78         csp.setFont(fp);
79         csp.setAlignment(HSSFCellStyle.ALIGN_CENTER);
80
81         // We create a date style
82
HSSFCellStyle dateStyle = wb.createCellStyle();
83         dateStyle.setFont(fp);
84         dateStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
85         dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
86
87         // We set the colum width of the two first columns
88
sheet.setColumnWidth((short) 0, (short) (20 * 256));
89         sheet.setColumnWidth((short) 1, (short) (20 * 256));
90
91         // We prepare some data
92
SortDefinition sort = pgHolder.getSort();
93         CountriesFilter filter = (CountriesFilter) pgHolder.getFilter();
94
95         int row = 0;
96
97         // We put some information about the user request on the sheet
98
// getCell is a useful add-on provided by the AbstractExcelView
99
// The labels could be pre-inserted in a template document
100
getCell(sheet, row, 0).setCellValue(getMessageSourceAccessor().getMessage("date.extraction"));
101         getCell(sheet, row, 1).setCellValue(pgHolder.getRefreshDate());
102         getCell(sheet, row, 1).setCellStyle(dateStyle);
103         row++;
104
105         getCell(sheet, row, 0).setCellValue(getMessageSourceAccessor().getMessage("nbRecords"));
106         getCell(sheet, row, 1).setCellValue(pgHolder.getNrOfElements());
107         getCell(sheet, row, 1).setCellStyle(csp);
108         row++;
109
110         getCell(sheet, row, 0).setCellValue(getMessageSourceAccessor().getMessage("sort.name"));
111         getCell(sheet, row, 1).setCellValue(getMessageSourceAccessor().getMessage(sort.getProperty(), ""));
112         getCell(sheet, row, 1).setCellStyle(csp);
113         row++;
114
115         getCell(sheet, row, 0).setCellValue(getMessageSourceAccessor().getMessage("sort.asc"));
116         getCell(sheet, row, 1).setCellValue(getMessageSourceAccessor().getMessage(new Boolean JavaDoc(sort.isAscending()).toString()));
117         getCell(sheet, row, 1).setCellStyle(csp);
118         row++;
119
120         getCell(sheet, row, 0).setCellValue(getMessageSourceAccessor().getMessage("sort.igncase"));
121         getCell(sheet, row, 1).setCellValue(getMessageSourceAccessor().getMessage(new Boolean JavaDoc(sort.isIgnoreCase()).toString()));
122         getCell(sheet, row, 1).setCellStyle(csp);
123         row++;
124
125         getCell(sheet, row, 0).setCellValue(getMessageSourceAccessor().getMessage("filter.name"));
126         getCell(sheet, row, 1).setCellValue(filter.getName());
127         getCell(sheet, row, 1).setCellStyle(csp);
128         row++;
129
130         getCell(sheet, row, 0).setCellValue(getMessageSourceAccessor().getMessage("filter.code"));
131         getCell(sheet, row, 1).setCellValue(filter.getCode());
132         getCell(sheet, row, 1).setCellStyle(csp);
133         // row += 3;
134

135         // We create a second shhet for the data
136
sheet = wb.createSheet(getMessageSourceAccessor().getMessage("countries"));
137         sheet.setColumnWidth((short) 1, (short) (30 * 256));
138         row = 0;
139
140         // We put now the headers of the list on the sheet
141
HSSFCell cell = getCell(sheet, row, 0);
142         cell.setCellStyle(cs);
143         cell.setCellValue(getMessageSourceAccessor().getMessage("code"));
144         cell = getCell(sheet, row, 1);
145         cell.setCellStyle(cs);
146         cell.setCellValue(getMessageSourceAccessor().getMessage("name"));
147         row++;
148
149         // We put now the countries from the list on the sheet
150
Iterator JavaDoc it = pgHolder.getSource().iterator();
151         while (it.hasNext()) {
152             Country country = (Country) it.next();
153             getCell(sheet, row, 0).setCellValue(country.getCode());
154             getCell(sheet, row, 1).setCellValue(country.getName());
155             row++;
156         }
157     }
158
159 }
160
Popular Tags