1 12 package org.displaytag.render; 13 14 import java.util.Calendar ; 15 import java.util.Date ; 16 import java.util.Iterator ; 17 18 import org.apache.commons.lang.ObjectUtils; 19 import org.apache.commons.lang.StringEscapeUtils; 20 import org.apache.commons.lang.StringUtils; 21 import org.apache.commons.lang.math.NumberUtils; 22 import org.apache.poi.hssf.usermodel.HSSFCell; 23 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 24 import org.apache.poi.hssf.usermodel.HSSFDataFormat; 25 import org.apache.poi.hssf.usermodel.HSSFFont; 26 import org.apache.poi.hssf.usermodel.HSSFRow; 27 import org.apache.poi.hssf.usermodel.HSSFSheet; 28 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 29 import org.apache.poi.hssf.util.HSSFColor; 30 import org.apache.poi.hssf.util.Region; 31 import org.displaytag.decorator.TableDecorator; 32 import org.displaytag.decorator.hssf.DecoratesHssf; 33 import org.displaytag.model.Column; 34 import org.displaytag.model.HeaderCell; 35 import org.displaytag.model.Row; 36 import org.displaytag.model.TableModel; 37 38 39 45 public class HssfTableWriter extends TableWriterAdapter 46 { 47 48 51 private HSSFWorkbook wb; 52 53 56 private HSSFSheet sheet; 57 58 61 private int rowNum; 62 63 66 private HSSFRow currentRow; 67 68 71 private int colNum; 72 73 76 private HSSFCell currentCell; 77 78 81 private short pctFormat = HSSFDataFormat.getBuiltinFormat("0.00%"); 82 83 87 public HssfTableWriter(HSSFWorkbook wb) 88 { 89 this.wb = wb; 90 } 91 92 95 protected void writeTableOpener(TableModel model) throws Exception 96 { 97 this.sheet = wb.createSheet("-"); 98 this.rowNum = 0; 99 } 100 101 104 protected void writeCaption(TableModel model) throws Exception 105 { 106 HSSFCellStyle style = this.wb.createCellStyle(); 107 HSSFFont bold = this.wb.createFont(); 108 bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 109 bold.setFontHeightInPoints((short) 14); 110 style.setFont(bold); 111 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 112 113 this.colNum = 0; 114 this.currentRow = this.sheet.createRow(this.rowNum++); 115 this.currentCell = this.currentRow.createCell((short) this.colNum); 116 this.currentCell.setCellStyle(style); 117 String caption = model.getCaption(); 118 this.currentCell.setCellValue(caption); 119 this.rowSpanTable(model); 120 } 121 122 128 private Region getMergeCellsRegion(short first, short last) 129 { 130 return new Region(this.currentRow.getRowNum(), first, this.currentRow.getRowNum(), last); 131 } 132 133 136 protected void writeTableHeader(TableModel model) throws Exception 137 { 138 this.currentRow = this.sheet.createRow(this.rowNum++); 139 this.colNum = 0; 140 HSSFCellStyle headerStyle = this.getHeaderFooterStyle(); 141 for (Iterator iterator = model.getHeaderCellList().iterator(); iterator.hasNext();) 142 { 143 HeaderCell headerCell = (HeaderCell) iterator.next(); 144 String columnHeader = headerCell.getTitle(); 145 if (columnHeader == null) 146 { 147 columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName()); 148 } 149 150 this.writeHeaderFooter(columnHeader, this.currentRow, headerStyle); 151 } 152 } 153 154 157 protected void writeDecoratedRowStart(TableModel model) 158 { 159 model.getTableDecorator().startRow(); 160 } 161 162 165 protected void writeRowOpener(Row row) throws Exception 166 { 167 this.currentRow = this.sheet.createRow(rowNum++); 168 this.colNum = 0; 169 } 170 171 175 protected void writeColumnOpener(Column column) throws Exception 176 { 177 column.getOpenTag(); this.currentCell = this.currentRow.createCell((short) this.colNum++); 179 this.currentCell.setEncoding(HSSFCell.ENCODING_UTF_16); 180 } 181 182 185 protected void writeColumnValue(Object value, Column column) throws Exception 186 { 187 if (value instanceof Number ) 188 { 189 Number num = (Number ) value; 190 if (value.toString().indexOf("%") > -1) 192 { 193 this.currentCell.setCellValue(num.doubleValue() / 100); 194 HSSFCellStyle cellStyle = this.wb.createCellStyle(); 195 cellStyle.setDataFormat(this.pctFormat); 196 this.currentCell.setCellStyle(cellStyle); 197 } 198 else 199 { 200 this.currentCell.setCellValue(num.doubleValue()); 201 } 202 } 203 else if (value instanceof Date ) 204 { 205 this.currentCell.setCellValue((Date ) value); 206 } 207 else if (value instanceof Calendar ) 208 { 209 this.currentCell.setCellValue((Calendar ) value); 210 } 211 else 212 { 213 this.currentCell.setCellValue(this.escapeColumnValue(value)); 214 } 215 216 } 217 218 221 protected void writeDecoratedRowFinish(TableModel model) throws Exception 222 { 223 DecoratesHssf decorator = (DecoratesHssf) model.getTableDecorator(); 224 decorator.setSheet(this.sheet); 225 ((TableDecorator) decorator).finishRow(); 226 this.rowNum = this.sheet.getLastRowNum(); 227 this.rowNum++; 228 } 229 230 233 protected void writePostBodyFooter(TableModel model) throws Exception 234 { 235 this.colNum = 0; 236 this.currentRow = this.sheet.createRow(this.rowNum++); 237 this.writeHeaderFooter(model.getFooter(), this.currentRow, this.getHeaderFooterStyle()); 238 this.rowSpanTable(model); 239 } 240 241 245 private void rowSpanTable(TableModel model) 246 { 247 this.sheet.addMergedRegion(this.getMergeCellsRegion(this.currentCell.getCellNum(), (short) (model 248 .getNumberOfColumns() - 1))); 249 } 250 251 254 protected void writeDecoratedTableFinish(TableModel model) 255 { 256 model.getTableDecorator().finish(); 257 } 258 259 265 protected String escapeColumnValue(Object rawValue) 266 { 267 if (rawValue == null) 268 { 269 return null; 270 } 271 String returnString = ObjectUtils.toString(rawValue); 272 returnString = StringEscapeUtils.escapeJava(StringUtils.trimToEmpty(returnString)); 274 returnString = StringUtils.replace(StringUtils.trim(returnString), "\\t", " "); 276 returnString = StringUtils.replace(StringUtils.trim(returnString), "\\r", " "); 278 returnString = StringEscapeUtils.unescapeJava(returnString); 280 return returnString; 281 } 282 283 288 protected boolean isNumber(String rawValue) 289 { 290 if (rawValue == null) 291 { 292 return false; 293 } 294 String rawV = rawValue; 295 if (rawV.indexOf('%') > -1) 296 { 297 rawV = rawV.replace('%', ' ').trim(); 298 } 299 if (rawV.indexOf('$') > -1) 300 { 301 rawV = rawV.replace('$', ' ').trim(); 302 } 303 if (rawV.indexOf(',') > -1) 304 { 305 rawV = StringUtils.replace(rawV, ",", ""); 306 } 307 return NumberUtils.isNumber(rawV.trim()); 308 } 309 310 316 private void writeHeaderFooter(String value, HSSFRow row, HSSFCellStyle style) 317 { 318 this.currentCell = row.createCell((short) this.colNum++); 319 this.currentCell.setCellValue(value); 320 this.currentCell.setCellStyle(style); 321 this.currentCell.setEncoding(HSSFCell.ENCODING_UTF_16); 322 } 323 324 328 private HSSFCellStyle getHeaderFooterStyle() 329 { 330 HSSFCellStyle style = this.wb.createCellStyle(); 331 style.setFillPattern(HSSFCellStyle.FINE_DOTS); 332 style.setFillBackgroundColor(HSSFColor.BLUE_GREY.index); 333 HSSFFont bold = this.wb.createFont(); 334 bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 335 bold.setColor(HSSFColor.WHITE.index); 336 style.setFont(bold); 337 return style; 338 } 339 } 340 | Popular Tags |