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);
    }
  }

}
相关推荐
仙俊红几秒前
Spring Boot `@Configuration` 与 `@Component` 笔记
java·spring boot·笔记
计算机学姐3 小时前
基于SpringBoot的社团管理系统【2026最新】
java·vue.js·spring boot·后端·mysql·spring·mybatis
天上掉下来个程小白3 小时前
微服务-25.网关登录校验-网关传递用户到微服务
java·数据库·微服务
vivi_and_qiao4 小时前
HTML的form表单
java·前端·html
Slaughter信仰5 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第四章知识点问答补充及重新排版
java·开发语言·jvm
心灵宝贝5 小时前
Mac用户安装JDK 22完整流程(Intel版dmg文件安装指南附安装包下载)
java·开发语言·macos
ta是个码农5 小时前
Mysql——日志
java·数据库·mysql·日志
今***b5 小时前
Python 操作 PPT 文件:从新手到高手的实战指南
java·python·powerpoint
David爱编程5 小时前
volatile 关键字详解:轻量级同步工具的边界与误区
java·后端
fatfishccc7 小时前
Spring MVC 全解析:从核心原理到 SSM 整合实战 (附完整源码)
java·spring·ajax·mvc·ssm·过滤器·拦截器interceptor