Convenient superclass for Excel document views.
Compatible with Apache POI 3.5 and higher, as of Spring 4.0.
Properties:
- url (optional): The url of an existing Excel document to pick as a starting point.
It is done without localization part nor the ".xls" extension.
The file will be searched with locations in the following order:
- [url]_[language]_[country].xls
- [url]_[language].xls
- [url].xls
For working with the workbook in the subclass, see
Apache's POI site
As an example, you can try this snippet:
protected void buildExcelDocument(
Map<String, Object> model, HSSFWorkbook workbook,
HttpServletRequest request, HttpServletResponse response) {
// Go to the first sheet.
// getSheetAt: only if workbook is created from an existing document
// HSSFSheet sheet = workbook.getSheetAt(0);
HSSFSheet sheet = workbook.createSheet("Spring");
sheet.setDefaultColumnWidth(12);
// Write a text at A1.
HSSFCell cell = getCell(sheet, 0, 0);
setText(cell, "Spring POI test");
// Write the current date at A2.
HSSFCellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
cell = getCell(sheet, 1, 0);
cell.setCellValue(new Date());
cell.setCellStyle(dateStyle);
// Write a number at A3
getCell(sheet, 2, 0).setCellValue(458);
// Write a range of numbers.
HSSFRow sheetRow = sheet.createRow(3);
for (short i = 0; i < 10; i++) {
sheetRow.createCell(i).setCellValue(i * 10);
}
}
This class is similar to the AbstractPdfView class in usage style.