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 导出,并需要内容单元格统一左中对齐,以上方案是当前最稳定的做法。

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

相关推荐
蛋蛋的就会顺顺的9 小时前
hot100 p0——栈,贪心算法,动态规划
java·前端·javascript·算法·leetcode·力扣
需要8269 小时前
Spring Bean 生命周期:从实例化到销毁的完整钩子
java·后端·spring
小 黄 鸡9 小时前
HashMap 面试必会知识点详解(JDK 8)
java·面试·职场和发展
伯恩bourne11 小时前
Spring Boot Bean 注册的两种方式:@Component 家族 vs @Configuration + @Bean
java·spring boot·后端
silvamux_12 小时前
curl 测试 OpenAI 兼容 API 的完整流程
java·人工智能·缓存·ai·oneapi
省钱兄--zs12 小时前
深圳24小时自助健身房软硬件解决方案实战指南与开发流程详解
java·数据仓库·spring boot·小程序·架构
顽疲12 小时前
Java vue 养老系统源码详解:月账单 care_month_bill 与 Quartz 扣费 Job 实践
java·开发语言·vue.js·vue养老·springboot养老·java养老
Wang's Blog13 小时前
Java 项目实战: 交友项目 - 安装开发工具与环境准备
java·开发语言·交友
用户212968352616813 小时前
开源了一个 Java 多智能体 LLM 框架 Baton:一个依赖,把 Agent 循环、工具调用、记忆、编排全配齐
java·llm
helmsail13 小时前
05 管理活动与选品
java