ExcelStyleUtil.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.ydtech.modules.ins.utils;
  2. import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
  3. import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
  4. import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
  5. import org.apache.poi.ss.usermodel.*;
  6. /**
  7. * excel样式
  8. */
  9. public class ExcelStyleUtil implements IExcelExportStyler {
  10. private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");
  11. // private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("0.00");
  12. private static final short FONT_SIZE_TEN = 9;
  13. private static final short FONT_SIZE_ELEVEN = 10;
  14. private static final short FONT_SIZE_TWELVE = 10;
  15. /**
  16. * 大标题样式
  17. */
  18. private CellStyle headerStyle;
  19. /**
  20. * 每列标题样式
  21. */
  22. private CellStyle titleStyle;
  23. /**
  24. * 数据行样式
  25. */
  26. private CellStyle styles;
  27. public ExcelStyleUtil(Workbook workbook) {
  28. this.init(workbook);
  29. }
  30. /**
  31. * 初始化样式
  32. *
  33. * @param workbook
  34. */
  35. private void init(Workbook workbook) {
  36. this.headerStyle = initHeaderStyle(workbook);
  37. this.titleStyle = initTitleStyle(workbook);
  38. this.styles = initStyles(workbook);
  39. }
  40. /**
  41. * 大标题样式
  42. *
  43. * @param color
  44. * @return
  45. */
  46. @Override
  47. public CellStyle getHeaderStyle(short color) {
  48. return headerStyle;
  49. }
  50. /**
  51. * 每列标题样式
  52. *
  53. * @param color
  54. * @return
  55. */
  56. @Override
  57. public CellStyle getTitleStyle(short color) {
  58. return titleStyle;
  59. }
  60. /**
  61. * 数据行样式
  62. *
  63. * @param parity 可以用来表示奇偶行
  64. * @param entity 数据内容
  65. * @return 样式
  66. */
  67. @Override
  68. public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
  69. return styles;
  70. }
  71. /**
  72. * 获取样式方法
  73. *
  74. * @param dataRow 数据行
  75. * @param obj 对象
  76. * @param data 数据
  77. */
  78. @Override
  79. public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
  80. return getStyles(true, entity);
  81. }
  82. /**
  83. * 模板使用的样式设置
  84. */
  85. @Override
  86. public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
  87. return null;
  88. }
  89. /**
  90. * 初始化--大标题样式
  91. *
  92. * @param workbook
  93. * @return
  94. */
  95. private CellStyle initHeaderStyle(Workbook workbook) {
  96. CellStyle style = getBaseCellStyle(workbook);
  97. style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
  98. return style;
  99. }
  100. /**
  101. * 初始化--每列标题样式
  102. *
  103. * @param workbook
  104. * @return
  105. */
  106. private CellStyle initTitleStyle(Workbook workbook) {
  107. CellStyle style = getBaseCellStyle(workbook);
  108. style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));
  109. //背景色
  110. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  111. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  112. return style;
  113. }
  114. /**
  115. * 初始化--数据行样式
  116. *
  117. * @param workbook
  118. * @return
  119. */
  120. private CellStyle initStyles(Workbook workbook) {
  121. CellStyle style = getBaseCellStyle(workbook);
  122. style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
  123. style.setDataFormat(STRING_FORMAT);
  124. return style;
  125. }
  126. /**
  127. * 基础样式
  128. *
  129. * @return
  130. */
  131. private CellStyle getBaseCellStyle(Workbook workbook) {
  132. CellStyle style = workbook.createCellStyle();
  133. //下边框
  134. style.setBorderBottom(BorderStyle.THIN);
  135. //左边框
  136. style.setBorderLeft(BorderStyle.THIN);
  137. //上边框
  138. style.setBorderTop(BorderStyle.THIN);
  139. //右边框
  140. style.setBorderRight(BorderStyle.THIN);
  141. //水平居中
  142. style.setAlignment(HorizontalAlignment.CENTER);
  143. //上下居中
  144. style.setVerticalAlignment(VerticalAlignment.CENTER);
  145. //设置自动换行
  146. style.setWrapText(true);
  147. return style;
  148. }
  149. /**
  150. * 字体样式
  151. *
  152. * @param size 字体大小
  153. * @param isBold 是否加粗
  154. * @return
  155. */
  156. private Font getFont(Workbook workbook, short size, boolean isBold) {
  157. Font font = workbook.createFont();
  158. //字体样式
  159. font.setFontName("宋体");
  160. //是否加粗
  161. font.setBold(isBold);
  162. //字体大小
  163. font.setFontHeightInPoints(size);
  164. return font;
  165. }
  166. }