EasyExcel 3.3.2 内容单元格左中对齐不生效的最终解决方案

在 EasyExcel 3.3.2 中,很多人会遇到"内容单元格对齐设置不生效"的问题。通常是因为默认的 FillStyleCellWriteHandler 在后续执行中覆盖了你手动设置的样式。本文给出一个稳定可用的最终方案:保证自定义样式在最后执行,并清理掉 WriteCellData 的样式引用,避免再次被覆盖。

适用场景

  • 使用 EasyExcel 3.3.2 写 Excel
  • 需要内容单元格统一"左对齐 + 垂直居中"
  • 当前样式不生效或被覆盖

最终方案(可直接使用)

1. 新增内容单元格样式处理器

复制代码
package com.agentspace.aichat.util.excel;

import com.alibaba.excel.constant.OrderConstant;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.HashMap;
import java.util.Map;

/**
 * 内容单元格左对齐+垂直居中
 */
public class LeftCenterStyleStrategy implements CellWriteHandler {

    private final Map<Short, CellStyle> styleCache = new HashMap<>();

    @Override
    public int order() {
        return OrderConstant.FILL_STYLE + 1;
    }

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        if (Boolean.TRUE.equals(context.getHead())) {
            return;
        }
        Cell cell = context.getCell();
        if (cell == null) {
            return;
        }
        cell.setCellStyle(getDataCellStyle(cell));

        WriteCellData<?> cellData = context.getFirstCellData();
        if (cellData != null) {
            cellData.setWriteCellStyle(null);
        }
    }

    private CellStyle getDataCellStyle(Cell cell) {
        CellStyle base = cell.getCellStyle();
        short baseIndex = base != null ? base.getIndex() : 0;
        CellStyle cached = styleCache.get(baseIndex);
        if (cached != null) {
            return cached;
        }
        Workbook workbook = cell.getSheet().getWorkbook();
        CellStyle created = workbook.createCellStyle();
        if (base != null) {
            created.cloneStyleFrom(base);
        }
        created.setAlignment(HorizontalAlignment.LEFT);
        created.setVerticalAlignment(VerticalAlignment.CENTER);
        styleCache.put(baseIndex, created);
        return created;
    }
}

为什么这个方案有效?

  • order() 设置为 OrderConstant.FILL_STYLE + 1,确保在 EasyExcel 默认样式处理器之后执行,避免被覆盖。
  • 使用 cloneStyleFrom 保留原本的边框、字体、格式,只替换对齐方式。
  • 清理 WriteCellData 的样式引用,防止后续再次覆盖。

总结

只要你使用 EasyExcel 3.3.2 进行 Excel 导出,并需要内容单元格统一左中对齐,以上方案是当前最稳定的做法。

若后续需要表头也统一对齐,或加上自动换行、边框、字体等,也可以基于此策略扩展。

相关推荐
utf8mb4安全女神17 小时前
⽇志管理与深层防⽕墙
java·开发语言·spring boot
better_liang17 小时前
每日Java面试场景题知识点之-数据库与缓存的一致性
java·数据库·redis·面试·分布式系统·缓存一致性·cache aside
减瓦17 小时前
Jackson 自定义反序列化器的类型不匹配陷阱
java·后端
qq_4523962317 小时前
第九篇:《Dockerfile 指令精讲(二):WORKDIR、ENV、ARG、EXPOSE》
java·开发语言·docker
JAVA社区17 小时前
Java高级全套教程(九)—— SpringCloud超详细实战详解
java·开发语言·后端·spring cloud·面试·职场和发展
wyjcxyyy17 小时前
java反序列化-cc1链
java·c语言·开发语言
garmin Chen17 小时前
Elasticsearch(1):Elasticsearch核心原理与基础操作总结
java·大数据·笔记·elasticsearch·搜索引擎·全文检索
Devin~Y17 小时前
大厂Java面试实录:Spring Boot/Cloud、Kafka、Redis、K8s 可观测性 + RAG/Agent(小Y翻车版)
java·spring boot·redis·spring cloud·kafka·kubernetes·mybatis
林森lsjs17 小时前
【日耕一题】2. 面向对象 Java 基础:构造方法与 toString
java·开发语言
学代码的真由酱17 小时前
【自用】测开面试问题-Java
java·面试·职场和发展