ExcelCommentHandle.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package jnpf.excel.handle;
  2. import cn.afterturn.easypoi.excel.entity.ExportParams;
  3. import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
  4. import jnpf.excel.ExcelHelper;
  5. import jnpf.excel.ExcelPreHandle;
  6. import jnpf.util.ExcelUtil;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.poi.ss.usermodel.*;
  9. import org.apache.poi.xssf.usermodel.XSSFCell;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Objects;
  13. /**
  14. * excel添加批注
  15. */
  16. public class ExcelCommentHandle implements ExcelPreHandle {
  17. String sheetName;
  18. Workbook workbook;
  19. int headerRowLen;
  20. int lastRowNum;
  21. /**
  22. * workbook增加标题批注
  23. *
  24. * @param workbook 工作簿
  25. * @param sheetName 工作表名称
  26. * @param columnIndex 列索引,从0开始
  27. * @param comment 批注内容
  28. */
  29. public void addComment(Workbook workbook, String sheetName, int rowIndex, int columnIndex, String comment) {
  30. Sheet sheet = workbook.getSheet(sheetName);
  31. Row headerRow = sheet.getRow(rowIndex);
  32. Cell headerCell = headerRow.getCell(columnIndex);
  33. // 判断是否存在批注
  34. Comment cellComment = headerCell.getCellComment();
  35. if (!Objects.isNull(cellComment)) {
  36. headerCell.setCellComment(null);
  37. }
  38. Drawing<?> drawing = sheet.createDrawingPatriarch();
  39. CreationHelper factory = workbook.getCreationHelper();
  40. ClientAnchor anchor = factory.createClientAnchor();
  41. anchor.setRow1(rowIndex);
  42. anchor.setCol1(columnIndex);
  43. Comment headerComment = drawing.createCellComment(anchor);
  44. RichTextString str = factory.createRichTextString(comment);
  45. headerComment.setString(str);
  46. headerCell.setCellComment(headerComment);
  47. }
  48. @Override
  49. public void execute(ExcelHelper data, Map<String, Object> params) {
  50. List<ExcelExportEntity> list = data.getEntities();
  51. ExportParams exportParams = data.getExportParams();
  52. workbook = data.getWorkbook();
  53. params.put("sheetName", sheetName);
  54. this.sheetName = exportParams.getSheetName();
  55. Sheet sheet = workbook.getSheet(exportParams.getSheetName());
  56. headerRowLen = data.isComplexTable() ? 2 : 1;
  57. sheet.createFreezePane(exportParams.getFreezeCol(),headerRowLen);
  58. lastRowNum = sheet.getLastRowNum();
  59. for (int i = 0; i < headerRowLen; i++) {
  60. Row headerRow = sheet.getRow(i);
  61. int lastCellNum = headerRow.getLastCellNum();
  62. for (int j = 0; j < lastCellNum; j++) {
  63. Cell cell = headerRow.getCell(j);
  64. this.addComment(cell, i, j);
  65. }
  66. }
  67. }
  68. private void addComment(Cell cell, int rowIndex, int columnIndex) {
  69. if (cell == null) return;
  70. String name = ((XSSFCell) cell).getRichStringCellValue().getString();
  71. if (StringUtils.isBlank(name)) return;
  72. String id = ExcelUtil.getIdFromCellValue(name);
  73. if (StringUtils.isNotBlank(id)) {
  74. this.addComment(workbook, sheetName, rowIndex, columnIndex, id);
  75. }
  76. }
  77. }