1 16 17 package org.springframework.web.servlet.view.document; 18 19 import java.util.Locale ; 20 import java.util.Map ; 21 22 import javax.servlet.ServletOutputStream ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.poi.hssf.usermodel.HSSFCell; 27 import org.apache.poi.hssf.usermodel.HSSFRow; 28 import org.apache.poi.hssf.usermodel.HSSFSheet; 29 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 30 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 31 32 import org.springframework.core.io.Resource; 33 import org.springframework.core.io.support.LocalizedResourceHelper; 34 import org.springframework.web.servlet.support.RequestContextUtils; 35 import org.springframework.web.servlet.view.AbstractView; 36 37 96 public abstract class AbstractExcelView extends AbstractView { 97 98 99 private static final String CONTENT_TYPE = "application/vnd.ms-excel"; 100 101 102 private static final String EXTENSION = ".xls"; 103 104 105 private String url; 106 107 108 112 public AbstractExcelView() { 113 setContentType(CONTENT_TYPE); 114 } 115 116 119 public void setUrl(String url) { 120 this.url = url; 121 } 122 123 124 127 protected final void renderMergedOutputModel( 128 Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { 129 130 HSSFWorkbook workbook; 131 if (this.url != null) { 132 workbook = getTemplateSource(this.url, request); 133 } 134 else { 135 workbook = new HSSFWorkbook(); 136 logger.debug("Created Excel Workbook from scratch"); 137 } 138 139 buildExcelDocument(model, workbook, request, response); 140 141 response.setContentType(getContentType()); 143 ServletOutputStream out = response.getOutputStream(); 144 workbook.write(out); 145 out.flush(); 146 } 147 148 155 protected HSSFWorkbook getTemplateSource(String url, HttpServletRequest request) throws Exception { 156 LocalizedResourceHelper helper = new LocalizedResourceHelper(getApplicationContext()); 157 Locale userLocale = RequestContextUtils.getLocale(request); 158 Resource inputFile = helper.findLocalizedResource(url, EXTENSION, userLocale); 159 160 if (logger.isDebugEnabled()) { 162 logger.debug("Loading Excel workbook from " + inputFile); 163 } 164 POIFSFileSystem fs = new POIFSFileSystem(inputFile.getInputStream()); 165 HSSFWorkbook workBook = new HSSFWorkbook(fs); 166 return workBook; 167 } 168 169 177 protected abstract void buildExcelDocument( 178 Map model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) 179 throws Exception ; 180 181 182 191 protected HSSFCell getCell(HSSFSheet sheet, int row, int col) { 192 HSSFRow sheetRow = sheet.getRow(row); 193 if (sheetRow == null) { 194 sheetRow = sheet.createRow(row); 195 } 196 HSSFCell cell = sheetRow.getCell((short) col); 197 if (cell == null) { 198 cell = sheetRow.createCell((short) col); 199 } 200 return cell; 201 } 202 203 208 protected void setText(HSSFCell cell, String text) { 209 cell.setCellType(HSSFCell.CELL_TYPE_STRING); 210 cell.setCellValue(text); 211 } 212 213 } 214 | Popular Tags |