PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/org.dexpi.xpview.model/src/org/dexpi/xpview/model/report/ExcelReport.java

https://github.com/dunkelrot/xpview
Java | 119 lines | 93 code | 18 blank | 8 comment | 12 complexity | 37dd7e3ab6bb7822c0f6372c753d465c MD5 | raw file
  1. package org.dexpi.xpview.model.report;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.List;
  7. import org.apache.log4j.Logger;
  8. import org.apache.poi.ss.usermodel.Cell;
  9. import org.apache.poi.ss.usermodel.CellStyle;
  10. import org.apache.poi.ss.usermodel.Font;
  11. import org.apache.poi.ss.usermodel.IndexedColors;
  12. import org.apache.poi.ss.usermodel.Row;
  13. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  14. import org.apache.poi.xssf.usermodel.XSSFSheet;
  15. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16. import org.dexpi.xpview.model.PlantItem;
  17. import org.dexpi.xpview.model.Property;
  18. import org.dexpi.xpview.model.Workspace;
  19. /**
  20. * Very simple excel reporting.
  21. *
  22. * @author Arndt Teinert
  23. *
  24. */
  25. public class ExcelReport {
  26. private static Logger log = Logger.getLogger(ExcelReport.class);
  27. protected ReportPropertyData propertyData;
  28. public ExcelReport(ReportPropertyData propertyData) {
  29. this.propertyData = propertyData;
  30. }
  31. public void report(File outputFile) throws IOException,
  32. FileNotFoundException {
  33. XSSFWorkbook wb = new XSSFWorkbook();
  34. FileOutputStream fileOut = new FileOutputStream(outputFile);
  35. XSSFSheet dataSheet = wb.createSheet("Data");
  36. int numColumns = createHeaderRow(dataSheet);
  37. createDataRows(dataSheet);
  38. for (int ii = 0; ii < numColumns; ii++) {
  39. dataSheet.autoSizeColumn(ii);
  40. }
  41. wb.write(fileOut);
  42. fileOut.close();
  43. }
  44. protected int createHeaderRow(XSSFSheet dataSheet) {
  45. Row row = dataSheet.createRow((short) 0);
  46. int cellIndex = 0;
  47. // Create a new font and alter it.
  48. Font font = dataSheet.getWorkbook().createFont();
  49. font.setFontHeightInPoints((short) 14);
  50. font.setFontName("Calibri");
  51. font.setBoldweight((short) 1);
  52. // Fonts are set into a style so create a new one to use.
  53. XSSFCellStyle style = dataSheet.getWorkbook().createCellStyle();
  54. style.setFont(font);
  55. style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
  56. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  57. for (ReportPropertyList propertyList : propertyData) {
  58. for (ReportProperty property : propertyList) {
  59. if (property.isChecked()) {
  60. Cell cell = row.createCell(cellIndex);
  61. cell.setCellValue(property.getProperty().getName());
  62. cell.setCellStyle(style);
  63. cellIndex += 1;
  64. }
  65. }
  66. }
  67. return cellIndex;
  68. }
  69. protected void createDataRows(XSSFSheet dataSheet) {
  70. int rowIndex = 1;
  71. List<PlantItem> plantItems = Workspace.getInstance().getPlant()
  72. .getAllPlantItems();
  73. for (PlantItem plantItem : plantItems) {
  74. if (propertyData.plantItemType.isAssignableFrom(plantItem.getClass())) {
  75. Row row = dataSheet.createRow((short) rowIndex);
  76. int cellIndex = 0;
  77. for (ReportPropertyList propertyList : propertyData) {
  78. for (ReportProperty reportProperty : propertyList) {
  79. if (reportProperty.isChecked()) {
  80. Property property = plantItem.getPropertyData()
  81. .getProperty(
  82. reportProperty.getProperty()
  83. .getName());
  84. if (property != null) {
  85. row.createCell(cellIndex).setCellValue(
  86. property.getValue());
  87. } else {
  88. log.info("Property "
  89. + reportProperty.getProperty()
  90. .getName()
  91. + " not found during export.");
  92. }
  93. cellIndex += 1;
  94. }
  95. }
  96. }
  97. rowIndex += 1;
  98. }
  99. }
  100. }
  101. }