当前位置: 首页 > news >正文

EasyExcel 格式设置大全

EasyExcel 是阿里巴巴开源的基于 POI 的 Excel 处理工具,虽然以简单易用和大数据量处理著称,但也提供了丰富的格式设置功能。以下是 EasyExcel 格式设置的全面指南:


1. 基础格式设置

1.1 单元格样式设置

// 创建样式配置
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 设置背景色
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
// 设置字体
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short)12);
headWriteFont.setBold(true);
headWriteFont.setColor(IndexedColors.WHITE.getIndex());
headWriteCellStyle.setWriteFont(headWriteFont);
// 设置边框
headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
headWriteCellStyle.setBorderRight(BorderStyle.THIN);
headWriteCellStyle.setBorderTop(BorderStyle.THIN);
headWriteCellStyle.setBorderBottom(BorderStyle.THIN);// 应用到表头
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

1.2 内容样式设置

WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 设置内容水平居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 设置内容垂直居中
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 设置自动换行
contentWriteCellStyle.setWrapped(true);

2. 高级格式设置

2.1 自定义样式策略

public class CustomStyleStrategy extends AbstractHorizontalCellStyleStrategy {@Overrideprotected WriteCellStyle headCellStyle(Head head) {// 自定义表头样式WriteCellStyle style = new WriteCellStyle();style.setFillForegroundColor(IndexedColors.BLUE.getIndex());return style;}@Overrideprotected WriteCellStyle contentCellStyle(WriteSheetHolder writeSheetHolder) {// 自定义内容样式WriteCellStyle style = new WriteCellStyle();style.setFillPatternType(FillPatternType.SOLID_FOREGROUND);return style;}
}

2.2 条件格式设置

// 使用拦截器实现条件格式
public class CustomCellWriteHandler implements CellWriteHandler {@Overridepublic void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {if (!isHead && cell.getColumnIndex() == 2) {// 对第三列设置特殊格式CellStyle cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);cell.setCellStyle(cellStyle);}}
}

3. 数字格式设置

3.1 设置数字格式

WriteCellStyle numberStyle = new WriteCellStyle();
// 设置数字格式为保留两位小数
numberStyle.setDataFormat((short)4);  // 0.00格式// 应用到特定列
Map<Integer, WriteCellStyle> styleMap = new HashMap<>();
styleMap.put(2, numberStyle);  // 第三列应用此样式HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle, styleMap);

3.2 自定义数字格式

// 创建自定义数据格式
Workbook workbook = new SXSSFWorkbook();
short format = workbook.createDataFormat().getFormat("#,##0.00_);[Red](#,##0.00)");WriteCellStyle style = new WriteCellStyle();
style.setDataFormat(format);

4. 字体设置

4.1 基本字体设置

WriteFont font = new WriteFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)14);
font.setBold(true);
font.setColor(IndexedColors.RED.getIndex());
font.setItalic(true);
font.setUnderline(Font.U_SINGLE);WriteCellStyle style = new WriteCellStyle();
style.setWriteFont(font);

4.2 多行表头字体设置

// 复杂表头样式
WriteCellStyle headStyle = new WriteCellStyle();
WriteFont headFont = new WriteFont();
headFont.setFontHeightInPoints((short)10);
headStyle.setWriteFont(headFont);// 使用注解设置表头
@ExcelProperty(value = {"主标题", "次标题"})
private String name;

5. 行高和列宽设置

5.1 设置行高

// 通过拦截器设置行高
public class CustomRowWriteHandler implements RowWriteHandler {@Overridepublic void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {if (isHead) {row.setHeightInPoints(30);  // 表头行高30} else {row.setHeightInPoints(20);  // 内容行高20}}
}

5.2 设置列宽

// 通过拦截器设置列宽
public class CustomSheetWriteHandler implements SheetWriteHandler {@Overridepublic void afterSheetCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder) {Sheet sheet = writeSheetHolder.getSheet();// 设置第一列宽度sheet.setColumnWidth(0, 5000);// 设置第二列自动调整宽度sheet.autoSizeColumn(1);}
}

6. 合并单元格

// 使用拦截器合并单元格
public class CustomMergeStrategy implements CellWriteHandler {@Overridepublic void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {// 合并第一行的1-3列if (isHead && cell.getRowIndex() == 0 && cell.getColumnIndex() == 0) {Sheet sheet = writeSheetHolder.getSheet();CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);sheet.addMergedRegion(region);}}
}

7. 边框设置

WriteCellStyle style = new WriteCellStyle();
// 设置细边框
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);// 设置边框颜色
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setRightBorderColor(IndexedColors.BLACK.getIndex());

8. 实际应用示例

// 完整导出示例
public void exportWithStyle(HttpServletResponse response) throws IOException {// 准备数据List<User> users = getUsers();// 设置响应头response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setHeader("Content-Disposition", "attachment;filename=users.xlsx");// 表头样式WriteCellStyle headWriteCellStyle = new WriteCellStyle();headWriteCellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());WriteFont headWriteFont = new WriteFont();headWriteFont.setFontHeightInPoints((short)12);headWriteFont.setColor(IndexedColors.WHITE.getIndex());headWriteCellStyle.setWriteFont(headWriteFont);// 内容样式WriteCellStyle contentWriteCellStyle = new WriteCellStyle();contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);// 数字列特殊样式WriteCellStyle numberStyle = new WriteCellStyle();numberStyle.setDataFormat((short)4);Map<Integer, WriteCellStyle> styleMap = new HashMap<>();styleMap.put(2, numberStyle);  // 第三列是数字// 构建样式策略HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle, styleMap);// 导出EasyExcel.write(response.getOutputStream(), User.class).registerWriteHandler(styleStrategy).registerWriteHandler(new CustomRowWriteHandler())  // 行高.registerWriteHandler(new CustomSheetWriteHandler())  // 列宽.registerWriteHandler(new CustomMergeStrategy())  // 合并单元格.sheet("用户列表").doWrite(users);
}

9. 常见问题

  1. 样式不生效:

    • 确保正确注册了样式策略

    • 检查样式设置的优先级

    • 确认没有其他拦截器覆盖了样式

  2. 性能问题:

    • 避免在循环中创建样式对象

    • 重用样式对象

    • 对于大数据量,考虑使用 SXSSF 模式

  3. 格式兼容性:

    • 某些复杂格式可能在旧版 Excel 中不兼容

    • 测试不同 Excel 版本的兼容性

EasyExcel 的格式设置虽然不如原生 POI 那样细致,但对于大多数业务场景已经足够,且在大数据量处理上有明显优势。

http://www.dtcms.com/a/309405.html

相关文章:

  • Qt-----初识
  • Qt 跨平台应用开发经验分享
  • 数据结构:链表(Linked List)
  • ModeSeq论文阅读
  • 使用 Vive Tracker 替代 T265 实现位姿获取(基于 Ubuntu + SteamVR)
  • Cloud Storage:一款高效便捷的云端存储解决方案
  • xcode swift项目运行、连接真机运行报错,引入文件夹失败
  • iOS 抓不到包怎么办?全流程排查思路与替代引导
  • Spring Boot 文件上传限制配置全攻略:Java 配法 vs YAML 配法
  • webpack面试题及详细答案80题(41-60)
  • k8s之DevicePlugin
  • 第13届蓝桥杯Python青少组_省赛_中/高级组_2022年4月17日真题
  • C语言---位运算符的分类与用法(按位与、按位或 |、按位异或^、按位取反~、左移<<、右移>>)
  • 【前端】CSS Flexbox布局示例介绍
  • 应用药品注册证识别技术,为医药行业的合规、高效与创新发展提供核心驱动力
  • 数据结构:算法复杂度与空间复杂度
  • 《协作画布的深层架构:React与TypeScript构建多人实时绘图应用的核心逻辑》
  • 提升文档管理:推荐一键Docker部署的全文索引搜索引擎工具
  • Ubuntu 24.04.2 LTS 安装mysql8.0.36保姆级教程(从安装到远程连接)
  • ZKmall开源商城微服务架构电商平台:服务注册与配置中心设计
  • 如何创建一个飞书应用获取自己的飞书AppID和AppSecret?
  • Spring之【循环引用】
  • 第三阶段—8天Python从入门到精通【itheima】-140节(pysqark实战——基础准备)
  • 江协科技STM32 12-2 BKP备份寄存器RTC实时时钟
  • 二分查找(基础)
  • 启动中国蚁剑
  • 东芝时钟控制的双极步进电机驱动器TB67S209FTG
  • 关于皮带机流水线的控制思想解析
  • Sklearn 机器学习 文本数据 TF-IDF实现文本向量化
  • Linux 内存管理之 Rmap 反向映射