在Java开发中,处理Excel文件是常见的业务需求。Apache POI作为老牌Java Excel操作库,功能强大但API较为底层。EasyExcel作为其封装库,简化了常见操作,但在2.2.10版本中对行级样式控制的支持仍有提升空间。本文介绍的LocalExcelRowStyleModifierHang工具类,正是针对这一痛点开发的解决方案。
这个工具的核心价值在于:它允许开发者对Excel文件中的任意行(单行或多行)进行精细化的样式控制,包括字体、行高、背景色等常见属性,同时统一管理边框样式。不同于常规的单元格级别样式设置,它以行为单位进行批量处理,大幅提升了开发效率。
工具类通过RowStyleConfig内部类封装了所有可配置的样式属性:
java复制public static class RowStyleConfig {
private boolean bold; // 字体加粗
private String fontName; // 字体名称
private short fontSize; // 字号(磅值)
private short fontColor; // 字体颜色(IndexedColors)
private short bgColor; // 背景色
private float rowHeight; // 行高(磅)
private HorizontalAlignment horizontalAlign; // 水平对齐
private VerticalAlignment verticalAlign; // 垂直对齐
private BorderStyle borderStyle; // 边框样式
private short borderColor; // 边框颜色
// 构造器和getter方法...
}
这种封装方式有三大优势:
代码中特别强调了对边框样式的统一处理:
java复制// 设置边框样式(上下左右统一)
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
// 设置边框颜色(统一为黑色)
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
// 其他边框颜色设置...
这种设计出于两个考虑:
核心方法modifyMultiRowWithDifferentStyle的流程图如下:
文件校验与加载
java复制File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
throw new IOException("原文件不存在:" + sourceFilePath);
}
Workbook workbook = new XSSFWorkbook(new FileInputStream(sourceFile));
行样式遍历应用
java复制for (Map.Entry<Integer, RowStyleConfig> entry : rowStyleMap.entrySet()) {
int rowNum = entry.getKey();
RowStyleConfig config = entry.getValue();
// 样式应用逻辑...
}
资源释放
java复制workbook.write(new FileOutputStream(targetFilePath));
workbook.close();
样式创建分为三个步骤:
字体配置
java复制Font font = workbook.createFont();
font.setBold(config.isBold());
font.setFontName(config.getFontName());
font.setFontHeightInPoints(config.getFontSize());
单元格样式配置
java复制CellStyle style = workbook.createCellStyle();
style.setFont(font);
style.setFillForegroundColor(config.getBgColor());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
应用到所有单元格
java复制for (int i = 0; i < lastCellNum; i++) {
Cell cell = row.getCell(i, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
cell.setCellStyle(style);
}
java复制Map<Integer, RowStyleConfig> styles = new HashMap<>();
// 表头行
styles.put(0, new RowStyleConfig(
true, "黑体", (short)22, IndexedColors.BLACK.index,
IndexedColors.WHITE.index, 47.6f,
HorizontalAlignment.CENTER, VerticalAlignment.CENTER,
BorderStyle.THIN, IndexedColors.BLACK.index
));
// 数据行
styles.put(1, new RowStyleConfig(
false, "宋体", (short)12, IndexedColors.BLACK.index,
IndexedColors.WHITE.index, 25f,
HorizontalAlignment.LEFT, VerticalAlignment.CENTER,
BorderStyle.THIN, IndexedColors.BLACK.index
));
modifyMultiRowWithDifferentStyle(
"input.xlsx", "output.xlsx", 0, styles
);
| 参数 | 类型 | 说明 | 常用值 |
|---|---|---|---|
| bold | boolean | 字体加粗 | true/false |
| fontName | String | 字体名称 | "宋体"、"黑体"等 |
| fontSize | short | 字号大小 | 10-36 |
| fontColor | short | 字体色值 | IndexedColors常量 |
| bgColor | short | 背景色值 | IndexedColors常量 |
| rowHeight | float | 行高(磅) | 15.0f-50.0f |
样式复用:相同样式的行应共享CellStyle对象
java复制Map<String, CellStyle> styleCache = new HashMap<>();
String styleKey = config.getFontName()+config.getFontSize()+...;
if (!styleCache.containsKey(styleKey)) {
// 创建新样式
styleCache.put(styleKey, newStyle);
}
return styleCache.get(styleKey);
批量写入:处理大量行时使用SXSSFWorkbook
java复制Workbook workbook = new SXSSFWorkbook(100); // 保留100行在内存
并行处理:对非连续行可采用并行流
java复制rowStyleMap.entrySet().parallelStream().forEach(entry -> {
// 线程安全的样式处理
});
java复制try (Workbook wb = ...; OutputStream os = ...) {
// 自动关闭资源
}
条件格式化:根据单元格值动态设置行样式
java复制if (cell.getNumericCellValue() > 100) {
// 应用警告样式
}
模板导出:结合模板文件实现样式继承
java复制Workbook template = WorkbookFactory.create(new File("template.xlsx"));
// 修改特定行后导出
样式预设:定义常用样式组合
java复制public enum PresetStyles {
HEADER, HIGHLIGHT, WARNING...
}
在实际项目中,这个工具类已经帮助我们将报表生成的代码量减少了40%,同时保证了样式的一致性。特别是在处理财务报告等对格式要求严格的场景时,它的行级控制能力显得尤为宝贵。