Easyexcel(7-自界说样式)

打印 上一主题 下一主题

主题 677|帖子 677|积分 2031

注解

@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. public abstract class AbstractCellStyleStrategy implements CellWriteHandler {
  2.     @Override
  3.     public int order() {
  4.         return OrderConstant.DEFINE_STYLE;
  5.     }
  6.     @Override
  7.     public void afterCellDispose(CellWriteHandlerContext context) {
  8.         if (context.getHead() == null) {
  9.             return;
  10.         }
  11.         if (context.getHead()) {
  12.             setHeadCellStyle(context);
  13.         } else {
  14.             setContentCellStyle(context);
  15.         }
  16.     }
  17.     /**
  18.      * 设置表头样式
  19.      */
  20.     protected void setHeadCellStyle(CellWriteHandlerContext context) {
  21.         setHeadCellStyle(context.getCell(), context.getHeadData(), context.getRelativeRowIndex());
  22.     }
  23.     /**
  24.      * 设置表头样式
  25.      */
  26.     protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
  27.         throw new UnsupportedOperationException("Custom styles must override the setHeadCellStyle method.");
  28.     }
  29.     /**
  30.      * 设置单元格内容样式
  31.      */
  32.     protected void setContentCellStyle(CellWriteHandlerContext context) {
  33.         setContentCellStyle(context.getCell(), context.getHeadData(), context.getRelativeRowIndex());
  34.     }
  35.     /**
  36.      * 设置单元格内容样式
  37.      */
  38.     protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
  39.         throw new UnsupportedOperationException("Custom styles must override the setContentCellStyle method.");
  40.     }
  41. }
复制代码
HorizontalCellStyleStrategy

HorizontalCellStyleStrategy 是提供的一个水平样式计谋,只需实现CellWriteHandlerContext类自界说样式即可,通过设置构建这个计谋对象根本上可以满足一般的要求了,比如:设置表头和内容的边框、底色、对齐方式、笔墨字体、笔墨颜色、笔墨大小等。设置完之后,需要创建 HorizontalCellStyleStrategy 对象,然后在导出文件时注册这个计谋的 handler 即可。
[code]Getter@Setter@EqualsAndHashCodepublic class HorizontalCellStyleStrategy extends AbstractCellStyleStrategy {    private WriteCellStyle headWriteCellStyle;    private List contentWriteCellStyleList;    public HorizontalCellStyleStrategy() {    }    public HorizontalCellStyleStrategy(WriteCellStyle headWriteCellStyle,        List 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()

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

星球的眼睛

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表