ExportUtils.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.minpay.common.util;
  2. import org.apache.poi.xssf.usermodel.*;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. /**
  7. *
  8. */
  9. public class ExportUtils {
  10. public final static String COLUMNS_SEPARATOR = "#";//分割符
  11. /**
  12. * 描述列样式
  13. * @param wb
  14. * @return
  15. */
  16. public static XSSFCellStyle getDescCellStyle(XSSFWorkbook wb){
  17. // 生成一个样式
  18. XSSFCellStyle style = wb.createCellStyle();
  19. // 设置这些样式
  20. style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  21. style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
  22. style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
  23. style.setBorderRight(XSSFCellStyle.BORDER_THIN);
  24. style.setBorderTop(XSSFCellStyle.BORDER_THIN);
  25. style.setAlignment(XSSFCellStyle.ALIGN_LEFT);
  26. // 生成一个字体
  27. XSSFFont font = wb.createFont();
  28. font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
  29. font.setFontName("宋体");
  30. font.setColor(new XSSFColor(java.awt.Color.RED));
  31. font.setFontHeightInPoints((short) 12);
  32. // 把字体应用到当前的样式
  33. style.setFont(font);
  34. return style;
  35. }
  36. /**
  37. * 标题样式
  38. * @param wb
  39. * @return
  40. */
  41. public static XSSFCellStyle getTitleCellStyle(XSSFWorkbook wb){
  42. // 生成一个样式
  43. XSSFCellStyle style = wb.createCellStyle();
  44. // 设置这些样式
  45. style.setFillForegroundColor(new XSSFColor(java.awt.Color.gray));
  46. style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  47. style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
  48. style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
  49. style.setBorderRight(XSSFCellStyle.BORDER_THIN);
  50. style.setBorderTop(XSSFCellStyle.BORDER_THIN);
  51. style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
  52. // 生成一个字体
  53. XSSFFont font = wb.createFont();
  54. font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
  55. font.setFontName("宋体");
  56. font.setColor(new XSSFColor(java.awt.Color.BLACK));
  57. font.setFontHeightInPoints((short) 11);
  58. // 把字体应用到当前的样式
  59. style.setFont(font);
  60. return style;
  61. }
  62. /**
  63. * 正文样式
  64. * @param wb
  65. * @return
  66. */
  67. public static XSSFCellStyle getTextCellStyle(XSSFWorkbook wb) {
  68. // 生成并设置另一个样式
  69. XSSFCellStyle style2 = wb.createCellStyle();
  70. style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE));
  71. style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
  72. style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
  73. style2.setBorderLeft(XSSFCellStyle.BORDER_THIN);
  74. style2.setBorderRight(XSSFCellStyle.BORDER_THIN);
  75. style2.setBorderTop(XSSFCellStyle.BORDER_THIN);
  76. style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
  77. style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
  78. // 生成另一个字体
  79. XSSFFont font2 = wb.createFont();
  80. font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL);
  81. // 把字体应用到当前的样式
  82. style2.setFont(font2);
  83. return style2;
  84. }
  85. public static String[] autocomplete(String[] titleArray, int manyTitle) {
  86. for(int i = 0; i< titleArray.length; i++) {
  87. titleArray[i] = completeStr(titleArray[i], manyTitle - titleArray[i].split(COLUMNS_SEPARATOR).length);
  88. }
  89. return titleArray;
  90. }
  91. private static String completeStr(String col, int count) {
  92. if (count == 0) {
  93. return col;
  94. }
  95. col = col.split(COLUMNS_SEPARATOR)[0] + COLUMNS_SEPARATOR + col;
  96. return completeStr(col, --count);
  97. }
  98. /**
  99. * 表头数据合并
  100. * @param sheet
  101. * @param coltitleIndex 列数
  102. * @param colTitles 标题行
  103. * @param num 行索引
  104. */
  105. public static void titleMerge(XSSFSheet sheet, int coltitleIndex, String[] colTitles, int num) {
  106. for(int i = 0; i < num; i++) {
  107. String preVal = "";
  108. int columnIndex = 0;
  109. for(int j = 0; j < colTitles.length; j++) {
  110. columnIndex++;
  111. preVal = colTitles[j].split(COLUMNS_SEPARATOR)[i];
  112. String columnVal = colTitles[j + 1].split(COLUMNS_SEPARATOR)[i];
  113. if(!"".equals(columnVal) && !preVal.equals(columnVal)) {
  114. List<Integer> list = new ArrayList<Integer>();
  115. for(int m = j - columnIndex + 1; m <= j; m++) {
  116. int bottom = 0;
  117. for(int k = i + 1; k <= num; k++) {
  118. String nextRowVal = "";
  119. try {
  120. nextRowVal = colTitles[m].split(COLUMNS_SEPARATOR)[k];
  121. } catch (Exception e) {
  122. list.add(bottom);
  123. }
  124. if(!"".equals(nextRowVal) && !preVal.equals(nextRowVal)) {
  125. list.add(bottom);
  126. break;
  127. }
  128. bottom++;
  129. }
  130. }
  131. Collections.sort(list); //从小到大
  132. columnIndex = 0;
  133. }
  134. preVal = columnVal;
  135. }
  136. }
  137. }
  138. }