EasyExcel中处理表头行高和内容行高

在学习easyExcek 时,发现使用easyExcek 处理表头和内容的行高十分容易只需要在类模型配置上使用@HeadRowHeight 或者@ContentRowHeight 注解实现控制。

  • 创建一个类模型配置类
java 复制代码
// 设置表头行高度
@HeadRowHeight(40)
// 设置内容行高度
@ContentRowHeight(60)
@Data
public class User {

  private String name;

  private String age;

  private String address;
}
  • 测试主类
java 复制代码
 public static void main(String[] args) {
     List<User> userList = new ArrayList<>();

    User user = new User();
    user.setName("李四");
    user.setAge("12");
    user.setAddress("火星");
    userList.add(user);

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

    总体感觉还是很easy的。

下面看看具体的源码:

  • 注解@HeadRowHeight
java 复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface HeadRowHeight {
    /**
     * 设置表头高,默认是-1,-1 表示自动设置高度
     */
    short value() default -1;
}
  • 注解@ContentRowHeight
java 复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ContentRowHeight {

    /**
     * 设置内容行高,默认是-1,-1 表示自动设置高度
     */
    short value() default -1;
}
  • 核心拦截器
java 复制代码
public abstract class AbstractRowHeightStyleStrategy implements RowWriteHandler {
  // 在完成该行的所有操作后调用。在填充的情况下,可以多次调用。
  @Override
  public void afterRowDispose(RowWriteHandlerContext context) {
    if (context.getHead() == null) {
      return;
    }
    
    // 当前是否是表头,是则调用setHeadColumnHeight
    // 反之调用setContentColumnHeight
    if (context.getHead()) {
      setHeadColumnHeight(context.getRow(), context.getRelativeRowIndex());
    } else {
      setContentColumnHeight(context.getRow(), context.getRelativeRowIndex());
    }
  }

  /**
   * 设置表头行高抽象方法,需要子类进行实现
   *
   */
  protected abstract void setHeadColumnHeight(Row row, int relativeRowIndex);

  /**
   * 设置内容行高抽象方法,需要子类进行实现
   *
   */
  protected abstract void setContentColumnHeight(Row row, int relativeRowIndex);

}
  • 唯一实现类
java 复制代码
public class SimpleRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy {
  private final Short headRowHeight;
  private final Short contentRowHeight;

  public SimpleRowHeightStyleStrategy(Short headRowHeight, Short contentRowHeight) {
    this.headRowHeight = headRowHeight;
    this.contentRowHeight = contentRowHeight;
  }

  @Override
  protected void setHeadColumnHeight(Row row, int relativeRowIndex) {
    if (headRowHeight != null) {
      row.setHeightInPoints(headRowHeight);
    }
  }

  @Override
  protected void setContentColumnHeight(Row row, int relativeRowIndex) {
    if (contentRowHeight != null) {
      row.setHeightInPoints(contentRowHeight);
    }
  }

}
相关推荐
快乐非自愿36 分钟前
Java垃圾收集器全解:从Serial到G1的进化之旅
java·开发语言·python
树在风中摇曳41 分钟前
Java 静态成员与继承封装实战:从报错到彻底吃透核心特性
java·开发语言
键来大师4 小时前
Android15 RK3588 修改默认不锁屏不休眠
android·java·framework·rk3588
合作小小程序员小小店5 小时前
web网页开发,在线%考试管理%系统,基于Idea,vscode,html,css,vue,java,maven,springboot,mysql
java·前端·系统架构·vue·intellij-idea·springboot
多多*6 小时前
maven常用的命令
java·log4j·maven
xie_pin_an6 小时前
MyBatis-Plus 实战:MPJLambdaWrapper 多表联查用法全解析
java·spring boot·spring·mybatis
ᐇ9596 小时前
Java LinkedList集合全面解析:双向链表的艺术与实战
java·开发语言·链表
luyun0202026 小时前
Windows 11操作更丝滑,绝了
java·运维·figma
码银6 小时前
【数据结构】顺序表
java·开发语言·数据结构
Boop_wu6 小时前
[Java EE] 计算机基础
java·服务器·前端