ExcelStyleLeftUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ExcelStyleLeftUtil 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 ExcelStyleLeftUtil(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. style.setAlignment(HorizontalAlignment.CENTER);
  99. return style;
  100. }
  101. /**
  102. * 初始化--每列标题样式
  103. *
  104. * @param workbook
  105. * @return
  106. */
  107. private CellStyle initTitleStyle(Workbook workbook) {
  108. CellStyle style = getBaseCellStyle(workbook);
  109. style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false));
  110. //背景色
  111. style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
  112. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  113. // CellRangeAddress region1 = new CellRangeAddress(0, 0, (short) 0, (short) 11); // 好像是合并单元格,没试过
  114. // workbook.getSheetAt(0).addMergedRegion(region1);
  115. return style;
  116. }
  117. /**
  118. * 初始化--数据行样式
  119. *
  120. * @param workbook
  121. * @return
  122. */
  123. private CellStyle initStyles(Workbook workbook) {
  124. CellStyle style = getBaseCellStyle(workbook);
  125. style.setFont(getFont(workbook, FONT_SIZE_TEN, false));
  126. style.setDataFormat(STRING_FORMAT);
  127. return style;
  128. }
  129. /**
  130. * 基础样式
  131. *
  132. * @return
  133. */
  134. private CellStyle getBaseCellStyle(Workbook workbook) {
  135. CellStyle style = workbook.createCellStyle();
  136. //下边框
  137. style.setBorderBottom(BorderStyle.THIN);
  138. //左边框
  139. style.setBorderLeft(BorderStyle.THIN);
  140. //上边框
  141. style.setBorderTop(BorderStyle.THIN);
  142. //右边框
  143. style.setBorderRight(BorderStyle.THIN);
  144. //水平居中
  145. style.setAlignment(HorizontalAlignment.LEFT);
  146. //上下居中
  147. style.setVerticalAlignment(VerticalAlignment.CENTER);
  148. //设置自动换行
  149. style.setWrapText(true);
  150. return style;
  151. }
  152. /**
  153. * 字体样式
  154. *
  155. * @param size 字体大小
  156. * @param isBold 是否加粗
  157. * @return
  158. */
  159. private Font getFont(Workbook workbook, short size, boolean isBold) {
  160. Font font = workbook.createFont();
  161. //字体样式
  162. font.setFontName("宋体");
  163. //是否加粗
  164. font.setBold(isBold);
  165. //字体大小
  166. font.setFontHeightInPoints(size);
  167. return font;
  168. }
  169. }