1 package org.springframework.samples.countries.web; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 6 import javax.servlet.http.HttpServletRequest ; 7 import javax.servlet.http.HttpServletResponse ; 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 39 public class CountriesExcelView extends AbstractExcelView { 40 41 protected void buildExcelDocument( 42 Map model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) 43 throws NoSuchMessageException { 44 45 RefreshablePagedListHolder pgHolder = (RefreshablePagedListHolder) model.get("countries"); 47 48 HSSFSheet sheet = wb.createSheet("Spring Countries"); 50 53 if (pgHolder == null) { 56 getCell(sheet, 0, 0).setCellValue(getMessageSourceAccessor().getMessage("nolist")); 57 return; 58 } 59 60 HSSFFont f = wb.createFont(); 62 f.setFontHeightInPoints((short) 12); 64 f.setColor((short) 0xc); 66 f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 68 69 HSSFCellStyle cs = wb.createCellStyle(); 71 cs.setFont(f); 72 cs.setAlignment(HSSFCellStyle.ALIGN_CENTER); 73 74 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 HSSFCellStyle dateStyle = wb.createCellStyle(); 83 dateStyle.setFont(fp); 84 dateStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); 85 dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); 86 87 sheet.setColumnWidth((short) 0, (short) (20 * 256)); 89 sheet.setColumnWidth((short) 1, (short) (20 * 256)); 90 91 SortDefinition sort = pgHolder.getSort(); 93 CountriesFilter filter = (CountriesFilter) pgHolder.getFilter(); 94 95 int row = 0; 96 97 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 (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 (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 135 sheet = wb.createSheet(getMessageSourceAccessor().getMessage("countries")); 137 sheet.setColumnWidth((short) 1, (short) (30 * 256)); 138 row = 0; 139 140 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 Iterator 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 |