easyexcel @cellstyle 用法
时间: 2023-11-15 15:56:54 浏览: 79
easyexcel 是一个基于阿里巴巴的开源项目,用于快速读写 Excel 文件。@cellstyle 是 easyexcel 中的一个注解,用于设置单元格样式。具体用法如下:
1. 在实体类中定义需要设置样式的字段,并在字段上添加 @cellstyle 注解,如下所示:
```java
@cellstyle(headColorIndex = 2, font = @Font(fontHeightInPoints = 20))
private String name;
```
2. 在写入 Excel 文件时,使用 WriteCellStyle 类来设置样式,如下所示:
```java
WriteCellStyle headCellStyle = new WriteCellStyle();
headCellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
headCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
headCellStyle.setWriteFont(new WriteFont());
headCellStyle.getWriteFont().setFontHeightInPoints((short) 20);
writer.setHeadCellStyle(headCellStyle);
```
其中,headCellStyle.setFillForegroundColor() 方法用于设置单元格背景色,headCellStyle.setFillPatternType() 方法用于设置填充模式,headCellStyle.setWriteFont() 方法用于设置字体,headCellStyle.getWriteFont().setFontHeightInPoints() 方法用于设置字体大小。