PageRenderTime 313ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/panemu/tiwulfx/common/ExportToExcel.java

https://bitbucket.org/panemu/tiwulfx
Java | 234 lines | 187 code | 22 blank | 25 comment | 31 complexity | ba097e104ce90c1cdb8069d9dc85850d MD5 | raw file
  1. package com.panemu.tiwulfx.common;
  2. import com.panemu.tiwulfx.table.BaseColumn;
  3. import com.panemu.tiwulfx.table.TableControl;
  4. import java.io.FileOutputStream;
  5. import java.math.BigDecimal;
  6. import java.util.List;
  7. import javafx.scene.control.TableColumn;
  8. import org.apache.commons.beanutils.NestedNullException;
  9. import org.apache.commons.beanutils.PropertyUtils;
  10. import org.apache.poi.hssf.usermodel.HSSFCell;
  11. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  12. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  13. import org.apache.poi.hssf.usermodel.HSSFFont;
  14. import org.apache.poi.hssf.usermodel.HSSFRow;
  15. import org.apache.poi.hssf.usermodel.HSSFSheet;
  16. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  17. import org.apache.poi.ss.util.CellRangeAddress;
  18. public class ExportToExcel<T> {
  19. private HSSFWorkbook wb;
  20. protected HSSFCellStyle csText;
  21. protected HSSFCellStyle csHeader;
  22. private HSSFCellStyle csTitle;
  23. protected HSSFCellStyle csIntNum;
  24. private HSSFCellStyle csDoubleNum;
  25. /**
  26. *
  27. * @param sheet
  28. * @param header
  29. * @param rowIdx
  30. * @param clmIdx
  31. * @param offset [x,y] where x = rowSpan (headerDepth) . y = colSpan. 0 means
  32. * no span
  33. * @return
  34. */
  35. private int[] createHeader(HSSFSheet sheet, List<TableColumn<T, ?>> header, int rowIdx, int clmIdx, int[] offset) {
  36. HSSFRow row = sheet.createRow(rowIdx);
  37. int nextCellOffset = clmIdx;
  38. int columnOffset = 0;
  39. int rowOffset = offset[0];
  40. for (int i = 0; i < header.size(); i++) {
  41. TableColumn column = header.get(i);
  42. if ((!(column instanceof BaseColumn) && column.getColumns().isEmpty()) || (!column.isVisible())) {
  43. /**
  44. * Skip column that is not BaseColumn and doesn't have inner columns
  45. */
  46. nextCellOffset--;
  47. continue;
  48. }
  49. int[] headerOffset = new int[]{offset[0], offset[1]};
  50. HSSFCell cell = row.createCell(i + nextCellOffset);
  51. cell.setCellValue(column.getText());
  52. if (!column.getColumns().isEmpty()) {
  53. rowOffset--;
  54. headerOffset = createHeader(sheet, column.getColumns(), rowIdx + 1, i + nextCellOffset, new int[]{headerOffset[0] - 1, headerOffset[1]});
  55. }
  56. columnOffset = columnOffset + headerOffset[1];
  57. sheet.addMergedRegion(new CellRangeAddress(rowIdx, rowIdx + headerOffset[0], i + nextCellOffset, i + headerOffset[1] + nextCellOffset));
  58. cell.setCellStyle(csHeader);
  59. if (!column.getColumns().isEmpty()) {
  60. nextCellOffset = nextCellOffset + headerOffset[1];
  61. }
  62. }
  63. return new int[]{rowOffset, offset[1] + header.size() - 1 + columnOffset};
  64. }
  65. private int getHeaderDepth(TableColumn<T, ?> column, int depth) {
  66. if (column.getColumns().isEmpty()) {
  67. return depth;
  68. }
  69. int result = depth;
  70. for (TableColumn clm : column.getColumns()) {
  71. int newDepth = getHeaderDepth(clm, depth + 1);
  72. result = Math.max(result, newDepth);
  73. }
  74. return result;
  75. }
  76. public void export(String title, String targetFile, TableControl<T> tableControl, List<T> data, List<Double> columnWidths) throws Exception {
  77. int headerDepth = 1;
  78. List<TableColumn<T, ?>> lstColumn = tableControl.getTableView().getColumns();
  79. for (TableColumn column : lstColumn) {
  80. int depth = getHeaderDepth(column, 1);
  81. headerDepth = Math.max(depth, headerDepth);
  82. }
  83. // create a new workbook
  84. wb = new HSSFWorkbook();
  85. prepareStyle();
  86. // create a new sheet
  87. HSSFSheet sheet = wb.createSheet();
  88. int rowIdx = 0;
  89. HSSFRow row = sheet.createRow(rowIdx);
  90. // set column width
  91. for (short i = 0; i < columnWidths.size(); i++) {
  92. sheet.setColumnWidth(i, (int) (258 / 8 * columnWidths.get(i)));
  93. }
  94. HSSFCell cell = row.createCell(0);
  95. cell.setCellStyle(csTitle);
  96. cell.setCellValue(title);
  97. row.setHeight((short) (row.getHeight() * 3));
  98. rowIdx++;
  99. rowIdx++;
  100. createHeader(sheet, lstColumn, rowIdx, 0, new int[]{headerDepth - 1, 0});
  101. List<TableColumn<T, ?>> lstLeafColumn = tableControl.getLeafColumns();
  102. if (headerDepth > 1) {
  103. row = sheet.getRow(rowIdx + headerDepth - 1);
  104. int i = 0;
  105. for (TableColumn column : lstLeafColumn) {
  106. if (column.isVisible() && column instanceof BaseColumn) {
  107. sheet.setColumnWidth(i, (int) (258 / 8 * column.getPrefWidth()));
  108. cell = row.getCell(i);
  109. if (cell == null) {
  110. cell = row.createCell(i);
  111. }
  112. cell.setCellStyle(csHeader);
  113. i++;
  114. }
  115. }
  116. }
  117. rowIdx = rowIdx + headerDepth;
  118. Object value;
  119. for (int i = 0; i < data.size(); i++) {
  120. row = sheet.createRow(rowIdx);
  121. int j = 0;
  122. for (TableColumn column : lstLeafColumn) {
  123. if (column instanceof BaseColumn && column.isVisible()) {
  124. BaseColumn baseColumn = (BaseColumn) column;
  125. String propertyName = baseColumn.getPropertyName();
  126. cell = row.createCell(j);
  127. if (propertyName.contains(".")) {
  128. try {
  129. value = PropertyUtils.getNestedProperty(data.get(i), propertyName);
  130. } catch (NestedNullException ex) {
  131. value = null;
  132. }
  133. } else {
  134. value = PropertyUtils.getSimpleProperty(data.get(i), propertyName);
  135. }
  136. if (value instanceof Long || value instanceof Integer) {
  137. cell.setCellStyle(csIntNum);
  138. cell.setCellValue(Double.parseDouble(value.toString()));
  139. } else if (value instanceof Double || value instanceof Float) {
  140. cell.setCellStyle(csDoubleNum);
  141. cell.setCellValue(Double.parseDouble(value.toString()));
  142. } else if (value instanceof BigDecimal) {
  143. cell.setCellStyle(csDoubleNum);
  144. BigDecimal bd = (BigDecimal) value;
  145. cell.setCellValue(bd.doubleValue());
  146. } else {
  147. cell.setCellStyle(csText);
  148. cell.setCellValue(baseColumn.convertToString(value));
  149. }
  150. j++;
  151. }
  152. }
  153. rowIdx++;
  154. }
  155. writeFooter(sheet, rowIdx, data.size());
  156. FileOutputStream fileOut;
  157. if (!targetFile.endsWith(".xls")) {
  158. targetFile = targetFile + ".xls";
  159. }
  160. fileOut = new FileOutputStream(targetFile);
  161. wb.write(fileOut);
  162. fileOut.close();
  163. TiwulFXUtil.openFile(targetFile);
  164. }
  165. /**
  166. * Callback to write footer. This method is called after writing records to
  167. * spreadsheet, before saving the sheet to file and close the output stream.
  168. *
  169. * @param sheet
  170. * @param rowIdx
  171. * @param dataCount
  172. */
  173. protected void writeFooter(HSSFSheet sheet, int rowIdx, int dataCount) {
  174. }
  175. private void prepareStyle() {
  176. csText = wb.createCellStyle();
  177. csText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  178. csText.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  179. csText.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  180. csText.setBorderRight(HSSFCellStyle.BORDER_THIN);
  181. csText.setBorderTop(HSSFCellStyle.BORDER_THIN);
  182. csHeader = wb.createCellStyle();
  183. csHeader.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  184. csHeader.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
  185. csHeader.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
  186. csHeader.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
  187. csHeader.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
  188. csHeader.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  189. csHeader.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  190. HSSFFont f = wb.createFont();
  191. f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  192. csHeader.setFont(f);
  193. csTitle = wb.createCellStyle();
  194. csTitle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
  195. f = wb.createFont();
  196. f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  197. csTitle.setFont(f);
  198. // -------INTEGER
  199. csIntNum = wb.createCellStyle();
  200. csIntNum.setDataFormat(wb.createDataFormat().getFormat("#,##0"));
  201. csIntNum.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  202. csIntNum.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  203. csIntNum.setBorderRight(HSSFCellStyle.BORDER_THIN);
  204. csIntNum.setBorderTop(HSSFCellStyle.BORDER_THIN);
  205. csDoubleNum = wb.createCellStyle();
  206. csDoubleNum.setDataFormat(wb.createDataFormat().getFormat("#.##"));
  207. csDoubleNum.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  208. csDoubleNum.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  209. csDoubleNum.setBorderRight(HSSFCellStyle.BORDER_THIN);
  210. csDoubleNum.setBorderTop(HSSFCellStyle.BORDER_THIN);
  211. }
  212. }