PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/_deactivated/cdi-tablemodel/src/main/java/org/rapidpm/data/table/export/Table2XLSX.java

https://gitlab.com/jforge/Modules
Java | 125 lines | 86 code | 13 blank | 26 comment | 13 complexity | cd3e2af20f477f5c5f158328b8da4f98 MD5 | raw file
  1. /*
  2. * Copyright [2013] [www.rapidpm.org / Sven Ruppert (sven.ruppert@rapidpm.org)]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.rapidpm.data.table.export;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import org.apache.log4j.Logger;
  22. import org.apache.poi.ss.usermodel.CellStyle;
  23. import org.apache.poi.ss.usermodel.Font;
  24. import org.apache.poi.ss.usermodel.Hyperlink;
  25. import org.apache.poi.ss.usermodel.IndexedColors;
  26. import org.apache.poi.xssf.usermodel.XSSFCell;
  27. import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
  28. import org.apache.poi.xssf.usermodel.XSSFHyperlink;
  29. import org.apache.poi.xssf.usermodel.XSSFRow;
  30. import org.apache.poi.xssf.usermodel.XSSFSheet;
  31. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  32. import org.rapidpm.data.table.Cell;
  33. import org.rapidpm.data.table.CellTypeEnum;
  34. import org.rapidpm.data.table.ColumnInformation;
  35. import org.rapidpm.data.table.Row;
  36. import org.rapidpm.data.table.Table;
  37. /**
  38. * Sven Ruppert - www.svenruppert.de
  39. *
  40. * @author Sven Ruppert
  41. * @version 0.1
  42. * <p></p>
  43. * This Source Code is part of the www.svenruppert.de project.
  44. * please contact sven.ruppert@me.com
  45. * @since 16.06.2010
  46. * Time: 10:48:51
  47. */
  48. public class Table2XLSX implements TableExport<ByteArrayOutputStream> {
  49. private static final Logger logger = Logger.getLogger(Table2XLSX.class);
  50. @Override
  51. public ByteArrayOutputStream export(final Table table) {
  52. final XSSFWorkbook workbook = new XSSFWorkbook();
  53. final XSSFSheet xssfSheet = workbook.createSheet(table.getTableName());
  54. final Collection<ColumnInformation> infoList = table.getColumnInfoList();
  55. final XSSFRow xssfHeaderRow = xssfSheet.createRow(0);
  56. for (final ColumnInformation information : infoList) {
  57. if (information.isExportable()) {
  58. final XSSFCell xssfCell = xssfHeaderRow.createCell(information.getSpaltenNr());
  59. xssfCell.setCellValue(information.getSpaltenName());
  60. xssfCell.setCellType(XSSFCell.CELL_TYPE_STRING);
  61. } else {
  62. if (logger.isDebugEnabled()) {
  63. logger.debug("ColInfo not exportable " + information.getSpaltenName());
  64. }
  65. }
  66. }
  67. final List<Row> tableRowList = table.getRowList();
  68. for (final Row row : tableRowList) {
  69. final XSSFRow xssfRow = xssfSheet.createRow(row.getRowNr() + 1);
  70. final List<Cell> cellList = row.getCells();
  71. for (final Cell cell : cellList) {
  72. if (cell.getColInfo().isExportable()) {
  73. final XSSFCell xssfCell = xssfRow.createCell(cell.getColInfo().getSpaltenNr());
  74. final CellTypeEnum cellType = cell.getCellType();
  75. if (cellType.equals(CellTypeEnum.RawData)) {
  76. xssfCell.setCellValue(cell.getFormattedValue());
  77. xssfCell.setCellType(XSSFCell.CELL_TYPE_STRING); //JIRA MOD-32 CellType in Abhängigkeit der ValueClass z.B. Number
  78. } else if (cellType.equals(CellTypeEnum.RawLink)) {
  79. final XSSFCreationHelper helper = workbook.getCreationHelper();
  80. final XSSFHyperlink xssfHyperlink = helper.createHyperlink(Hyperlink.LINK_URL);
  81. xssfHyperlink.setAddress(cell.getFormattedValue());
  82. xssfHyperlink.setLabel(cell.getLabel());
  83. xssfCell.setCellValue(cell.getLabel());
  84. xssfCell.setHyperlink(xssfHyperlink);
  85. final CellStyle hlink_style = createHyperLinkStyle(workbook);
  86. xssfCell.setCellStyle(hlink_style);
  87. } else {
  88. }
  89. } else {
  90. if (logger.isDebugEnabled()) {
  91. logger.debug("Cell not exportable ");
  92. }
  93. }
  94. }
  95. }
  96. final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  97. try {
  98. workbook.write(outputStream);
  99. } catch (IOException e) {
  100. logger.error(e);
  101. }
  102. return outputStream;
  103. }
  104. private CellStyle createHyperLinkStyle(final XSSFWorkbook workbook) {
  105. final CellStyle hlink_style = workbook.createCellStyle();
  106. final Font hlink_font = workbook.createFont();
  107. hlink_font.setUnderline(Font.U_SINGLE);
  108. hlink_font.setColor(IndexedColors.BLUE.getIndex());
  109. hlink_style.setFont(hlink_font);
  110. return hlink_style;
  111. }
  112. }