PageRenderTime 69ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/de/dsteiner/wub/web/orderlist/report/excel/SheetBuilderBase.java

https://github.com/dsteiner/wub
Java | 226 lines | 170 code | 37 blank | 19 comment | 12 complexity | b84f8228ffaeba6763bfd2367150c06c MD5 | raw file
  1. package de.dsteiner.wub.web.orderlist.report.excel;
  2. import org.apache.commons.lang.Validate;
  3. import org.apache.poi.hssf.usermodel.HSSFCell;
  4. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  5. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
  6. import org.apache.poi.hssf.usermodel.HSSFComment;
  7. import org.apache.poi.hssf.usermodel.HSSFFont;
  8. import org.apache.poi.hssf.usermodel.HSSFPatriarch;
  9. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  10. import org.apache.poi.hssf.usermodel.HSSFRow;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.hssf.util.HSSFColor;
  14. import de.dsteiner.wub.domain.Food;
  15. import de.dsteiner.wub.domain.OrderListTotalFoodQuantity;
  16. public abstract class SheetBuilderBase {
  17. //protected Log log = LogFactory.getLog(SheetBuilderBase.class);
  18. protected final HSSFCellStyle borderedCellStyle;
  19. protected final HSSFCellStyle borderedBoldCellStyle;
  20. protected final HSSFCellStyle borderedCurrencyCellStyle;
  21. protected final HSSFCellStyle borderedBoldUnderlinedCellStyle;
  22. protected final HSSFFont fatUnterlinedFont;
  23. protected final HSSFCellStyle borderedHeaderCellStyle;
  24. protected final HSSFWorkbook workbook;
  25. protected String sheetName;
  26. private HSSFPatriarch drawingPatriarch;
  27. private static final char[] A2Z = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  28. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
  29. 'V', 'W', 'X', 'Y', 'Z' };
  30. protected final HSSFSheet sheet;
  31. protected static final String COLUMN_NUMBER_STR = "Anzahl";
  32. protected static final String COLUMN_TOTAL_PRICE_STR = "Gesamtpreis";
  33. protected static final String COLUMN_UNIT_PRICE_STR = "Einzelpreis";
  34. protected static final String COLUMN_COMMON_FOOD_CONTRIBUTION_STR = "Einz.Umlage";
  35. public SheetBuilderBase(final HSSFWorkbook workbook, final String sheetName) {
  36. this.workbook = workbook;
  37. this.sheetName = sheetName;
  38. Validate.notNull(workbook);
  39. Validate.notNull(sheetName);
  40. borderedCellStyle = createBorderedCellStyle(workbook);
  41. borderedBoldCellStyle = createBorderedBoldFontCellStyle(workbook);
  42. borderedCurrencyCellStyle = createBorderedCurrencyCellStyle(workbook,
  43. null);
  44. fatUnterlinedFont = createFont(workbook, true, true);
  45. borderedBoldUnderlinedCellStyle = createBorderedCurrencyCellStyle(
  46. workbook, fatUnterlinedFont);
  47. borderedHeaderCellStyle = createBorderedHeaderCellStyle(workbook);
  48. sheet = workbook.createSheet(sheetName);
  49. sheet.setDefaultColumnWidth((short) 12);
  50. }
  51. /**
  52. * returns the excel cell number (eg. C11, E4, AD1305 etc.) for this cell.
  53. */
  54. public String getCellRefString(final int row, final int col) {
  55. final StringBuffer retval = new StringBuffer();
  56. int tempcellnum = col;
  57. do {
  58. retval.insert(0, A2Z[tempcellnum % 26]);
  59. tempcellnum = (tempcellnum / 26) - 1;
  60. } while (tempcellnum >= 0);
  61. retval.append(row + 1);
  62. return retval.toString();
  63. }
  64. public String getCellRefString(final int row, final int col,
  65. final String sheetName) {
  66. String ref = getCellRefString(row, col);
  67. if (sheetName != null) {
  68. ref = sheetName + "!" + ref;
  69. }
  70. return ref;
  71. }
  72. private HSSFFont createFont(final HSSFWorkbook workbook,
  73. final boolean bold, final boolean doubleUnderlined) {
  74. final HSSFFont font = workbook.createFont();
  75. font.setFontHeightInPoints((short) 10);
  76. font.setFontName("Arial");
  77. if (bold) {
  78. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  79. }
  80. if (doubleUnderlined) {
  81. font.setUnderline(HSSFFont.U_DOUBLE);
  82. }
  83. return font;
  84. }
  85. private HSSFCellStyle createBorderedHeaderCellStyle(
  86. final HSSFWorkbook workbook) {
  87. final HSSFFont headerFont = createFont(workbook, true, false);
  88. final HSSFCellStyle headerCellStyle = workbook.createCellStyle();
  89. headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  90. headerCellStyle.setFont(headerFont);
  91. headerCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  92. headerCellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
  93. headerCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  94. headerCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
  95. headerCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  96. headerCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
  97. return headerCellStyle;
  98. }
  99. protected HSSFCell createCell(final short x, final String content,
  100. final HSSFRow row, final HSSFCellStyle cellStyle) {
  101. final HSSFCell cell = row.createCell(x);
  102. cell.setCellValue(content);
  103. cell.setCellStyle(cellStyle);
  104. return cell;
  105. }
  106. protected HSSFCell createNumberCell(final short x,
  107. final double euroAndCents, final HSSFRow row,
  108. final HSSFCellStyle cellStyle) {
  109. final HSSFCell cell = row.createCell(x);
  110. cell.setCellValue(euroAndCents);
  111. cell.setCellStyle(cellStyle);
  112. return cell;
  113. }
  114. protected HSSFCell createCell(final short x, final int content,
  115. final HSSFRow row, final HSSFCellStyle cellStyle) {
  116. final HSSFCell cell = row.createCell(x);
  117. cell.setCellValue(content);
  118. cell.setCellStyle(cellStyle);
  119. return cell;
  120. }
  121. private HSSFCellStyle createBorderedCellStyle(final HSSFWorkbook workbook) {
  122. final HSSFFont bodyCellFont = createFont(workbook, false, false);
  123. final HSSFCellStyle bodyCellStyle = workbook.createCellStyle();
  124. bodyCellStyle.setFont(bodyCellFont);
  125. bodyCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  126. bodyCellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
  127. bodyCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  128. bodyCellStyle.setRightBorderColor(HSSFColor.BLACK.index);
  129. return bodyCellStyle;
  130. }
  131. private HSSFCellStyle createBorderedBoldFontCellStyle(
  132. final HSSFWorkbook workbook) {
  133. final HSSFCellStyle cellStyle = createBorderedHeaderCellStyle(workbook);
  134. cellStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  135. return cellStyle;
  136. }
  137. private HSSFCellStyle createBorderedCurrencyCellStyle(
  138. final HSSFWorkbook workbook, final HSSFFont font) {
  139. // final CreationHelper createHelper = workbook.getCreationHelper();
  140. final HSSFCellStyle cellStyle = createBorderedCellStyle(workbook);
  141. if (font != null) {
  142. cellStyle.setFont(font);
  143. }
  144. // 3.0 ist buggy, geht nicht
  145. // final short format = workbook.createDataFormat().getFormat(
  146. // "#.##0.00\u20AC");
  147. cellStyle.setDataFormat((short) 4);
  148. return cellStyle;
  149. }
  150. protected double getPriceAsDouble(
  151. final OrderListTotalFoodQuantity totalFoodQuantity) {
  152. return getPriceAsDouble(totalFoodQuantity.getFood());
  153. }
  154. protected double getPriceAsDouble(final Food food) {
  155. double e = food.getEuro() == null ? 0.0 : food.getEuro();
  156. // auf 2 Stellen abschneiden.
  157. return ((int)(e*100))/100.0;
  158. }
  159. // protected HeaderColumn {
  160. // final protected String name;
  161. // private HeaderColumn(final String name)
  162. // {
  163. // this.name = name;
  164. // }
  165. // }
  166. public HSSFPatriarch getDrawingPatriarch() {
  167. if (drawingPatriarch == null) {
  168. drawingPatriarch = workbook.getSheet(sheetName)
  169. .createDrawingPatriarch();
  170. }
  171. return drawingPatriarch;
  172. }
  173. protected void createSimpleComment(final HSSFCell cell,
  174. final String commentString, final String author) {
  175. Validate.notNull(commentString);
  176. // must be the same instance per sheet!
  177. final HSSFPatriarch patr = this.getDrawingPatriarch();
  178. // anchor defines size and position of the comment in worksheet
  179. final HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
  180. (short) 4, 2, (short) 6, 7);
  181. final HSSFComment comment = patr.createComment(anchor);
  182. // set text in the comment
  183. comment.setString(new HSSFRichTextString(commentString));
  184. if (author != null) {
  185. comment.setAuthor(author);
  186. }
  187. cell.setCellComment(comment);
  188. }
  189. }