PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ru/green/report/Report.java

https://github.com/00Green27/DBAnalyzer
Java | 207 lines | 103 code | 21 blank | 83 comment | 3 complexity | 5cf6ca84c99964520276fc5ec684b04d MD5 | raw file
  1. /*
  2. * Copyright 2011 00Green27 <00Green27@gmail.com>
  3. *
  4. * This file is part of mDBExplorer.
  5. *
  6. * This code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this work. If not, see http://www.gnu.org/licenses/.
  18. */
  19. package ru.green.report;
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import org.apache.poi.hssf.usermodel.HSSFCell;
  24. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  25. import org.apache.poi.hssf.usermodel.HSSFFont;
  26. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  27. import org.apache.poi.hssf.usermodel.HSSFRow;
  28. import org.apache.poi.hssf.usermodel.HSSFSheet;
  29. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  30. import org.apache.poi.hssf.util.CellRangeAddress;
  31. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  32. import org.apache.poi.ss.usermodel.CellStyle;
  33. import org.apache.poi.ss.usermodel.Font;
  34. /**
  35. * Класс создает xls-файл с выгруженной информацией
  36. *
  37. * @author ehd
  38. */
  39. public class Report {
  40. private HSSFWorkbook wb;
  41. private HSSFCell cell;
  42. private HSSFCellStyle cellStyle;
  43. private HSSFCellStyle captionStyle;
  44. private HSSFSheet sheet;
  45. private HSSFFont font;
  46. /**
  47. * Прочитать шаблон.
  48. *
  49. * @throws IOException
  50. * Ошибки чтения файла
  51. */
  52. public void readTemplate() throws IOException {
  53. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
  54. "etc/grid.xlt"));
  55. wb = new HSSFWorkbook(fs);
  56. sheet = wb.getSheetAt(0);
  57. cellStyle = wb.createCellStyle();
  58. captionStyle = wb.createCellStyle();
  59. font = wb.createFont();
  60. createCaptionStyle();
  61. createCellStyle();
  62. }
  63. /**
  64. * Метод предназначен для печати строки в документ с использованием
  65. * оформления.
  66. *
  67. * @param rowNumber
  68. * Номер строки.
  69. * @param cellNumber
  70. * Номер ячейки.
  71. * @param cellValue
  72. * Значение ячейки.
  73. */
  74. public void setCell(int rowNumber, int cellNumber, String cellValue) {
  75. HSSFRow row = createRow(rowNumber);
  76. cell = row.createCell(cellNumber);
  77. cell.setCellStyle(cellStyle);
  78. cell.setCellValue(new HSSFRichTextString(cellValue));
  79. }
  80. /**
  81. * Метод предназначен для печати числа в документ с использованием
  82. * оформления.
  83. *
  84. * @param rowNumber
  85. * Номер строки.
  86. * @param cellNumber
  87. * Номер ячейки.
  88. * @param cellValue
  89. * Значение ячейки.
  90. */
  91. public void setNumericCell(int rowNumber, int cellNumber, double cellValue) {
  92. HSSFRow row = createRow(rowNumber);
  93. cell = row.createCell(cellNumber);
  94. cell.setCellStyle(cellStyle);
  95. cell.setCellValue(cellValue);
  96. }
  97. public void setFormulaCell(int rowNumber, int cellNumber, String formula) {
  98. HSSFRow row = createRow(rowNumber);
  99. cell = row.createCell(cellNumber);
  100. cell.setCellStyle(cellStyle);
  101. cell.setCellFormula(formula);
  102. }
  103. /**
  104. * Метод предназначен для печати числа в документ с использованием
  105. * оформления.
  106. *
  107. * @param rowNumber
  108. * Номер строки.
  109. * @param cellNumber
  110. * Номер ячейки.
  111. * @param cellValue
  112. * Значение ячейки.
  113. */
  114. public void setCell(int rowNumber, int cellNumber, int cellValue) {
  115. HSSFRow row = createRow(rowNumber);
  116. cell = row.createCell(cellNumber);
  117. cell.setCellStyle(cellStyle);
  118. cell.setCellValue(cellValue);
  119. }
  120. /**
  121. * Метод предназначен для печати строки в документ.
  122. *
  123. * @param rowNumber
  124. * Номер строки.
  125. * @param cellNumber
  126. * Номер ячейки.
  127. * @param cellValue
  128. * Значение ячейки.
  129. */
  130. public void setMiscCell(int rowNumber, int cellNumber, String cellValue) {
  131. HSSFRow row = createRow(rowNumber);
  132. cell = row.createCell(cellNumber);
  133. cell.setCellValue(new HSSFRichTextString(cellValue));
  134. }
  135. public void setCaptionCell(int rowNumber, int cellNumber, String cellValue) {
  136. HSSFRow row = createRow(rowNumber);
  137. cell = row.createCell(cellNumber);
  138. cell.setCellStyle(captionStyle);
  139. cell.setCellValue(new HSSFRichTextString(cellValue));
  140. }
  141. private HSSFRow createRow(int rowNumber) {
  142. HSSFRow row = null;
  143. if (sheet.getRow(rowNumber) == null)
  144. row = sheet.createRow(rowNumber);
  145. else
  146. row = sheet.getRow(rowNumber);
  147. return row;
  148. }
  149. /**
  150. * Метод формирует стиль для ячейки.
  151. */
  152. private void createCellStyle() {
  153. cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
  154. cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
  155. cellStyle.setBorderRight(CellStyle.BORDER_THIN);
  156. }
  157. private void createCaptionStyle() {
  158. captionStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
  159. captionStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
  160. captionStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
  161. captionStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
  162. captionStyle.setAlignment(CellStyle.ALIGN_CENTER);
  163. captionStyle.setWrapText(true);
  164. font.setBoldweight(Font.BOLDWEIGHT_BOLD);
  165. captionStyle.setFont(font);
  166. }
  167. /**
  168. * Записать шаблон в файл.
  169. *
  170. * @param file
  171. * Имя файла.
  172. * @throws IOException
  173. * Проблемы с записью файла.
  174. */
  175. public void write(String file) throws IOException {
  176. for (int i = 0; i < 256; i++)
  177. sheet.autoSizeColumn((short) i);
  178. FileOutputStream output = new FileOutputStream(file);
  179. wb.write(output);
  180. output.flush();
  181. output.close();
  182. }
  183. public void addMergedRegion(int lastCol) {
  184. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, lastCol - 1));
  185. }
  186. }