注解
@ContentStyle
用于设置内容格式注解,可作用于类和字段上
- dataFormat:日期格式
- hidden:设置单元格使用此样式隐藏
- locked:设置单元格使用此样式锁定
- quotePrefix:在单元格前面增加`符号,数字或公式将以字符串形式展示
- horizontalAlignment:设置是否水平居中
- wrapped:设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
- verticalAlignment:设置是否垂直居中
- rotation:设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
- indent:设置单元格中缩进文本的空格数
- borderLeft:设置左边框的样式
- borderRight:设置右边框样式
- borderTop:设置上边框样式
- borderBottom:设置下边框样式
- leftBorderColor:设置左边框颜色
- rightBorderColor:设置右边框颜色
- topBorderColor:设置上边框颜色
- bottomBorderColor:设置下边框颜色
- fillPatternType:设置填充类型
- fillBackgroundColor:设置背景色
- fillForegroundColor:设置前景色
- shrinkToFit:设置自动单元格自动大小
@ContentFontStyle
用于设置单元格内容字体格式的注解,可作用于类和字段上
- fontName:字体名称
- fontHeightInPoints:字体高度
- italic:是否斜体
- strikeout:是否设置删除水平线
- color:字体颜色
- typeOffset:偏移量
- underline:下划线
- bold:是否加粗
- charset:编码格式
@HeadStyle
用于设置标题样式,可作用于类和字段上
- dataFormat:日期格式
- hidden:设置单元格使用此样式隐藏
- locked:设置单元格使用此样式锁定
- quotePrefix:在单元格前面增加`符号,数字或公式将以字符串形式展示
- horizontalAlignment:设置是否水平居中
- wrapped:设置文本是否应换行。将此标志设置为true通过在多行上显示使单元格中的所有内容可见
- verticalAlignment:设置是否垂直居中
- rotation:设置单元格中文本旋转角度。03版本的Excel旋转角度区间为-90°90°,07版本的Excel旋转角度区间为0°180°
- indent:设置单元格中缩进文本的空格数
- borderLeft:设置左边框的样式
- borderRight:设置右边框样式
- borderTop:设置上边框样式
- borderBottom:设置下边框样式
- leftBorderColor:设置左边框颜色
- rightBorderColor:设置右边框颜色
- topBorderColor:设置上边框颜色
- bottomBorderColor:设置下边框颜色
- fillPatternType:设置填充类型
- fillBackgroundColor:设置背景色
- fillForegroundColor:设置前景色
- shrinkToFit:设置自动单元格自动大小
@HeadFontStyle
用于定制标题字体格式,可作用于类和字段上
- fontName:设置字体名称
- fontHeightInPoints:设置字体高度
- italic:设置字体是否斜体
- strikeout:是否设置删除线
- color:设置字体颜色
- typeOffset:设置偏移量
- underline:设置下划线
- charset:设置字体编码
- bold:设置字体是否加粗
类方法

AbstractCellStyleStrategy
通过继承AbstractCellStyleStrategy类,实现其setHeadCellStyle和setContentCellStyle方法可以自定义设置表头和单元格内容样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public abstract class AbstractCellStyleStrategy implements CellWriteHandler {
@Override public int order() { return OrderConstant.DEFINE_STYLE; }
@Override public void afterCellDispose(CellWriteHandlerContext context) { if (context.getHead() == null) { return; } if (context.getHead()) { setHeadCellStyle(context); } else { setContentCellStyle(context); } }
protected void setHeadCellStyle(CellWriteHandlerContext context) { setHeadCellStyle(context.getCell(), context.getHeadData(), context.getRelativeRowIndex()); }
protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) { throw new UnsupportedOperationException("Custom styles must override the setHeadCellStyle method."); }
protected void setContentCellStyle(CellWriteHandlerContext context) { setContentCellStyle(context.getCell(), context.getHeadData(), context.getRelativeRowIndex()); }
protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) { throw new UnsupportedOperationException("Custom styles must override the setContentCellStyle method."); } }
|
HorizontalCellStyleStrategy
HorizontalCellStyleStrategy 是提供的一个水平样式策略,只需实现CellWriteHandlerContext类自定义样式即可,通过设置构建这个策略对象基本上可以满足一般的要求了,比如:设置表头和内容的边框、底色、对齐方式、文字字体、文字颜色、文字大小等。设置完之后,需要创建 HorizontalCellStyleStrategy 对象,然后在导出文件时注册这个策略的 handler 即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| @Getter @Setter @EqualsAndHashCode public class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy {
private WriteCellStyle headWriteCellStyle; private List<WriteCellStyle> contentWriteCellStyleList;
public HorizontalCellStyleStrategy() { }
public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle, List<WriteCellStyle> contentWriteCellStyleList) { this.headWriteCellStyle = headWriteCellStyle; this.contentWriteCellStyleList = contentWriteCellStyleList; }
public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) { this.headWriteCellStyle = headWriteCellStyle; if (contentWriteCellStyle != null) { this.contentWriteCellStyleList = ListUtils.newArrayList(contentWriteCellStyle); } }
@Override protected void setHeadCellStyle(CellWriteHandlerContext context) { if (stopProcessing(context) || headWriteCellStyle == null) { return; } WriteCellData<?> cellData = context.getFirstCellData(); WriteCellStyle.merge(headWriteCellStyle, cellData.getOrCreateStyle()); }
@Override protected void setContentCellStyle(CellWriteHandlerContext context) { if (stopProcessing(context) || CollectionUtils.isEmpty(contentWriteCellStyleList)) { return; } WriteCellData<?> cellData = context.getFirstCellData(); if (context.getRelativeRowIndex() == null || context.getRelativeRowIndex() <= 0) { WriteCellStyle.merge(contentWriteCellStyleList.get(0), cellData.getOrCreateStyle()); } else { WriteCellStyle.merge( contentWriteCellStyleList.get(context.getRelativeRowIndex() % contentWriteCellStyleList.size()), cellData.getOrCreateStyle()); } }
protected boolean stopProcessing(CellWriteHandlerContext context) { return context.getFirstCellData() == null; } }
|
DefaultStyle
Easyexcel在导出文件内容时默认使用DefaultStyle类来设置表头和单元格样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class DefaultStyle extends HorizontalCellStyleStrategy {
@Override public int order() { return OrderConstant.DEFAULT_DEFINE_STYLE; }
public DefaultStyle() { super(); WriteCellStyle headWriteCellStyle = new WriteCellStyle(); headWriteCellStyle.setWrapped(true); headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); headWriteCellStyle.setLocked(true); headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND); headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); headWriteCellStyle.setBorderTop(BorderStyle.THIN); headWriteCellStyle.setBorderBottom(BorderStyle.THIN); headWriteCellStyle.setBorderLeft(BorderStyle.THIN); headWriteCellStyle.setBorderRight(BorderStyle.THIN); WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontName("宋体"); headWriteFont.setFontHeightInPoints((short)14); headWriteFont.setBold(true); headWriteCellStyle.setWriteFont(headWriteFont);
setHeadWriteCellStyle(headWriteCellStyle); } }
|
Easyexcel的默认风格是,对于表头会显示灰色背景,但是如果取消使用默认风格之后,表头的背景颜色就会恢复白色
Easyexcel控制是否开启默认风格的属性方法是:useDefaultStyle(false)
注意:useDefaultStyle方法一定要放在sheet方法前面,否则会失效(放在后面会因为在构造ExcelWriter类时获取不到useDefaultStyle的值,从而导致使用的是默认风格)
1 2 3 4 5 6 7
| public void test01() { EasyExcel.write("test01.xlsx", DemoData.class) .useDefaultStyle(false) .sheet("testSheet01") .doWrite(demoData); }
|

样式工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
public class CustomExcelStyleUtil {
public static WriteCellStyle getHeadStyle() { WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont(); headWriteFont.setFontName("宋体"); headWriteFont.setFontHeightInPoints((short) 14); headWriteFont.setBold(true); headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setBorderBottom(BorderStyle.THIN); headWriteCellStyle.setBottomBorderColor((short) 0); headWriteCellStyle.setBorderLeft(BorderStyle.THIN); headWriteCellStyle.setLeftBorderColor((short) 0); headWriteCellStyle.setBorderRight(BorderStyle.THIN); headWriteCellStyle.setRightBorderColor((short) 0); headWriteCellStyle.setBorderTop(BorderStyle.THIN); headWriteCellStyle.setTopBorderColor((short) 0);
headWriteCellStyle.setWrapped(true);
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); headWriteCellStyle.setShrinkToFit(true);
return headWriteCellStyle; }
public static WriteCellStyle getContentStyle() { WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
WriteFont contentWriteFont = new WriteFont(); contentWriteFont.setFontHeightInPoints((short) 12); contentWriteFont.setFontName("宋体"); contentWriteCellStyle.setWriteFont(contentWriteFont);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN); contentWriteCellStyle.setBottomBorderColor((short) 0); contentWriteCellStyle.setBorderLeft(BorderStyle.THIN); contentWriteCellStyle.setLeftBorderColor((short) 0); contentWriteCellStyle.setBorderRight(BorderStyle.THIN); contentWriteCellStyle.setRightBorderColor((short) 0); contentWriteCellStyle.setBorderTop(BorderStyle.THIN); contentWriteCellStyle.setTopBorderColor((short) 0);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); contentWriteCellStyle.setWrapped(true); contentWriteCellStyle.setShrinkToFit(true);
return contentWriteCellStyle; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Data public class User {
@ExcelProperty(value = "用户Id") private Integer userId;
@ExcelProperty(value = "姓名") private String name;
@ExcelProperty(value = "手机") private String phone;
@ExcelProperty(value = "邮箱") private String email;
@ExcelProperty(value = "创建时间") private Date createTime; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @GetMapping("/download1") public void download1(HttpServletResponse response) { try { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
User user = new User(); user.setUserId(123); user.setName("as"); user.setPhone("15213"); user.setEmail("5456"); user.setCreateTime(new Date()); EasyExcel.write(response.getOutputStream(), User.class) .sheet("模板") .registerWriteHandler(new HorizontalCellStyleStrategy(CustomExcelStyleUtil.getHeadStyle(), CustomExcelStyleUtil.getContentStyle())) .doWrite(Arrays.asList(user)); } catch (Exception e) { e.printStackTrace(); } }
|
