PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/skala/erp/bmp/server/excellexporters/ExcelExporter.java

https://bitbucket.org/ignatenko/bmp
Java | 214 lines | 207 code | 7 blank | 0 comment | 2 complexity | 6304271cdee37c7a9ee8e320e35735c4 MD5 | raw file
  1. package skala.erp.bmp.server.excellexporters;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.apache.poi.hpsf.SummaryInformation;
  9. import org.apache.poi.hssf.usermodel.HSSFRow;
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.hssf.util.CellReference;
  13. import org.apache.poi.ss.usermodel.Cell;
  14. import org.apache.poi.ss.usermodel.CellStyle;
  15. import org.apache.poi.ss.usermodel.Font;
  16. import org.apache.poi.ss.usermodel.IndexedColors;
  17. import org.apache.poi.ss.usermodel.Workbook;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. public abstract class ExcelExporter {
  20. protected final int MAX_LIST_LANDSCAPE_HEIGHT = 720;
  21. protected final int MAX_LIST_PORTRAIT_HEIGTH = 1018;
  22. protected final int ROW_HEIGHT = 12;
  23. protected HSSFWorkbook createWorkbook(String filename, String host)
  24. throws FileNotFoundException {
  25. FileInputStream file = null;
  26. String templatePath;
  27. if (host.contains("127.0.0.1") || host.contains("localhost"))
  28. templatePath = "C:\\Workspace\\BMP\\war\\ExcelTemplates\\"
  29. + filename;
  30. else
  31. templatePath = "/usr/share/ExcelTemplates/" + filename;
  32. file = new FileInputStream(new File(templatePath));
  33. HSSFWorkbook hwb = null;
  34. try {
  35. hwb = new HSSFWorkbook(file);
  36. } catch (IOException e1) {
  37. e1.printStackTrace();
  38. }
  39. SummaryInformation summaryInfo = hwb.getSummaryInformation();
  40. summaryInfo.setAuthor("BMP-System");
  41. return hwb;
  42. }
  43. protected Map<String, CellStyle> createStyles(Workbook wb) {
  44. Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
  45. CellStyle style;
  46. Font titleFont = wb.createFont();
  47. titleFont.setFontHeightInPoints((short) 14);
  48. style = wb.createCellStyle();
  49. style.setAlignment(CellStyle.ALIGN_LEFT);
  50. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  51. style.setFont(titleFont);
  52. styles.put("title", style);
  53. Font monthFont = wb.createFont();
  54. monthFont.setFontHeightInPoints((short) 12);
  55. monthFont.setColor(IndexedColors.WHITE.getIndex());
  56. style = wb.createCellStyle();
  57. style.setAlignment(CellStyle.ALIGN_CENTER);
  58. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
  59. style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
  60. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  61. style.setFont(monthFont);
  62. style.setWrapText(true);
  63. styles.put("header", style);
  64. style = wb.createCellStyle();
  65. style.setAlignment(CellStyle.ALIGN_CENTER);
  66. style.setWrapText(true);
  67. style.setBorderRight(CellStyle.BORDER_THIN);
  68. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  69. style.setBorderLeft(CellStyle.BORDER_THIN);
  70. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  71. style.setBorderTop(CellStyle.BORDER_THIN);
  72. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  73. style.setBorderBottom(CellStyle.BORDER_THIN);
  74. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  75. styles.put("cell", style);
  76. style = wb.createCellStyle();
  77. style.setAlignment(CellStyle.ALIGN_LEFT);
  78. style.setWrapText(true);
  79. style.setBorderRight(CellStyle.BORDER_THIN);
  80. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  81. style.setBorderLeft(CellStyle.BORDER_THIN);
  82. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  83. style.setBorderTop(CellStyle.BORDER_THIN);
  84. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  85. style.setBorderBottom(CellStyle.BORDER_THIN);
  86. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  87. styles.put("cell_left", style);
  88. style = wb.createCellStyle();
  89. style.setAlignment(CellStyle.ALIGN_LEFT);
  90. style.setWrapText(true);
  91. styles.put("simple_text", style);
  92. return styles;
  93. }
  94. protected void fillEmptyCells(HSSFRow currentRow, CellStyle style,
  95. int begin, int end) {
  96. for (int i = begin; i <= end; i++) {
  97. Cell cell = currentRow.createCell(i);
  98. cell.setCellStyle(style);
  99. }
  100. }
  101. protected void addHeaderRow(CellStyle style, HSSFSheet sheet, int rowIndex,
  102. String content) {
  103. HSSFRow headerRow = sheet.createRow(rowIndex);
  104. Cell titleCell = headerRow.createCell(0);
  105. titleCell.setCellValue(content);
  106. titleCell.setCellStyle(style);
  107. sheet.addMergedRegion(CellRangeAddress.valueOf("$A$" + (rowIndex + 1)
  108. + ":$H$" + (rowIndex + 1)));
  109. }
  110. @SuppressWarnings("deprecation")
  111. protected void setCellValue(HSSFSheet sheet, String cellAddres, String value) {
  112. CellReference cellReference = new CellReference(cellAddres);
  113. HSSFRow currentRow = sheet.getRow(cellReference.getRow());
  114. Cell currentCell = currentRow.getCell(cellReference.getCol());
  115. currentCell.setCellValue(value);
  116. }
  117. @SuppressWarnings("deprecation")
  118. protected void setCellValue(HSSFSheet sheet, String cellAddres, double value) {
  119. CellReference cellReference = new CellReference(cellAddres);
  120. HSSFRow currentRow = sheet.getRow(cellReference.getRow());
  121. Cell currentCell = currentRow.getCell(cellReference.getCol());
  122. currentCell.setCellValue(value);
  123. }
  124. protected void copyRow(HSSFWorkbook hwb, HSSFRow sourceRow,
  125. HSSFSheet sheet, int rowIndex) {
  126. HSSFRow newRow = sheet.createRow(rowIndex);
  127. newRow.setHeight(sourceRow.getHeight());
  128. for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
  129. Cell oldCell = sourceRow.getCell(i);
  130. Cell newCell = newRow.createCell(i);
  131. // If the old cell is null jump to next cell
  132. if (oldCell == null) {
  133. newCell = null;
  134. continue;
  135. }
  136. // Copy style from old cell and apply to new cell
  137. CellStyle newCellStyle = hwb.createCellStyle();
  138. newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
  139. ;
  140. newCell.setCellStyle(newCellStyle);
  141. if (oldCell.getCellComment() != null) {
  142. newCell.setCellComment(oldCell.getCellComment());
  143. }
  144. if (oldCell.getHyperlink() != null) {
  145. newCell.setHyperlink(oldCell.getHyperlink());
  146. }
  147. newCell.setCellType(oldCell.getCellType());
  148. switch (oldCell.getCellType()) {
  149. case Cell.CELL_TYPE_BLANK:
  150. newCell.setCellValue(oldCell.getStringCellValue());
  151. break;
  152. case Cell.CELL_TYPE_BOOLEAN:
  153. newCell.setCellValue(oldCell.getBooleanCellValue());
  154. break;
  155. case Cell.CELL_TYPE_ERROR:
  156. newCell.setCellErrorValue(oldCell.getErrorCellValue());
  157. break;
  158. case Cell.CELL_TYPE_FORMULA:
  159. newCell.setCellFormula(oldCell.getCellFormula());
  160. break;
  161. case Cell.CELL_TYPE_NUMERIC:
  162. newCell.setCellValue(oldCell.getNumericCellValue());
  163. break;
  164. case Cell.CELL_TYPE_STRING:
  165. newCell.setCellValue(oldCell.getRichStringCellValue());
  166. break;
  167. }
  168. }
  169. for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
  170. CellRangeAddress cellRangeAddress = sheet.getMergedRegion(i);
  171. if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
  172. CellRangeAddress newCellRangeAddress = new CellRangeAddress(
  173. newRow.getRowNum(),
  174. (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress
  175. .getFirstRow())), cellRangeAddress
  176. .getFirstColumn(), cellRangeAddress
  177. .getLastColumn());
  178. sheet.addMergedRegion(newCellRangeAddress);
  179. }
  180. }
  181. }
  182. public String removeLinebreaksFromString(String str) {
  183. str = str.replace("\n", " ");
  184. str = str.replace("\r", " ");
  185. return str;
  186. }
  187. }