Coding the View
Listing 8-41 shows the class com.apress.expertspringmvc.flight.web.view.HomePageExcel,
which extends AbstractExcelView. The required method that you need to override is called
buildExcelDocument().
Listing 8-41.Creating the Excel Spreadsheet from the Model
public class HomePageExcel extends AbstractExcelView {
@Override
protected void buildExcelDocument(
Map model,
HSSFWorkbook workbook,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
HSSFSheet dataSheet = workbook.getSheet("FlightSearch");
List
(List
int row = 4;
for (SpecialDeal deal : specials) {
int column = 0;
getCell(dataSheet, row, ++column)
.setCellValue(deal.getDepartFrom().getName());
getCell(dataSheet, row, ++column)
.setCellValue(deal.getArriveAt().getName());
getCell(dataSheet, row, ++column)
.setCellValue(deal.getCost());
row++;
}
}
}
This should all be starting to look quite familiar by now if you’ve read the XSLT and PDF
sections earlier in this chapter.
The workbook is created for you by Spring and passed into this method as a parameter.
You simply need to manipulate this object appropriately. In the preceding code, you can see
that the model data is placed in particular cells of the spreadsheet in order to fit into the tem-
plate. The getCell()method is a convenience method included with AbstractExcelViewthat
just ensures the cell is available for you to set values on.
CHAPTER 8 ■SUPPORTED VIEW TYPES 259