• 利用easy excel 实现文件导出


    一.创建实体类

    1. package com.siact.product.jwp.module.report.dto;
    2. import com.alibaba.excel.annotation.ExcelProperty;
    3. import com.alibaba.excel.annotation.write.style.ColumnWidth;
    4. import com.alibaba.excel.annotation.write.style.ContentRowHeight;
    5. import com.alibaba.excel.annotation.write.style.HeadRowHeight;
    6. import lombok.AllArgsConstructor;
    7. import lombok.Data;
    8. import lombok.NoArgsConstructor;
    9. /**
    10. * @Description:
    11. */
    12. @Data
    13. @AllArgsConstructor
    14. @NoArgsConstructor
    15. @ContentRowHeight(30)
    16. @HeadRowHeight(25)
    17. @ColumnWidth(25)
    18. public class PatrolProcessDTO {
    19. @ExcelProperty(index = 0,value = "序号")
    20. private int orderNum;
    21. @ExcelProperty(index = 1,value = "工单名称")
    22. private String workOrderName;
    23. @ExcelProperty(index = 2,value = "巡检路线")
    24. private String patrolRoute;
    25. @ExcelProperty(index = 3,value = "维修技师")
    26. private String maintenanceUserName;
    27. @ExcelProperty(index = 4,value = "打点情况")
    28. private String content;
    29. @ExcelProperty(index = 5,value = "处理结果(工单状态)")
    30. private String workOrderSolveStatusName;
    31. @ExcelProperty(index = 6,value = "维修技师处理时长")
    32. private String maintenanceTime;
    33. @ExcelProperty(index = 7,value = "维修班长确认时长")
    34. private String confirmTime;
    35. @ExcelProperty(index = 8,value = "总用时")
    36. private String workOrderSpendTime;
    37. }

    二、引入合并策略

    1. package com.siact.product.jwp.module.report.service.impl.StyleUtils;
    2. import com.alibaba.excel.metadata.Head;
    3. import com.alibaba.excel.metadata.data.CellData;
    4. import com.alibaba.excel.metadata.data.WriteCellData;
    5. import com.alibaba.excel.write.handler.CellWriteHandler;
    6. import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
    7. import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
    8. import org.apache.poi.ss.usermodel.Cell;
    9. import org.apache.poi.ss.usermodel.CellType;
    10. import org.apache.poi.ss.usermodel.Row;
    11. import org.apache.poi.ss.usermodel.Sheet;
    12. import org.apache.poi.ss.util.CellRangeAddress;
    13. import java.util.List;
    14. /**
    15. * @Description:
    16. */
    17. public class ExcelFillCellMergeStrategy implements CellWriteHandler {
    18. // /*
    19. // * 要合并的列 (下表也是从0开始)
    20. // */
    21. // private Set mergeColumnIndex;
    22. // /*
    23. // * 用第几行开始合并 ,默认为1,因为第0行是标题,EasyExcel 的默认也是
    24. // */
    25. // private int mergeBeginRowIndex = 1;
    26. //
    27. // public ExcelFillCellMergeStrategy(Set mergeColumnIndex) {
    28. // this.mergeColumnIndex = mergeColumnIndex;
    29. // }
    30. //
    31. //
    32. // /*
    33. // * 在创建单元格之前调用
    34. // */
    35. // @Override
    36. // public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
    37. // }
    38. //
    39. // /*
    40. // * 在创建单元格之后调用
    41. // */
    42. // @Override
    43. // public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    44. //
    45. // }
    46. //
    47. //
    48. //
    49. // /*
    50. // * 在对单元格的所有操作完成后调用
    51. // */
    52. // @Override
    53. // public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
    54. // //当前行
    55. // int curRowIndex = cell.getRowIndex();
    56. // //当前列
    57. // int curColIndex = cell.getColumnIndex();
    58. //
    59. // if (curRowIndex > mergeBeginRowIndex) {
    60. // if (mergeColumnIndex.contains(curColIndex)) {
    61. // mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
    62. // }
    63. // }
    64. // }
    65. //
    66. // /**
    67. // * 当前单元格向上合并
    68. // *
    69. // * @param writeSheetHolder
    70. // * @param cell 当前单元格
    71. // * @param curRowIndex 当前行
    72. // * @param curColIndex 当前列
    73. // */
    74. // private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
    75. // //获取当前行的当前列的数据和上一行的当前列列数据,通过上一行数据是否相同进行合并
    76. // Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
    77. // Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
    78. // Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
    79. // // 比较当前行的第一列的单元格与上一行是否相同,相同合并当前单元格与上一行
    80. // if (curData.equals(preData)) {
    81. // Sheet sheet = writeSheetHolder.getSheet();
    82. // // 获取合并信息
    83. // List mergeRegions = sheet.getMergedRegions();
    84. // int size = mergeRegions.size();
    85. // CellRangeAddress cellRangeAddr;
    86. // if (size > 0) {
    87. // cellRangeAddr = mergeRegions.get(size - 1);
    88. // // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
    89. // if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
    90. // // 移除当前合并信息
    91. // sheet.removeMergedRegion(size - 1);
    92. // // 重新设置当前结束行
    93. // cellRangeAddr.setLastRow(curRowIndex);
    94. // } else {
    95. // // 若上一个单元格未被合并,则新增合并单元
    96. // cellRangeAddr = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
    97. // }
    98. // } else {
    99. // // 若上一个单元格未被合并,则新增合并单元
    100. // cellRangeAddr = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
    101. // }
    102. // // 添加新的合并信息
    103. // sheet.addMergedRegion(cellRangeAddr);
    104. // }
    105. // }
    106. // 需要从第几行开始合并,0表示第1行
    107. private final int mergeRowIndex;
    108. // 合并的哪些列,比如为4时,当前行id和上一行id相同则合并前五列
    109. private final int mergeColumnRegion;
    110. private final List ignoreColumn;
    111. public ExcelFillCellMergeStrategy(int mergeRowIndex, int mergeColumnRegion, List ignoreColumn) {
    112. this.mergeRowIndex = mergeRowIndex;
    113. this.mergeColumnRegion = mergeColumnRegion;
    114. this.ignoreColumn = ignoreColumn;
    115. }
    116. @Override
    117. public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    118. }
    119. // @Override
    120. // public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    121. // // 隐藏id列
    122. writeSheetHolder.getSheet().setColumnHidden(0, true);
    123. // }
    124. @Override
    125. public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    126. //当前行
    127. int curRowIndex = cell.getRowIndex();
    128. //当前列
    129. int curColIndex = cell.getColumnIndex();
    130. if (!ignoreColumn.contains(curColIndex) && curRowIndex > mergeRowIndex) {
    131. for (int i = 0; i < mergeColumnRegion; i++) {
    132. if (curColIndex <= mergeColumnRegion) {
    133. mergeWithPreviousRow(writeSheetHolder, cell, curRowIndex, curColIndex);
    134. break;
    135. }
    136. }
    137. }
    138. }
    139. /**
    140. * 当前单元格向上合并:当前行的id和上一行的id相同则合并前面(mergeColumnRegion+1)列
    141. *
    142. * @param cell 当前单元格
    143. * @param curRowIndex 当前行
    144. * @param curColIndex 当前列
    145. */
    146. private void mergeWithPreviousRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
    147. // 当前行的第一个Cell
    148. Cell curFirstCell = cell.getSheet().getRow(curRowIndex).getCell(0);
    149. Object curFirstData = curFirstCell.getCellType().getCode() == CellType.STRING.getCode() ? curFirstCell.getStringCellValue() : curFirstCell.getNumericCellValue();
    150. // 上一行的第一个Cell
    151. Cell preFirstCell = cell.getSheet().getRow(curRowIndex - 1).getCell(0);
    152. Object preFirstData = preFirstCell.getCellType().getCode() == CellType.STRING.getCode() ? preFirstCell.getStringCellValue() : preFirstCell.getNumericCellValue();
    153. // 当前cell
    154. Object data = cell.getCellType().getCode() == CellType.STRING.getCode() ? cell.getStringCellValue() : cell.getNumericCellValue();
    155. // 上面的Cell
    156. Cell upCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
    157. Object upData = upCell.getCellType().getCode() == CellType.STRING.getCode() ? upCell.getStringCellValue() : upCell.getNumericCellValue();
    158. // 当前行的id和上一行的id相同则合并前面(mergeColumnRegion+1)列 且上一行值相同
    159. if (curFirstData.equals(preFirstData) && data.equals(upData)) {
    160. Sheet sheet = writeSheetHolder.getSheet();
    161. List mergeRegions = sheet.getMergedRegions();
    162. boolean isMerged = false;
    163. for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
    164. CellRangeAddress cellRangeAddr = mergeRegions.get(i);
    165. // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
    166. if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
    167. sheet.removeMergedRegion(i);
    168. cellRangeAddr.setLastRow(curRowIndex);
    169. sheet.addMergedRegion(cellRangeAddr);
    170. isMerged = true;
    171. }
    172. }
    173. // 若上一个单元格未被合并,则新增合并单元
    174. if (!isMerged) {
    175. CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
    176. sheet.addMergedRegion(cellRangeAddress);
    177. }
    178. }
    179. }
    180. @Override
    181. public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
    182. }
    183. }

    三、引入其他样式

    1. package com.siact.product.jwp.module.report.service.impl.StyleUtils;
    2. import cn.hutool.core.util.StrUtil;
    3. import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
    4. import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
    5. import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
    6. import com.siact.product.jwp.module.report.service.impl.StyleUtils.CellStyleModel;
    7. import org.apache.poi.ss.usermodel.*;
    8. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
    9. import org.apache.poi.xssf.usermodel.XSSFColor;
    10. import org.apache.poi.xssf.usermodel.XSSFFont;
    11. import org.camunda.bpm.engine.impl.util.CollectionUtil;
    12. import org.springframework.stereotype.Component;
    13. import java.util.ArrayList;
    14. import java.util.List;
    15. import java.util.stream.Collectors;
    16. /**
    17. * @Author: 刘 旺
    18. * @CreateTime: 2023-08-04 17:50
    19. * @Description: 自定义单元格样式处理器(支持字体样式、背景颜色、边框样式、对齐方式、自动换行)
    20. */
    21. @Component
    22. public class CustomCellStyleHandler extends AbstractRowWriteHandler {
    23. /**
    24. * sheet页名称列表
    25. */
    26. private List sheetNameList;
    27. /**
    28. * 样式信息
    29. */
    30. private List cellStyleList = new ArrayList<>();
    31. /**
    32. * 自定义样式适配器构造方法
    33. *
    34. * @param cellStyleList 样式信息
    35. */
    36. public CustomCellStyleHandler(List cellStyleList) {
    37. if (CollectionUtil.isEmpty(cellStyleList)) {
    38. return;
    39. }
    40. cellStyleList = cellStyleList.stream().filter(x -> x != null
    41. //判断sheet名称KEY是否存在
    42. && StrUtil.isNotBlank(x.getSheetName())
    43. //字体样式
    44. //判断字体颜色KEY是否存在
    45. && (x.getFontColor() == null || x.getFontColor() instanceof IndexedColors
    46. || x.getFontColor() instanceof XSSFColor)
    47. //判断背景颜色KEY是否存在
    48. && (x.getBackgroundColor() == null || x.getBackgroundColor() instanceof IndexedColors
    49. || x.getBackgroundColor() instanceof XSSFColor)
    50. //边框样式
    51. // 判断上边框线条颜色KEY是否存在
    52. && (x.getTopBorderColor() == null || x.getTopBorderColor() instanceof IndexedColors
    53. || x.getTopBorderColor() instanceof XSSFColor)
    54. // 判断右边框线条颜色KEY是否存在
    55. && (x.getRightBorderColor() == null || x.getRightBorderColor() instanceof IndexedColors
    56. || x.getRightBorderColor() instanceof XSSFColor)
    57. // 判断下边框线条颜色KEY是否存在
    58. && (x.getBottomBorderColor() == null || x.getBottomBorderColor() instanceof IndexedColors
    59. || x.getBottomBorderColor() instanceof XSSFColor)
    60. // 判断左边框线条颜色KEY是否存在
    61. && (x.getLeftBorderColor() == null || x.getLeftBorderColor() instanceof IndexedColors
    62. || x.getLeftBorderColor() instanceof XSSFColor)
    63. ).collect(Collectors.toList());
    64. this.cellStyleList = cellStyleList;
    65. sheetNameList = this.cellStyleList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
    66. }
    67. @Override
    68. public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row
    69. , Integer relativeRowIndex, Boolean isHead) {
    70. Sheet sheet = writeSheetHolder.getSheet();
    71. //不需要添加样式,或者当前sheet页不需要添加样式
    72. if (cellStyleList == null || cellStyleList.size() <= 0 || sheetNameList.contains(sheet.getSheetName()) == false) {
    73. return;
    74. }
    75. //获取当前行的样式信息
    76. List rowCellStyleList = cellStyleList.stream().filter(x ->
    77. StrUtil.equals(x.getSheetName(), sheet.getSheetName()) && x.getRowIndex() == relativeRowIndex).collect(Collectors.toList());
    78. //该行不需要设置样式
    79. if (rowCellStyleList == null || rowCellStyleList.size() <= 0) {
    80. return;
    81. }
    82. for (CellStyleModel cellStyleModel : rowCellStyleList) {
    83. //设置单元格样式
    84. setCellStyle(cellStyleModel, row);
    85. }
    86. //删除已添加的样式信息
    87. cellStyleList.removeAll(rowCellStyleList);
    88. //重新获取要添加的sheet页姓名
    89. sheetNameList = cellStyleList.stream().map(x -> x.getSheetName()).distinct().collect(Collectors.toList());
    90. }
    91. /**
    92. * 给单元格设置样式
    93. *
    94. * @param cellStyleModel 样式信息
    95. * @param row 行对象
    96. */
    97. private void setCellStyle(CellStyleModel cellStyleModel, Row row) {
    98. //背景颜色
    99. Object backgroundColor = cellStyleModel.getBackgroundColor();
    100. //自动换行
    101. Boolean wrapText = cellStyleModel.getWrapText();
    102. //列索引
    103. int colIndex = cellStyleModel.getColIndex();
    104. //边框样式
    105. Cell cell = row.getCell(colIndex);
    106. if (cell == null) {
    107. cell = row.createCell(colIndex);
    108. }
    109. XSSFCellStyle style = (XSSFCellStyle) cell.getRow().getSheet().getWorkbook().createCellStyle();
    110. // 克隆出一个 style
    111. style.cloneStyleFrom(cell.getCellStyle());
    112. //设置背景颜色
    113. if (backgroundColor != null) {
    114. //使用IndexedColors定义的颜色
    115. if (backgroundColor instanceof IndexedColors) {
    116. style.setFillForegroundColor(((IndexedColors) backgroundColor).getIndex());
    117. }
    118. //使用自定义的RGB颜色
    119. else if (backgroundColor instanceof XSSFColor) {
    120. style.setFillForegroundColor((XSSFColor) backgroundColor);
    121. }
    122. style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    123. }
    124. //设置自动换行
    125. if (wrapText != null) {
    126. style.setWrapText(wrapText);
    127. }
    128. //设置字体样式
    129. setFontStyle(row, style, cellStyleModel);
    130. //设置边框样式
    131. setBorderStyle(style, cellStyleModel);
    132. //设置对齐方式
    133. setAlignmentStyle(style, cellStyleModel);
    134. cell.setCellStyle(style);
    135. }
    136. /**
    137. * 设置字体样式
    138. *
    139. * @param row 行对象
    140. * @param style 单元格样式
    141. * @param cellStyleModel 样式信息
    142. */
    143. private void setFontStyle(Row row, XSSFCellStyle style, CellStyleModel cellStyleModel) {
    144. //字体名称
    145. String fontName = cellStyleModel.getFontName();
    146. //字体大小
    147. Double fontHeight = cellStyleModel.getFontHeight();
    148. //字体颜色
    149. Object fontColor = cellStyleModel.getFontColor();
    150. //字体加粗
    151. Boolean fontBold = cellStyleModel.getFontBold();
    152. //字体斜体
    153. Boolean fontItalic = cellStyleModel.getFontItalic();
    154. //字体下划线
    155. Byte fontUnderLine = cellStyleModel.getFontUnderLine();
    156. //字体上标下标
    157. Short fontTypeOffset = cellStyleModel.getFontTypeOffset();
    158. //字体删除线
    159. Boolean fontStrikeout = cellStyleModel.getFontStrikeout();
    160. //不需要设置字体样式
    161. if (fontName == null && fontHeight == null && fontColor == null && fontBold == null && fontItalic == null
    162. && fontUnderLine == null && fontTypeOffset == null && fontStrikeout == null) {
    163. return;
    164. }
    165. XSSFFont font = null;
    166. //样式存在字体对象时,使用原有的字体对象
    167. if (style.getFontIndex() != 0) {
    168. font = style.getFont();
    169. }
    170. //样式不存在字体对象时,创建字体对象
    171. else {
    172. font = (XSSFFont) row.getSheet().getWorkbook().createFont();
    173. //默认字体为宋体
    174. font.setFontName("宋体");
    175. }
    176. //设置字体名称
    177. if (fontName != null) {
    178. font.setFontName(fontName);
    179. }
    180. //设置字体大小
    181. if (fontHeight != null) {
    182. font.setFontHeight(fontHeight);
    183. }
    184. //设置字体颜色
    185. if (fontColor != null) {
    186. //使用IndexedColors定义的颜色
    187. if (fontColor instanceof IndexedColors) {
    188. font.setColor(((IndexedColors) fontColor).getIndex());
    189. }
    190. //使用自定义的RGB颜色
    191. else if (fontColor instanceof XSSFColor) {
    192. font.setColor((XSSFColor) fontColor);
    193. }
    194. }
    195. //设置字体加粗
    196. if (fontBold != null) {
    197. font.setBold(fontBold);
    198. }
    199. //设置字体斜体
    200. if (fontItalic != null) {
    201. font.setItalic(fontItalic);
    202. }
    203. //设置字体下划线
    204. if (fontUnderLine != null) {
    205. font.setUnderline(fontUnderLine);
    206. }
    207. //设置字体上标下标
    208. if (fontTypeOffset != null) {
    209. font.setTypeOffset(fontTypeOffset);
    210. }
    211. //设置字体删除线
    212. if (fontStrikeout != null) {
    213. font.setStrikeout(fontStrikeout);
    214. }
    215. style.setFont(font);
    216. }
    217. /**
    218. * 设置边框样式
    219. *
    220. * @param style 单元格样式
    221. * @param cellStyleModel 样式信息
    222. */
    223. private void setBorderStyle(XSSFCellStyle style, CellStyleModel cellStyleModel) {
    224. //上边框线条类型
    225. BorderStyle borderTop = cellStyleModel.getBorderTop();
    226. //右边框线条类型
    227. BorderStyle borderRight = cellStyleModel.getBorderRight();
    228. //下边框线条类型
    229. BorderStyle borderBottom = cellStyleModel.getBorderBottom();
    230. //左边框线条类型
    231. BorderStyle borderLeft = cellStyleModel.getBorderLeft();
    232. //上边框颜色类型
    233. Object topBorderColor = cellStyleModel.getTopBorderColor();
    234. //右边框颜色类型
    235. Object rightBorderColor = cellStyleModel.getRightBorderColor();
    236. //下边框颜色类型
    237. Object bottomBorderColor = cellStyleModel.getBottomBorderColor();
    238. //左边框颜色类型
    239. Object leftBorderColor = cellStyleModel.getLeftBorderColor();
    240. //不需要设置边框样式
    241. if (borderTop == null && borderRight == null && borderBottom == null && borderLeft == null && topBorderColor == null
    242. && rightBorderColor == null && bottomBorderColor == null && leftBorderColor == null) {
    243. return;
    244. }
    245. //设置上边框线条类型
    246. if (borderTop != null) {
    247. style.setBorderTop(borderTop);
    248. }
    249. //设置右边框线条类型
    250. if (borderRight != null) {
    251. style.setBorderRight(borderRight);
    252. }
    253. //设置下边框线条类型
    254. if (borderBottom != null) {
    255. style.setBorderBottom(borderBottom);
    256. }
    257. //设置左边框线条类型
    258. if (borderLeft != null) {
    259. style.setBorderLeft(borderLeft);
    260. }
    261. //设置上边框线条颜色
    262. if (topBorderColor != null) {
    263. //使用IndexedColors定义的颜色
    264. if (topBorderColor instanceof IndexedColors) {
    265. style.setTopBorderColor(((IndexedColors) topBorderColor).getIndex());
    266. }
    267. //使用自定义的RGB颜色
    268. else if (topBorderColor instanceof XSSFColor) {
    269. style.setTopBorderColor((XSSFColor) topBorderColor);
    270. }
    271. }
    272. //设置右边框线条颜色
    273. if (rightBorderColor != null) {
    274. //使用IndexedColors定义的颜色
    275. if (rightBorderColor instanceof IndexedColors) {
    276. style.setRightBorderColor(((IndexedColors) rightBorderColor).getIndex());
    277. }
    278. //使用自定义的RGB颜色
    279. else if (rightBorderColor instanceof XSSFColor) {
    280. style.setRightBorderColor((XSSFColor) rightBorderColor);
    281. }
    282. }
    283. //设置下边框线条颜色
    284. if (bottomBorderColor != null) {
    285. //使用IndexedColors定义的颜色
    286. if (bottomBorderColor instanceof IndexedColors) {
    287. style.setBottomBorderColor(((IndexedColors) bottomBorderColor).getIndex());
    288. }
    289. //使用自定义的RGB颜色
    290. else if (bottomBorderColor instanceof XSSFColor) {
    291. style.setBottomBorderColor((XSSFColor) bottomBorderColor);
    292. }
    293. }
    294. //设置左边框线条颜色
    295. if (leftBorderColor != null) {
    296. //使用IndexedColors定义的颜色
    297. if (leftBorderColor instanceof IndexedColors) {
    298. style.setLeftBorderColor(((IndexedColors) leftBorderColor).getIndex());
    299. }
    300. //使用自定义的RGB颜色
    301. else if (topBorderColor instanceof XSSFColor) {
    302. style.setLeftBorderColor((XSSFColor) leftBorderColor);
    303. }
    304. }
    305. }
    306. /**
    307. * 设置对齐方式
    308. *
    309. * @param style 单元格样式
    310. * @param cellStyleModel 样式信息
    311. */
    312. private void setAlignmentStyle(XSSFCellStyle style, CellStyleModel cellStyleModel) {
    313. //水平对齐方式
    314. HorizontalAlignment horizontalAlignment = cellStyleModel.getHorizontalAlignment();
    315. //垂直对齐方式
    316. VerticalAlignment verticalAlignment = cellStyleModel.getVerticalAlignment();
    317. //不需要设置对齐方式
    318. if (horizontalAlignment == null && verticalAlignment == null) {
    319. return;
    320. }
    321. //设置水平对齐方式
    322. if (horizontalAlignment != null) {
    323. style.setAlignment(horizontalAlignment);
    324. }
    325. //设置垂直对齐方式
    326. if (verticalAlignment != null) {
    327. style.setVerticalAlignment(verticalAlignment);
    328. }
    329. }
    330. }

    四、如有换行及其他背景字体样式,需引入换行样式(换行识别标识‘\n’)

    1. package com.siact.product.jwp.module.report.service.impl.StyleUtils;
    2. import cn.hutool.core.util.StrUtil;
    3. import lombok.Data;
    4. import org.apache.poi.ss.usermodel.*;
    5. import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
    6. import org.apache.poi.xssf.usermodel.XSSFColor;
    7. /**
    8. * @Description:
    9. */
    10. @Data
    11. public class CellStyleModel {
    12. /**
    13. * sheet名称
    14. */
    15. private String sheetName;
    16. /**
    17. * 列索引
    18. */
    19. private int colIndex;
    20. /**
    21. * 行索引
    22. */
    23. private int rowIndex;
    24. /**
    25. * 字体名称
    26. */
    27. private String fontName;
    28. /**
    29. * 字体大小
    30. */
    31. private Double fontHeight;
    32. /**
    33. * 字体颜色
    34. */
    35. private Object fontColor;
    36. /**
    37. * 字体加粗
    38. */
    39. private Boolean fontBold;
    40. /**
    41. * 字体斜体
    42. */
    43. private Boolean fontItalic;
    44. /**
    45. * 字体下划线
    46. */
    47. private Byte fontUnderLine;
    48. /**
    49. * 字体上标下标
    50. */
    51. private Short fontTypeOffset;
    52. /**
    53. * 字体删除线
    54. */
    55. private Boolean fontStrikeout;
    56. /**
    57. * 背景颜色
    58. */
    59. private Object backgroundColor;
    60. /**
    61. * 上边框线条类型
    62. */
    63. private BorderStyle borderTop;
    64. /**
    65. * 右边框线条类型
    66. */
    67. private BorderStyle borderRight;
    68. /**
    69. * 下边框线条类型
    70. */
    71. private BorderStyle borderBottom;
    72. /**
    73. * 左边框线条类型
    74. */
    75. private BorderStyle borderLeft;
    76. /**
    77. * 上边框线条颜色
    78. */
    79. private Object topBorderColor;
    80. /**
    81. * 上边框线条颜色
    82. */
    83. private Object rightBorderColor;
    84. /**
    85. * 下边框线条颜色
    86. */
    87. private Object bottomBorderColor;
    88. /**
    89. */
    90. private Object leftBorderColor;
    91. /**
    92. * 水平对齐方式
    93. */
    94. private HorizontalAlignment horizontalAlignment;
    95. /**
    96. * 垂直对齐方式
    97. */
    98. private VerticalAlignment verticalAlignment;
    99. /**
    100. * 自动换行方式
    101. */
    102. private Boolean wrapText;
    103. /**
    104. * 生成字体名称样式信息
    105. *
    106. * @param sheetName sheet页名称
    107. * @param rowIndex 行号
    108. * @param columnIndex 列号
    109. * @param fontName 字体名称(默认宋体)
    110. * @return
    111. */
    112. public static CellStyleModel createFontNameCellStyleModel(String sheetName, int rowIndex, int columnIndex, String fontName) {
    113. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, fontName, null, null, null, null, null, null, null);
    114. }
    115. /**
    116. * 生成字体名称大小信息
    117. *
    118. * @param sheetName sheet页名称
    119. * @param rowIndex 行号
    120. * @param columnIndex 列号
    121. * @param fontHeight 字体大小
    122. * @return
    123. */
    124. public static CellStyleModel createFontHeightCellStyleModel(String sheetName, int rowIndex, int columnIndex
    125. , Double fontHeight) {
    126. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, fontHeight, null, null, null, null, null, null);
    127. }
    128. /**
    129. * 得到RBG自定义颜色
    130. *
    131. * @param redNum 红色数值
    132. * @param greenNum 绿色数值
    133. * @param blueNum 蓝色数值
    134. * @return
    135. */
    136. public static XSSFColor getRGBColor(int redNum, int greenNum, int blueNum) {
    137. XSSFColor color = new XSSFColor(new byte[]{(byte) redNum, (byte) greenNum, (byte) blueNum}, new DefaultIndexedColorMap());
    138. return color;
    139. }
    140. /**
    141. * 生成字体颜色样式信息(支持自定义RGB颜色)
    142. *
    143. * @param sheetName sheet页名称
    144. * @param rowIndex 行号
    145. * @param columnIndex 列号
    146. * @param redNum 红色数值
    147. * @param greenNum 绿色数值
    148. * @param blueNum 蓝色数值
    149. * @return
    150. */
    151. public static CellStyleModel createFontColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    152. , int redNum, int greenNum, int blueNum) {
    153. XSSFColor fontColor = getRGBColor(redNum, greenNum, blueNum);
    154. return createFontColorCellStyleModel(sheetName, rowIndex, columnIndex, fontColor);
    155. }
    156. /**
    157. * 生成字体颜色样式信息
    158. *
    159. * @param sheetName sheet页名称
    160. * @param rowIndex 行号
    161. * @param columnIndex 列号
    162. * @param fontColor 字体颜色
    163. * @return
    164. */
    165. public static CellStyleModel createFontColorCellStyleModel(String sheetName, int rowIndex, int columnIndex, Object fontColor) {
    166. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, fontColor, null, null, null, null, null);
    167. }
    168. /**
    169. * 生成字体加粗样式信息
    170. *
    171. * @param sheetName sheet页名称
    172. * @param rowIndex 行号
    173. * @param columnIndex 列号
    174. * @param fontBold 字体加粗
    175. * @return
    176. */
    177. public static CellStyleModel createFontBoldCellStyleModel(String sheetName, int rowIndex, int columnIndex, Boolean fontBold) {
    178. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, fontBold, null, null, null, null);
    179. }
    180. /**
    181. * 生成字体斜体样式信息
    182. *
    183. * @param sheetName sheet页名称
    184. * @param rowIndex 行号
    185. * @param columnIndex 列号
    186. * @param fontItalic 字体斜体
    187. * @return
    188. */
    189. public static CellStyleModel createFontItalicCellStyleModel(String sheetName, int rowIndex, int columnIndex, Boolean fontItalic) {
    190. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, fontItalic, null, null, null);
    191. }
    192. /**
    193. * 生成字体下划线样式信息
    194. *
    195. * @param sheetName sheet页名称
    196. * @param rowIndex 行号
    197. * @param columnIndex 列号
    198. * @param fontUnderLine 字体下划线
    199. * @return
    200. */
    201. public static CellStyleModel createFontUnderLineCellStyleModel(String sheetName, int rowIndex, int columnIndex, Byte fontUnderLine) {
    202. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, fontUnderLine, null, null);
    203. }
    204. /**
    205. * 生成字体上标下标样式信息
    206. *
    207. * @param sheetName sheet页名称
    208. * @param rowIndex 行号
    209. * @param columnIndex 列号
    210. * @param fontTypeOffset 字体上标下标
    211. * @return
    212. */
    213. public static CellStyleModel createFontTypeOffsetCellStyleModel(String sheetName, int rowIndex, int columnIndex, Short fontTypeOffset) {
    214. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, fontTypeOffset, null);
    215. }
    216. /**
    217. * 生成字体删除线样式信息
    218. *
    219. * @param sheetName sheet页名称
    220. * @param rowIndex 行号
    221. * @param columnIndex 列号
    222. * @param fontStrikeout 字体删除线
    223. * @return
    224. */
    225. public static CellStyleModel createFontStrikeoutCellStyleModel(String sheetName, int rowIndex, int columnIndex, Boolean fontStrikeout) {
    226. return createFontCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, null, fontStrikeout);
    227. }
    228. /**
    229. * 生成字体样式信息
    230. *
    231. * @param sheetName sheet页名称
    232. * @param rowIndex 行号
    233. * @param columnIndex 列号
    234. * @param fontName 字体名称(默认宋体)
    235. * @param fontHeight 字体大小
    236. * @param fontColor 字体颜色
    237. * @param fontItalic 字体斜体
    238. * @param fontBold 字体加粗
    239. * @param fontUnderLine 字体下划线
    240. * @param fontTypeOffset 字体上标下标
    241. * @param fontStrikeout 字体删除线
    242. * @return
    243. */
    244. public static CellStyleModel createFontCellStyleModel(String sheetName, int rowIndex, int columnIndex
    245. , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
    246. , Short fontTypeOffset, Boolean fontStrikeout) {
    247. return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
    248. , fontUnderLine, fontTypeOffset, fontStrikeout, null);
    249. }
    250. /**
    251. * 生成背景颜色样式信息
    252. *
    253. * @param sheetName sheet页名称
    254. * @param rowIndex 行号
    255. * @param columnIndex 列号
    256. * @param backgroundColor 背景颜色
    257. * @return
    258. */
    259. public static CellStyleModel createBackgroundColorCellStyleModel(String sheetName, int rowIndex, int columnIndex, Object backgroundColor) {
    260. return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, null, null, backgroundColor);
    261. }
    262. /**
    263. * 生成背景颜色样式信息(支持自定义RGB颜色)
    264. *
    265. * @param sheetName sheet页名称
    266. * @param rowIndex 行号
    267. * @param columnIndex 列号
    268. * @param redNum 红色数值
    269. * @param greenNum 绿色数值
    270. * @param blueNum 蓝色数值
    271. * @return
    272. */
    273. public static CellStyleModel createBackgroundColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    274. , int redNum, int greenNum, int blueNum) {
    275. XSSFColor backgroundColor = getRGBColor(redNum, greenNum, blueNum);
    276. return createBackgroundColorCellStyleModel(sheetName, rowIndex, columnIndex, backgroundColor);
    277. }
    278. /**
    279. * 生成样式信息
    280. *
    281. * @param sheetName sheet页名称
    282. * @param rowIndex 行号
    283. * @param columnIndex 列号
    284. * @param fontName 字体名称(宋体)
    285. * @param fontHeight 字体大小
    286. * @param fontColor 字体颜色
    287. * @param fontBold 字体加粗
    288. * @param fontItalic 字体斜体
    289. * @param fontUnderLine 字体下划线
    290. * @param fontTypeOffset 字体上标下标
    291. * @param fontStrikeout 字体删除线
    292. * @param backgroundColor 背景颜色
    293. * @return
    294. */
    295. public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
    296. , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
    297. , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor) {
    298. return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
    299. , fontUnderLine, fontTypeOffset, fontStrikeout, backgroundColor, null, null, null, null, null, null, null, null);
    300. }
    301. /**
    302. * 生成上边框线条颜色样式信息
    303. *
    304. * @param sheetName sheet页名称
    305. * @param rowIndex 行号
    306. * @param columnIndex 列号
    307. * @param topBorderColor 上边框线条颜色
    308. * @return
    309. */
    310. public static CellStyleModel createTopBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    311. , Object topBorderColor) {
    312. return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, topBorderColor, null, null, null);
    313. }
    314. /**
    315. * 生成右边框线条颜色样式信息
    316. *
    317. * @param sheetName sheet页名称
    318. * @param rowIndex 行号
    319. * @param columnIndex 列号
    320. * @param rightBorderColor 右边框线条颜色
    321. * @return
    322. */
    323. public static CellStyleModel createRightBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    324. , Object rightBorderColor) {
    325. return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, null, rightBorderColor, null, null);
    326. }
    327. /**
    328. * 生成下边框线条颜色样式信息
    329. *
    330. * @param sheetName sheet页名称
    331. * @param rowIndex 行号
    332. * @param columnIndex 列号
    333. * @param bottomBorderColor 下边框线条颜色
    334. * @return
    335. */
    336. public static CellStyleModel createBottomBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    337. , Object bottomBorderColor) {
    338. return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, null, null, bottomBorderColor, null);
    339. }
    340. /**
    341. * 生成左边框线条颜色样式信息
    342. *
    343. * @param sheetName sheet页名称
    344. * @param rowIndex 行号
    345. * @param columnIndex 列号
    346. * @param leftBorderColor 左边框线条颜色
    347. * @return
    348. */
    349. public static CellStyleModel createLeftBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    350. , Object leftBorderColor) {
    351. return createBorderColorCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, leftBorderColor);
    352. }
    353. /**
    354. * 生成上边框线条类型样式信息
    355. *
    356. * @param sheetName sheet页名称
    357. * @param rowIndex 行号
    358. * @param columnIndex 列号
    359. * @param borderTop 上边框线条类型
    360. * @return
    361. */
    362. public static CellStyleModel createTopBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
    363. , BorderStyle borderTop) {
    364. return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, borderTop, null, null, null);
    365. }
    366. /**
    367. * 生成右边框线条类型样式信息
    368. *
    369. * @param sheetName sheet页名称
    370. * @param rowIndex 行号
    371. * @param columnIndex 列号
    372. * @param borderRight 右边框线条类型
    373. * @return
    374. */
    375. public static CellStyleModel createRightBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
    376. , BorderStyle borderRight) {
    377. return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, null, borderRight, null, null);
    378. }
    379. /**
    380. * 生成下边框线条类型样式信息
    381. *
    382. * @param sheetName sheet页名称
    383. * @param rowIndex 行号
    384. * @param columnIndex 列号
    385. * @param borderBottom 下边框线条类型
    386. * @return
    387. */
    388. public static CellStyleModel createBottomBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
    389. , BorderStyle borderBottom) {
    390. return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, null, null, borderBottom, null);
    391. }
    392. /**
    393. * 生成左边框线条类型样式信息
    394. *
    395. * @param sheetName sheet页名称
    396. * @param rowIndex 行号
    397. * @param columnIndex 列号
    398. * @param borderLeft 左边框线条类型
    399. * @return
    400. */
    401. public static CellStyleModel createLeftBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
    402. , BorderStyle borderLeft) {
    403. return createBorderLineTypeCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, borderLeft);
    404. }
    405. /**
    406. * 生成边框线条颜色样式信息
    407. *
    408. * @param sheetName sheet页名称
    409. * @param rowIndex 行号
    410. * @param columnIndex 列号
    411. * @param borderColor 边框线条颜色
    412. * @return
    413. */
    414. public static CellStyleModel createBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    415. , Object borderColor) {
    416. return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, null, borderColor);
    417. }
    418. /**
    419. * 生成边框线条颜色样式信息
    420. *
    421. * @param sheetName sheet页名称
    422. * @param rowIndex 行号
    423. * @param columnIndex 列号
    424. * @param topBorderColor 上边框线条颜色
    425. * @param rightBorderColor 右边框线条颜色
    426. * @param bottomBorderColor 下边框线条颜色
    427. * @param leftBorderColor 左边框线条颜色
    428. * @return
    429. */
    430. public static CellStyleModel createBorderColorCellStyleModel(String sheetName, int rowIndex, int columnIndex
    431. , Object topBorderColor, Object rightBorderColor, Object bottomBorderColor, Object leftBorderColor) {
    432. return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null
    433. , topBorderColor, rightBorderColor, bottomBorderColor, leftBorderColor);
    434. }
    435. /**
    436. * 生成边框线条类型样式信息
    437. *
    438. * @param sheetName sheet页名称
    439. * @param rowIndex 行号
    440. * @param columnIndex 列号
    441. * @param borderLineType 边框线条类型
    442. * @return
    443. */
    444. public static CellStyleModel createBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
    445. , BorderStyle borderLineType) {
    446. return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, borderLineType, null);
    447. }
    448. /**
    449. * 生成边框线条类型样式信息
    450. *
    451. * @param sheetName sheet页名称
    452. * @param rowIndex 行号
    453. * @param columnIndex 列号
    454. * @param borderTop 上边框线条类型
    455. * @param borderRight 右边框线条类型
    456. * @param borderBottom 下边框线条类型
    457. * @param borderLeft 左边框线条类型
    458. * @return
    459. */
    460. public static CellStyleModel createBorderLineTypeCellStyleModel(String sheetName, int rowIndex, int columnIndex
    461. , BorderStyle borderTop, BorderStyle borderRight, BorderStyle borderBottom, BorderStyle borderLeft) {
    462. return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, borderTop, borderRight, borderBottom, borderLeft
    463. , null, null, null, null);
    464. }
    465. /**
    466. * 生成边框样式信息
    467. *
    468. * @param sheetName sheet页名称
    469. * @param rowIndex 行号
    470. * @param columnIndex 列号
    471. * @param borderLineType 边框线条类型
    472. * @param borderColor 边框线条颜色
    473. * @return
    474. */
    475. public static CellStyleModel createBorderCellStyleModel(String sheetName, int rowIndex, int columnIndex
    476. , BorderStyle borderLineType, Object borderColor) {
    477. return createBorderCellStyleModel(sheetName, rowIndex, columnIndex, borderLineType, borderLineType, borderLineType, borderLineType
    478. , borderColor, borderColor, borderColor, borderColor);
    479. }
    480. /**
    481. * 生成边框样式信息
    482. *
    483. * @param sheetName sheet页名称
    484. * @param rowIndex 行号
    485. * @param columnIndex 列号
    486. * @param borderTop 上边框线条类型
    487. * @param borderRight 右边框线条类型
    488. * @param borderBottom 下边框线条类型
    489. * @param borderLeft 左边框线条类型
    490. * @param topBorderColor 上边框线条颜色
    491. * @param rightBorderColor 右边框线条颜色
    492. * @param bottomBorderColor 下边框线条颜色
    493. * @param leftBorderColor 左边框线条颜色
    494. * @return
    495. */
    496. public static CellStyleModel createBorderCellStyleModel(String sheetName, int rowIndex, int columnIndex
    497. , BorderStyle borderTop, BorderStyle borderRight, BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor
    498. , Object rightBorderColor, Object bottomBorderColor, Object leftBorderColor) {
    499. return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null, null, null, null
    500. , null, borderTop, borderRight, borderBottom, borderLeft, topBorderColor, rightBorderColor
    501. , bottomBorderColor, leftBorderColor);
    502. }
    503. /**
    504. * 生成样式信息
    505. *
    506. * @param sheetName sheet页名称
    507. * @param rowIndex 行号
    508. * @param columnIndex 列号
    509. * @param fontName 字体名称(宋体)
    510. * @param fontHeight 字体大小
    511. * @param fontColor 字体颜色
    512. * @param fontBold 字体加粗
    513. * @param fontItalic 字体斜体
    514. * @param fontUnderLine 字体下划线
    515. * @param fontTypeOffset 字体上标下标
    516. * @param fontStrikeout 字体删除线
    517. * @param backgroundColor 背景颜色
    518. * @param borderTop 上边框线条类型
    519. * @param borderRight 右边框线条类型
    520. * @param borderBottom 下边框线条类型
    521. * @param borderLeft 左边框线条类型
    522. * @param topBorderColor 上边框线条颜色
    523. * @param rightBorderColor 右边框线条颜色
    524. * @param bottomBorderColor 下边框线条颜色
    525. * @param leftBorderColor 左边框线条颜色
    526. * @return
    527. */
    528. public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
    529. , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
    530. , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor, BorderStyle borderTop, BorderStyle borderRight
    531. , BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor, Object rightBorderColor, Object bottomBorderColor
    532. , Object leftBorderColor) {
    533. return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
    534. , fontUnderLine, fontTypeOffset, fontStrikeout, backgroundColor, borderTop, borderRight, borderBottom
    535. , borderLeft, topBorderColor, rightBorderColor, bottomBorderColor, leftBorderColor, null, null);
    536. }
    537. /**
    538. * 生成水平对齐方式信息
    539. *
    540. * @param sheetName sheet页名称
    541. * @param rowIndex 行号
    542. * @param columnIndex 列号
    543. * @param horizontalAlignment 水平对齐方式
    544. * @return
    545. */
    546. public static CellStyleModel createHorizontalAlignmentCellStyleModel(String sheetName, int rowIndex, int columnIndex
    547. , HorizontalAlignment horizontalAlignment) {
    548. return createAlignmentCellStyleModel(sheetName, rowIndex, columnIndex, horizontalAlignment, null);
    549. }
    550. /**
    551. * 生成垂直对齐方式信息
    552. *
    553. * @param sheetName sheet页名称
    554. * @param rowIndex 行号
    555. * @param columnIndex 列号
    556. * @param verticalAlignment 垂直对齐方式
    557. * @return
    558. */
    559. public static CellStyleModel createVerticalAlignmentCellStyleModel(String sheetName, int rowIndex, int columnIndex
    560. , VerticalAlignment verticalAlignment) {
    561. return createAlignmentCellStyleModel(sheetName, rowIndex, columnIndex, null, verticalAlignment);
    562. }
    563. /**
    564. * 生成对齐方式信息
    565. *
    566. * @param sheetName sheet页名称
    567. * @param rowIndex 行号
    568. * @param columnIndex 列号
    569. * @param horizontalAlignment 水平对齐方式
    570. * @param verticalAlignment 垂直对齐方式
    571. * @return
    572. */
    573. public static CellStyleModel createAlignmentCellStyleModel(String sheetName, int rowIndex, int columnIndex
    574. , HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) {
    575. return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null
    576. , null, null, null, null, null, null, null
    577. , null, null, null, null, null, null
    578. , horizontalAlignment, verticalAlignment);
    579. }
    580. /**
    581. * 生成样式信息
    582. *
    583. * @param sheetName sheet页名称
    584. * @param rowIndex 行号
    585. * @param columnIndex 列号
    586. * @param fontName 字体名称(宋体)
    587. * @param fontHeight 字体大小
    588. * @param fontColor 字体颜色
    589. * @param fontBold 字体加粗
    590. * @param fontItalic 字体斜体
    591. * @param fontUnderLine 字体下划线
    592. * @param fontTypeOffset 字体上标下标
    593. * @param fontStrikeout 字体删除线
    594. * @param backgroundColor 背景颜色
    595. * @param borderTop 上边框线条类型
    596. * @param borderRight 右边框线条类型
    597. * @param borderBottom 下边框线条类型
    598. * @param borderLeft 左边框线条类型
    599. * @param topBorderColor 上边框线条颜色
    600. * @param rightBorderColor 右边框线条颜色
    601. * @param bottomBorderColor 下边框线条颜色
    602. * @param leftBorderColor 左边框线条颜色
    603. * @param horizontalAlignment 水平对齐方式
    604. * @param verticalAlignment 垂直对齐方式
    605. * @return
    606. */
    607. public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
    608. , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
    609. , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor, BorderStyle borderTop, BorderStyle borderRight
    610. , BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor, Object rightBorderColor, Object bottomBorderColor
    611. , Object leftBorderColor, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) {
    612. return createCellStyleModel(sheetName, rowIndex, columnIndex, fontName, fontHeight, fontColor, fontBold, fontItalic
    613. , fontUnderLine, fontTypeOffset, fontStrikeout, backgroundColor, borderTop, borderRight, borderBottom
    614. , borderLeft, topBorderColor, rightBorderColor, bottomBorderColor, leftBorderColor, horizontalAlignment, verticalAlignment, null);
    615. }
    616. /**
    617. * 生成自动换行样式信息
    618. *
    619. * @param sheetName sheet页名称
    620. * @param rowIndex 行号
    621. * @param columnIndex 列号
    622. * @param wrapText 自动换行
    623. * @return
    624. */
    625. public static CellStyleModel createWrapTextCellStyleModel(String sheetName, int rowIndex, int columnIndex
    626. , Boolean wrapText) {
    627. return createCellStyleModel(sheetName, rowIndex, columnIndex, null, null, null, null, null
    628. , null, null, null, null, null, null, null
    629. , null, null, null, null, null, null, null
    630. , wrapText);
    631. }
    632. /**
    633. * 生成样式信息
    634. *
    635. * @param sheetName sheet页名称
    636. * @param rowIndex 行号
    637. * @param columnIndex 列号
    638. * @param fontName 字体名称(宋体)
    639. * @param fontHeight 字体大小
    640. * @param fontColor 字体颜色
    641. * @param fontBold 字体加粗
    642. * @param fontItalic 字体斜体
    643. * @param fontUnderLine 字体下划线
    644. * @param fontTypeOffset 字体上标下标
    645. * @param fontStrikeout 字体删除线
    646. * @param backgroundColor 背景颜色
    647. * @param borderTop 上边框线条类型
    648. * @param borderRight 右边框线条类型
    649. * @param borderBottom 下边框线条类型
    650. * @param borderLeft 左边框线条类型
    651. * @param topBorderColor 上边框线条颜色
    652. * @param rightBorderColor 右边框线条颜色
    653. * @param bottomBorderColor 下边框线条颜色
    654. * @param leftBorderColor 左边框线条颜色
    655. * @param horizontalAlignment 水平对齐方式
    656. * @param verticalAlignment 垂直对齐方式
    657. * @param wrapText 自动换行
    658. * @return
    659. */
    660. public static CellStyleModel createCellStyleModel(String sheetName, int rowIndex, int columnIndex
    661. , String fontName, Double fontHeight, Object fontColor, Boolean fontBold, Boolean fontItalic, Byte fontUnderLine
    662. , Short fontTypeOffset, Boolean fontStrikeout, Object backgroundColor, BorderStyle borderTop, BorderStyle borderRight
    663. , BorderStyle borderBottom, BorderStyle borderLeft, Object topBorderColor, Object rightBorderColor, Object bottomBorderColor
    664. , Object leftBorderColor, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, Boolean wrapText) {
    665. CellStyleModel cellStyleModel = new CellStyleModel();
    666. //sheet页名称
    667. cellStyleModel.setSheetName(sheetName);
    668. //行号
    669. cellStyleModel.setRowIndex(rowIndex);
    670. //列号
    671. cellStyleModel.setColIndex(columnIndex);
    672. //设置字体样式
    673. //字体名称(比如宋体)
    674. fontName = fontName != null && StrUtil.equals(fontName, "") ? "宋体" : fontName;
    675. cellStyleModel.setFontName(fontName);
    676. //字体大小
    677. fontHeight = fontHeight != null && fontHeight <= 0 ? null : fontHeight;
    678. cellStyleModel.setFontHeight(fontHeight);
    679. //字体颜色
    680. fontColor = fontColor != null && (fontColor instanceof IndexedColors == false && fontColor instanceof XSSFColor == false)
    681. ? null : fontColor;
    682. cellStyleModel.setFontColor(fontColor);
    683. //字体加粗
    684. cellStyleModel.setFontBold(fontBold);
    685. //字体斜体
    686. cellStyleModel.setFontItalic(fontItalic);
    687. //字体下划线
    688. fontUnderLine = fontUnderLine != null && (fontUnderLine != Font.U_NONE && fontUnderLine != Font.U_SINGLE && fontUnderLine != Font.U_DOUBLE
    689. && fontUnderLine != Font.U_DOUBLE_ACCOUNTING && fontUnderLine != Font.U_SINGLE_ACCOUNTING) ? null : fontUnderLine;
    690. cellStyleModel.setFontUnderLine(fontUnderLine);
    691. //字体上标下标
    692. fontTypeOffset = fontTypeOffset != null && (fontTypeOffset != Font.SS_NONE && fontTypeOffset != Font.SS_SUB && fontTypeOffset != Font.SS_SUPER)
    693. ? null : fontTypeOffset;
    694. cellStyleModel.setFontTypeOffset(fontTypeOffset);
    695. //字体删除线
    696. cellStyleModel.setFontStrikeout(fontStrikeout);
    697. //背景颜色
    698. backgroundColor = backgroundColor != null && (backgroundColor instanceof IndexedColors == false && backgroundColor instanceof XSSFColor == false)
    699. ? null : backgroundColor;
    700. cellStyleModel.setBackgroundColor(backgroundColor);
    701. //边框样式
    702. //上边框线条类型
    703. cellStyleModel.setBorderTop(borderTop);
    704. //右边框线条类型
    705. cellStyleModel.setBorderRight(borderRight);
    706. //下边框线条类型
    707. cellStyleModel.setBorderBottom(borderBottom);
    708. //左边框线条类型
    709. cellStyleModel.setBorderLeft(borderLeft);
    710. //上边框颜色类型
    711. cellStyleModel.setTopBorderColor(topBorderColor);
    712. //右边框颜色类型
    713. cellStyleModel.setRightBorderColor(rightBorderColor);
    714. //下边框颜色类型
    715. cellStyleModel.setBottomBorderColor(bottomBorderColor);
    716. //左边框颜色类型
    717. cellStyleModel.setLeftBorderColor(leftBorderColor);
    718. //对齐方式
    719. //水平对齐方式
    720. cellStyleModel.setHorizontalAlignment(horizontalAlignment);
    721. //垂直对齐方式
    722. cellStyleModel.setVerticalAlignment(verticalAlignment);
    723. //自动换行
    724. cellStyleModel.setWrapText(wrapText);
    725. return cellStyleModel;
    726. }
    727. }

    五、导出实现

    1. public void exportPatrolWorkOrderInfo(String startTime, String endTime, HttpServletResponse response) {
    2. List allContentList = new ArrayList<>();
    3. //设置合并策略
    4. List cellStyleList = new ArrayList<>();
    5. //根据时间获取工单
    6. List patrolProcessExportDTOList = analysisPatrolProcessExportInfo.analyzeExportProcessInfo(new ExportWorkOrderInfoDTO(startTime, endTime));
    7. if (!CollectionUtils.isEmpty(patrolProcessExportDTOList)) {
    8. patrolProcessExportDTOList = patrolProcessExportDTOList.stream().sorted(Comparator.comparing(PatrolProcessExportDTO::getProcessInstanceStartTime)).collect(Collectors.toList());
    9. int i = 1;
    10. for (PatrolProcessExportDTO patrolProcessExportDTO : patrolProcessExportDTOList) {
    11. Map>> nfcMap = patrolProcessExportDTO.getNfcMap();
    12. if (!CollectionUtils.isEmpty(nfcMap)) {
    13. int finalI = i;
    14. nfcMap.forEach((k, v) -> {
    15. PatrolProcessDTO patrolProcessDTO = new PatrolProcessDTO();
    16. BeanUtils.copyProperties(patrolProcessExportDTO, patrolProcessDTO);
    17. patrolProcessDTO.setOrderNum(finalI);
    18. //设置打卡点情况
    19. if (v.size() == 0) {
    20. patrolProcessDTO.setContent(k + "未打卡");
    21. //设置单元格背景颜色
    22. cellStyleList.add(CellStyleModel.createBackgroundColorCellStyleModel("巡检工单", allContentList.size(), 4, IndexedColors.YELLOW));
    23. } else {
    24. patrolProcessDTO.setContent(getNfcInfo(v, k));
    25. }
    26. allContentList.add(patrolProcessDTO);
    27. });
    28. } else {
    29. PatrolProcessDTO patrolProcessDTO = new PatrolProcessDTO();
    30. BeanUtils.copyProperties(patrolProcessExportDTO, patrolProcessDTO);
    31. patrolProcessDTO.setOrderNum(i);
    32. allContentList.add(patrolProcessDTO);
    33. }
    34. i++;
    35. }
    36. }
    37. for (int i = 0; i <= allContentList.size(); i++) {
    38. for (int j = 0; j < 9; j++) {
    39. // cellStyleList.add(CellStyleModel.createBorderCellStyleModel("巡检工单", i, j, BorderStyle.THIN, IndexedColors.BLACK));
    40. //设置换行策略
    41. cellStyleList.add(CellStyleModel.createWrapTextCellStyleModel("巡检工单", i, j, true));
    42. }
    43. }
    44. //向会话写入
    45. try {
    46. response.setContentType("application/vnd.ms-excel");
    47. response.setCharacterEncoding("utf-8");
    48. String fileName = URLEncoder.encode("巡检工单", "UTF-8"); //.replaceAll("\\+", "%20")
    49. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    50. EasyExcel.write(response.getOutputStream(), PatrolProcessDTO.class)
    51. .excelType(ExcelTypeEnum.XLSX)
    52. .autoCloseStream(Boolean.TRUE)
    53. // 添加自定义处理程序,相当于Spring的AOP切面
    54. .registerWriteHandler(new ExcelFillCellMergeStrategy(0, 8, new ArrayList<>()))
    55. .registerWriteHandler(new CustomCellStyleHandler(cellStyleList))
    56. .sheet("巡检工单").doWrite(allContentList);
    57. } catch (Exception e) {
    58. log.error("导出出错-{}", ExceptionUtils.getStackTrace(e));
    59. renderString(response, JSON.toJSONString(new ResultInfo().error(SystemError.SYS_10055)));
    60. }
    61. }

    备注:由于合并策略是自定义得,在效率上没法保证,当牵扯到大数据量合并导出时,用时较长。

  • 相关阅读:
    win10端口转发
    MinIO:开源对象存储解决方案的领先者
    Linux Shell入门常用命令使用
    Docker Compose 是什么
    开篇——初识云原生
    深度学习(PyTorch)——生成对抗网络(GAN)
    ModelCache.safeGet(androidProjectResult.androidProject::getNdkVersion, ““) must not be null
    nn.AdaptiveAvgPool2d(output_size)详解
    Android应用内组件通讯之EventBus的使用(一)
    微服务11-Sentinel中的授权规则以及Sentinel服务规则持久化
  • 原文地址:https://blog.csdn.net/weixin_44705744/article/details/133669306