• Java整合EasyExcel实战——3(上下列相同合并单元格策略)


    参考:https://juejin.cn/post/7322156759443095561?searchId=202405262043517631094B7CCB463FDA06icon-default.png?t=N7T8https://juejin.cn/post/7322156759443095561?searchId=202405262043517631094B7CCB463FDA06

    准备条件

    依赖

    1. com.alibaba
    2. easyexcel
    3. 2.2.6

    工具类

    1. package co.yixiang.exam.listener;
    2. import com.alibaba.excel.metadata.CellData;
    3. import com.alibaba.excel.metadata.Head;
    4. import com.alibaba.excel.write.handler.CellWriteHandler;
    5. import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
    6. import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
    7. import org.apache.poi.ss.usermodel.Cell;
    8. import org.apache.poi.ss.usermodel.CellType;
    9. import org.apache.poi.ss.usermodel.Row;
    10. import org.apache.poi.ss.usermodel.Sheet;
    11. import org.apache.poi.ss.util.CellRangeAddress;
    12. import java.util.List;
    13. public class ExcelFillCellMergeStrategy implements CellWriteHandler {
    14. private int[] mergeColumnIndex;
    15. private int mergeRowIndex;
    16. public ExcelFillCellMergeStrategy() {
    17. }
    18. public ExcelFillCellMergeStrategy(int mergeRowIndex, int[] mergeColumnIndex) {
    19. this.mergeRowIndex = mergeRowIndex;
    20. this.mergeColumnIndex = mergeColumnIndex;
    21. }
    22. @Override
    23. public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
    24. }
    25. @Override
    26. public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    27. }
    28. @Override
    29. public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {
    30. }
    31. @Override
    32. public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
    33. //当前行
    34. int curRowIndex = cell.getRowIndex();
    35. //当前列
    36. int curColIndex = cell.getColumnIndex();
    37. if (curRowIndex > mergeRowIndex) {
    38. for (int i = 0; i < mergeColumnIndex.length; i++) {
    39. if (curColIndex == mergeColumnIndex[i]) {
    40. mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
    41. break;
    42. }
    43. }
    44. }
    45. }
    46. /**
    47. * 当前单元格向上合并
    48. *
    49. * @param writeSheetHolder
    50. * @param cell 当前单元格
    51. * @param curRowIndex 当前行
    52. * @param curColIndex 当前列
    53. */
    54. private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
    55. //获取当前行的当前列的数据和上一行的当前列列数据,通过上一行数据是否相同进行合并
    56. Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
    57. Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
    58. Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
    59. // 比较当前行的第一列的单元格与上一行是否相同,相同合并当前单元格与上一行
    60. //
    61. if (curData.equals(preData)) {
    62. Sheet sheet = writeSheetHolder.getSheet();
    63. List mergeRegions = sheet.getMergedRegions();
    64. boolean isMerged = false;
    65. for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
    66. CellRangeAddress cellRangeAddr = mergeRegions.get(i);
    67. // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
    68. if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
    69. sheet.removeMergedRegion(i);
    70. cellRangeAddr.setLastRow(curRowIndex);
    71. sheet.addMergedRegion(cellRangeAddr);
    72. isMerged = true;
    73. }
    74. }
    75. // 若上一个单元格未被合并,则新增合并单元
    76. if (!isMerged) {
    77. CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
    78. sheet.addMergedRegion(cellRangeAddress);
    79. }
    80. }
    81. }
    82. }

    ServiceImpl层应用

    sql 数据的重复数据,合并单元格

    1. SELECT
    2. eq.id,eq.question_title,eo.options_content,eq.options_correct,eq.question_answer,eq.question_score,eq.question_subject,eq.question_title_zi,eq.question_type
    3. FROM
    4. ex_question eq
    5. LEFT JOIN
    6. ex_options eo
    7. on eq.id = eo.question_id where eq.is_del = 0 and eo.is_del = 0;
    1. @Override
    2. public R exportExQuestionOptions(HttpServletResponse response) throws IOException {
    3. // 定义导出的Excel文件名
    4. String fileName = "test.xlsx";
    5. // 设置响应的内容类型为二进制流,这是文件下载的标准设置
    6. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    7. // 设置响应头的Content-Disposition,使用"attachment"指示浏览器这是一个需要下载的文件
    8. response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));
    9. // 查询需要导出的数据 (包含复杂数据)
    10. List questionsOptionsAll = exQuestionMapper.getQuestionsOptionsAll();
    11. // 假设我们要合并第1行和第2列到第4列的数据(索引从0开始)
    12. int mergeRowIndex = 0; // 行索引
    13. int[] mergeColumnIndex = new int[]{0,1,2,3,4,5,6}; // 列索引数组 要合并的列
    14. ExcelFillCellMergeStrategy excelFillCellMergeStrategy = new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumnIndex);
    15. EasyExcel.write(response.getOutputStream(), ExExcelQuestionOptionsDto.class)
    16. .registerWriteHandler(excelFillCellMergeStrategy)
    17. .sheet("测试")
    18. .doWrite(questionsOptionsAll);
    19. return R.success();
    20. }

  • 相关阅读:
    视频剪辑调色软件推荐—DaVinlve Studio 18(达芬奇18)
    弘辽科技:淘宝店铺怎么补流量单?淘宝如何快速获取流量?
    SKEP Senta代码 finetune训练步骤 记录
    跨站脚本攻击(XSS):为什么Cookie中有HttpOnly属性?
    无穿戴人体动捕方案全新上线,创新赋能多领域应用
    教程:如何使用 Bootstrap 5 构建简单的管理仪表板界面
    Hyperledger Besu环境搭建(Linux)
    项目进展(一)-晶振正常输出、焊接驱动芯片、查找芯片手册并学习
    甲骨文真的要开放Java EE?
    龙旗科技提交IPO注册:计划募资18亿元,小米为其股东和主要客户
  • 原文地址:https://blog.csdn.net/weixin_67573348/article/details/139285585