ExcelExportStyler.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package jnpf.excel;
  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. import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
  7. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  8. import org.apache.poi.xssf.usermodel.XSSFColor;
  9. public class ExcelExportStyler implements IExcelExportStyler {
  10. private static final short FONT_SIZE_TWELVE = 12;
  11. private Workbook workbook;
  12. private CellStyle headStyle;
  13. private XSSFCellStyle cellStyle;
  14. public ExcelExportStyler(Workbook workbook) {
  15. this.workbook = workbook;
  16. this.headStyle = getBaseCellStyle();
  17. this.cellStyle = (XSSFCellStyle) workbook.createCellStyle();
  18. }
  19. @Override
  20. public CellStyle getHeaderStyle(short color) {
  21. CellStyle style = this.headStyle;
  22. return style;
  23. }
  24. @Override
  25. public CellStyle getTitleStyle(short color) {
  26. XSSFCellStyle style = (XSSFCellStyle) this.headStyle;
  27. style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true));
  28. // 自定义背景颜色
  29. byte[] rgb = new byte[]{(byte) 221, (byte) 220, (byte) 223}; // RGB值
  30. XSSFColor customColor = new XSSFColor(rgb, new DefaultIndexedColorMap());
  31. style.setFillForegroundColor(customColor);
  32. //设置填充模式为实心填充
  33. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  34. return style;
  35. }
  36. public void setBlackBorder(CellStyle style) {
  37. // 设置边框样式为细线
  38. style.setBorderBottom(BorderStyle.THIN);
  39. style.setBorderLeft(BorderStyle.THIN);
  40. style.setBorderTop(BorderStyle.THIN);
  41. style.setBorderRight(BorderStyle.THIN);
  42. // 设置单元格颜色
  43. style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
  44. style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
  45. style.setRightBorderColor(IndexedColors.BLACK.getIndex());
  46. style.setTopBorderColor(IndexedColors.BLACK.getIndex());
  47. // 单元格内容水平居中
  48. style.setAlignment(HorizontalAlignment.LEFT);
  49. // 单元格内容垂直居中
  50. style.setVerticalAlignment(VerticalAlignment.CENTER);
  51. // 单元格内的文本超出单元格宽度时自动换行
  52. style.setWrapText(true);
  53. }
  54. @Override
  55. public CellStyle getStyles(boolean parity, ExcelExportEntity entity) {
  56. XSSFCellStyle style = cellStyle;
  57. this.setBlackBorder(style);
  58. // 单元格内容水平居中
  59. style.setAlignment(HorizontalAlignment.LEFT);
  60. // 单元格内容垂直居中
  61. style.setVerticalAlignment(VerticalAlignment.CENTER);
  62. // 单元格内的文本超出单元格宽度时自动换行
  63. style.setWrapText(true);
  64. return style;
  65. }
  66. @Override
  67. public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) {
  68. return getStyles(true, entity);
  69. }
  70. @Override
  71. public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
  72. return null;
  73. }
  74. /**
  75. * 表头单元格样式
  76. *
  77. * @return
  78. */
  79. private CellStyle getBaseCellStyle() {
  80. CellStyle style = workbook.createCellStyle();
  81. this.setBlackBorder(style);
  82. // 单元格内容水平居中
  83. style.setAlignment(HorizontalAlignment.CENTER);
  84. return style;
  85. }
  86. /**
  87. * 字体修改
  88. *
  89. * @param size 字号
  90. * @param isBold 加粗
  91. * @return
  92. */
  93. private Font getFont(Workbook workbook, short size, boolean isBold) {
  94. Font font = workbook.createFont();
  95. font.setFontName("宋体");
  96. font.setBold(isBold);
  97. font.setFontHeightInPoints(size);
  98. return font;
  99. }
  100. }