PageRenderTime 72ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/wflow-consoleweb/src/main/java/org/joget/apps/displaytag/export/CustomExcelHssfView.java

https://github.com/realakuma/jw-community
Java | 211 lines | 125 code | 31 blank | 55 comment | 15 complexity | fa360986e2c2ac513a9e351d1fc17e23 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, GPL-3.0
  1. package org.joget.apps.displaytag.export;
  2. import java.io.OutputStream;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.Iterator;
  6. import javax.servlet.jsp.JspException;
  7. import org.apache.commons.lang.ObjectUtils;
  8. import org.apache.commons.lang.StringEscapeUtils;
  9. import org.apache.commons.lang.StringUtils;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. import org.apache.poi.hssf.usermodel.HSSFFont;
  13. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  14. import org.apache.poi.hssf.usermodel.HSSFRow;
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  17. import org.apache.poi.hssf.util.HSSFColor;
  18. import org.displaytag.Messages;
  19. import org.displaytag.exception.BaseNestableJspTagException;
  20. import org.displaytag.exception.SeverityEnum;
  21. import org.displaytag.export.BinaryExportView;
  22. import org.displaytag.export.excel.ExcelHssfView;
  23. import org.displaytag.model.Column;
  24. import org.displaytag.model.ColumnIterator;
  25. import org.displaytag.model.HeaderCell;
  26. import org.displaytag.model.Row;
  27. import org.displaytag.model.RowIterator;
  28. import org.displaytag.model.TableModel;
  29. public class CustomExcelHssfView implements BinaryExportView {
  30. /**
  31. * TableModel to render.
  32. */
  33. private TableModel model;
  34. /**
  35. * export full list?
  36. */
  37. private boolean exportFull;
  38. /**
  39. * include header in export?
  40. */
  41. private boolean header;
  42. /**
  43. * decorate export?
  44. */
  45. private boolean decorated;
  46. /**
  47. * @see org.displaytag.export.ExportView#setParameters(TableModel, boolean, boolean, boolean)
  48. */
  49. public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
  50. boolean decorateValues) {
  51. this.model = tableModel;
  52. this.exportFull = exportFullList;
  53. this.header = includeHeader;
  54. this.decorated = decorateValues;
  55. }
  56. /**
  57. * @return "application/vnd.ms-excel"
  58. * @see org.displaytag.export.BaseExportView#getMimeType()
  59. */
  60. public String getMimeType() {
  61. return "application/vnd.ms-excel"; //$NON-NLS-1$
  62. }
  63. public void doExport(OutputStream out) throws JspException {
  64. try {
  65. HSSFWorkbook wb = new HSSFWorkbook();
  66. HSSFSheet sheet = wb.createSheet("-");
  67. int rowNum = 0;
  68. int colNum = 0;
  69. if (this.header) {
  70. // Create an header row
  71. HSSFRow xlsRow = sheet.createRow(rowNum++);
  72. HSSFCellStyle headerStyle = wb.createCellStyle();
  73. headerStyle.setFillPattern(HSSFCellStyle.FINE_DOTS);
  74. headerStyle.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
  75. HSSFFont bold = wb.createFont();
  76. bold.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  77. bold.setColor(HSSFColor.WHITE.index);
  78. headerStyle.setFont(bold);
  79. Iterator iterator = this.model.getHeaderCellList().iterator();
  80. while (iterator.hasNext()) {
  81. HeaderCell headerCell = (HeaderCell) iterator.next();
  82. String columnHeader = headerCell.getTitle();
  83. if (columnHeader == null) {
  84. columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
  85. }
  86. HSSFCell cell = xlsRow.createCell(colNum++);
  87. cell.setCellValue(new HSSFRichTextString(columnHeader));
  88. cell.setCellStyle(headerStyle);
  89. }
  90. }
  91. // get the correct iterator (full or partial list according to the exportFull field)
  92. RowIterator rowIterator = this.model.getRowIterator(this.exportFull);
  93. // iterator on rows
  94. while (rowIterator.hasNext()) {
  95. Row row = rowIterator.next();
  96. HSSFRow xlsRow = sheet.createRow(rowNum++);
  97. colNum = 0;
  98. // iterator on columns
  99. ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());
  100. while (columnIterator.hasNext()) {
  101. Column column = columnIterator.nextColumn();
  102. // Get the value to be displayed for the column
  103. Object value = column.getValue(this.decorated);
  104. HSSFCell cell = xlsRow.createCell(colNum++);
  105. writeCell(value, cell);
  106. }
  107. }
  108. // adjust the column widths
  109. int colCount = 0;
  110. while (colCount <= colNum) {
  111. sheet.autoSizeColumn((short) colCount++);
  112. }
  113. wb.write(out);
  114. } catch (Exception e) {
  115. throw new ExcelGenerationException(e);
  116. }
  117. }
  118. /**
  119. * Write the value to the cell. Override this method if you have complex data types that may need to be exported.
  120. * @param value the value of the cell
  121. * @param cell the cell to write it to
  122. */
  123. protected void writeCell(Object value, HSSFCell cell) {
  124. if (value instanceof Number) {
  125. Number num = (Number) value;
  126. cell.setCellValue(num.doubleValue());
  127. } else if (value instanceof Date) {
  128. cell.setCellValue((Date) value);
  129. } else if (value instanceof Calendar) {
  130. cell.setCellValue((Calendar) value);
  131. } else {
  132. cell.setCellValue(new HSSFRichTextString(escapeColumnValue(value)));
  133. }
  134. }
  135. // patch from Karsten Voges
  136. /**
  137. * Escape certain values that are not permitted in excel cells.
  138. * @param rawValue the object value
  139. * @return the escaped value
  140. */
  141. protected String escapeColumnValue(Object rawValue) {
  142. if (rawValue == null) {
  143. return null;
  144. }
  145. String returnString = ObjectUtils.toString(rawValue);
  146. // escape the String to get the tabs, returns, newline explicit as \t \r \n
  147. returnString = StringEscapeUtils.escapeJava(StringUtils.trimToEmpty(returnString));
  148. // remove tabs, insert four whitespaces instead
  149. returnString = StringUtils.replace(StringUtils.trim(returnString), "\\t", " ");
  150. // remove the return, only newline valid in excel
  151. returnString = StringUtils.replace(StringUtils.trim(returnString), "\\r", " ");
  152. // unescape so that \n gets back to newline
  153. returnString = StringEscapeUtils.unescapeJava(returnString);
  154. return returnString;
  155. }
  156. /**
  157. * Wraps IText-generated exceptions.
  158. * @author Fabrizio Giustina
  159. * @version $Revision: 1143 $ ($Author: fgiust $)
  160. */
  161. static class ExcelGenerationException extends BaseNestableJspTagException {
  162. /**
  163. * D1597A17A6.
  164. */
  165. private static final long serialVersionUID = 899149338534L;
  166. /**
  167. * Instantiate a new PdfGenerationException with a fixed message and the given cause.
  168. * @param cause Previous exception
  169. */
  170. public ExcelGenerationException(Throwable cause) {
  171. super(ExcelHssfView.class, Messages.getString("ExcelView.errorexporting"), cause); //$NON-NLS-1$
  172. }
  173. /**
  174. * @see org.displaytag.exception.BaseNestableJspTagException#getSeverity()
  175. */
  176. public SeverityEnum getSeverity() {
  177. return SeverityEnum.ERROR;
  178. }
  179. }
  180. }