EasyExcel中处理内容占多个单元格

在一些业务需求中内容需要占用多个单元格的情况,如下图:
在这里插入图片描述
或者是这样
在这里插入图片描述
这样
在这里插入图片描述
总有一些奇怪怪的需求。
在这里插入图片描述
不过使用EasyExcel可以轻松处理这些变态的需求。EasyExcel中提供了@ContentLoopMerge 注解就是为了处理这种问题的。下面先看看如何使用@ContentLoopMerge 注解完成上面三个图中excel的合并。


  • 完成图一

    • 创建类模型配置相关注解
    publuc  class User {
         
         private String name;
         private String age;
         @ContentLoopMerge(eachRow = 4)
         private String address;
    }
    
  • 完成图二

    • 创建类模型配置相关注解
    public class User {
         
       private String name;
       private String age;
       @ContentLoopMerge(columnExtend = 4)
       private String address;
    }
    
  • 完成图三

    • 创建类模型配置相关注解
    public class User {
         
        private String name;
         private String age;
         @ContentLoopMerge(eachRow = 4, columnExtend = 5)
         private String address;
    }
    
  • 主测试类

public class Test{
   
  public static void mian(String[] args){
   
    List<User> userList = new ArrayList<>();
    User user = new User();
    user.setName("李四");
    user.setAge("12");
    user.setAddress("火星xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    userList.add(user);

    EasyExcel.write("F:\\excel\\a.xls", User.class)
        .sheet()
        .doWrite(userList);
  }
}

在这里插入图片描述


下面看看EasyExcel如何实现。

  • 注解@ContentLoopMerge
/**
 * 内容循环合并
 */
@Target({
   ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ContentLoopMerge {
   

  /**
   * 当前属性扩展行
   *
   * @return
   */
  int eachRow() default 1;

  /**
   * 当前属性扩展列
   *
   * @return
   */
  int columnExtend() default 1;
}
  • 在通过内容合并拦截器处

/**
 * 循环合并的区域
 *
 */
public class LoopMergeStrategy implements RowWriteHandler {
   
  /**
   * 当前属性扩展行
   */
  private final int eachRow;
  /**
   * 当前属性扩展列
   */
  private final int columnExtend;
  /**
   * 当前属性列索引
   */
  private final int columnIndex;

  public LoopMergeStrategy(int eachRow, int columnIndex) {
   
    this(eachRow, 1, columnIndex);
  }

  public LoopMergeStrategy(int eachRow, int columnExtend, int columnIndex) {
   
    if (eachRow < 1) {
   
      throw new IllegalArgumentException("EachRows must be greater than 1");
    }
    if (columnExtend < 1) {
   
      throw new IllegalArgumentException("ColumnExtend must be greater than 1");
    }
    if (columnExtend == 1 && eachRow == 1) {
   
      throw new IllegalArgumentException("ColumnExtend or eachRows must be greater than 1");
    }
    if (columnIndex < 0) {
   
      throw new IllegalArgumentException("ColumnIndex must be greater than 0");
    }
    this.eachRow = eachRow;
    this.columnExtend = columnExtend;
    this.columnIndex = columnIndex;
  }

  public LoopMergeStrategy(LoopMergeProperty loopMergeProperty, Integer columnIndex) {
   
    this(loopMergeProperty.getEachRow(), loopMergeProperty.getColumnExtend(), columnIndex);
  }

   // 核心:在完成行内容写入完成后会调用此方法进行处理合并
  @Override
  public void afterRowDispose(RowWriteHandlerContext context) {
   
    if (context.getHead() || context.getRelativeRowIndex() == null) {
   
      return;
    }
    if (context.getRelativeRowIndex() % eachRow == 0) {
   
      CellRangeAddress cellRangeAddress = new CellRangeAddress(context.getRowIndex(),
          context.getRowIndex() + eachRow - 1,
          columnIndex, columnIndex + columnExtend - 1);
      context.getWriteSheetHolder().getSheet().addMergedRegionUnsafe(cellRangeAddress);
    }
  }
}

在这里插入图片描述

以上结合源码简单说明了内容合并具体还得自行学习源码.

相关推荐

  1. eazyexcel生成校验单元内容的excel文件

    2023-12-26 03:04:03       10 阅读
  2. EsayExcel读取合并单元

    2023-12-26 03:04:03       36 阅读
  3. 在Excel使用正则提取单元内容

    2023-12-26 03:04:03       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-26 03:04:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-26 03:04:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-26 03:04:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-26 03:04:03       20 阅读

热门阅读

  1. 【期末复习】微信小程序复习大纲

    2023-12-26 03:04:03       32 阅读
  2. Selenium4自动化测试框架

    2023-12-26 03:04:03       35 阅读
  3. 力扣:208. 实现 Trie (前缀树)(Python3)

    2023-12-26 03:04:03       38 阅读
  4. 表单验证开发 - 登录注册开发(3)

    2023-12-26 03:04:03       47 阅读