Using Excel Views

If your application requires Excel output, you can use Spring to create an Excel view rather than directly writing the contents of an Excel file to the output stream. Just like AbstractPdfView, Spring provides AbstractExcelView, which you can subclass to further simplify development of new Excel views. You are going to need the Jakarta POI (http://jakarta.apache.org/poi/) library to perform the actual Excel I/O; the POI JAR file comes with the Spring distribution.

Now we are going to show you a simple ProductsExcelView that renders a list of products into an Excel spreadsheet. We implement ProductsExcelView and modify the views.properties file. The implementation of ProductsExcelView extends AbstractExcelView, as shown in Listing 18-23.

Listing 18-23: ProductsExcelView Implementation

image from book
package com.apress.prospring.ch18.web.views;      import java.util.List; import java.util.Map;      import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;      import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.web.servlet.view.document.AbstractExcelView;      import com.apress.prospring.ch18.domain.Product;      public class ProductsExcelView extends AbstractExcelView {          private static final int COL_PRODUCT_ID = 0;     private static final int COL_NAME = 1;     private static final int COL_EXPIRATION_DATE = 2;          protected void buildExcelDocument(Map model, HSSFWorkbook wb,         HttpServletRequest request, HttpServletResponse response)          throws Exception {         List<Product> products = (List<Product>)model.get("products");         HSSFSheet sheet = wb.createSheet("Products");         int row = 0;         getCell(sheet, row, COL_PRODUCT_ID).setCellValue("ProductId");         getCell(sheet, row, COL_NAME).setCellValue("Name");         getCell(sheet, row, COL_EXPIRATION_DATE).setCellValue("ExpirationDate");         row++;         for (Product product : products) {             getCell(sheet, row, COL_PRODUCT_ID).setCellValue(                 product.getProductId());             getCell(sheet, row, COL_NAME).setCellValue(                 product.getName());             getCell(sheet, row, COL_EXPIRATION_DATE).setCellValue(                 product.getExpirationDate());             row++;         }     }      } 
image from book

The code for the ProductsExcelView class is quite simple: we get a List of Product objects and iterate over the list, adding a row to the Excel workbook in each iteration.

Next, we make sure that the product-index view declared in views.properties is referencing the newly created ProductsExcelView class, as shown in Listing 18-24.

Listing 18-24: views.properties with ProductsExcelView Definition

image from book
product-index.class=com.apress.prospring.ch18.web.views.ProductsExcelView
image from book

Again, we do not need to set any additional properties for this view. The ProductController.index() method does not need to be modified because it already returns ModelAndView("product-index", …). When we make a request to /product/index.html, we get an Excel spreadsheet, as shown on Figure 18-4.

image from book
Figure 18-4: Output of the ProductsExcelView

Using AbstractExcelView is very similar to using AbstractPdfView: in both cases, we have to implement code that creates the content of the document; we do not need to handle the I/O.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net