Skip to content

4 动态设置样式

guanquan.wang edited this page Nov 21, 2024 · 4 revisions

所谓动态就是根据单元格或行数据不同为每个单元格或者每一行设置不同样式,这个功能可以极大丰富文件的可读性和多样性,算是EEC的个性化功能吧。

使用StyleDesign注解

Java Bean可以使用@StyleDesign注解动态编辑样式(包含字体,填充,边框,格式等),StyleDesign作用于Type,FieldMethod,前者影响整行样式,后两种影响单个Cell

作用于Type

StyleDesign指定的类需要实现StyleProcessor<T>接口,该接口方法有3个参数,第一个为Java Bean,第2个是现有样式,第3个是Styles实例,

@StyleDesign(using = StudentScoreStyle.class)
public static class DesignStudent {
    @ExcelColumn
    private String name;
    @ExcelColumn
    private int score;
}

public static class StudentScoreStyle implements StyleProcessor<DesignStudent> {
    @Override
    public int build(DesignStudent o, int style, Styles st) {
        // 低于60分时背景色标黄
        if (o.getScore() < 60) {
            style = st.modifyFill(style, new Fill(PatternType.solid, Color.orange));
        } else if (o.getScore() > 95) {
            // 粗体+下划线(这里使用clone可以保留原字体和大小)
            Font newFont = st.getFont(style).clone().underline().bold();
            style = st.modifyFont(style, newFont);
        }
        return style;
    }
}

效果如下:95分以上的行字体被加粗并加下划线,低于60分的整行背景色标黄

annotation on type

作用于Field和Method

StyleDesign使用于Field和Method用法与Type完全一样,只是传入的第1个参数变成单元格的值

public static class DesignStudent {
    @StyleDesign(using = NameMatch.class)
    @ExcelColumn
    private String name;
    private int score;

    @StyleDesign(using = ScoreStyle.class)
    @ExcelColumn
    public int getScore() {
        return score;
    }
}

private static final Set<String> VIP_SET = new HashSet<>(Arrays.asList("a", "b", "x"));

public static class NameMatch implements StyleProcessor<String> {
    @Override
    public int build(String name, int style, Styles sst) {
        if (VIP_SET.contains(name)) {
            Font font = sst.getFont(style).clone();
            style = sst.modifyFont(style, font.bold());
        }
        return style;
    }
}

public static class ScoreStyle implements StyleProcessor<Integer> {
    @Override
    public int build(Integer score, int style, Styles st) {
        if (score < 60) {
            style = st.modifyFill(style, new Fill(PatternType.solid, Color.orange));
        }
        return style;
    }
}

annotation on field

使用StyleProcessor

对于ListMapSheet,ResultSetSheet或StatementSheet这三种无法使用注解的Worksheet,EEC提供了setStyleProcessor方法,与StyleDesign一样,可以应用于整行或者单个单元格

作用于Worksheet

new Workbook()
    .addSheet(new ListSheet<>(list
        , new Column("姓名", "name")
        , new Column("数学成绩", "score")
        , new Column("备注", "toString")
    ).setStyleProcessor((o,s,st) -> o.getScore() < 60 ? st.modifyFill(s, new Fill(PatternType.solid, Color.orange)) : s))
    .writeTo(Paths.get("F:/excel"));

低于60分整个单元格标黄 process2

作用于Column

new Workbook()
    .addSheet(new ListSheet<>(list
        , new Column("姓名", "name").setStyleProcessor((n, s, sst) -> Styles.modifyHorizontal(s, Horizontals.CENTER))
        , new Column("数学成绩", "score").setWidth(12D)
        , new Column("备注", "toString").setWidth(25.32D).setWrapText(true)
    )).writeTo(Paths.get("F:/excel"));

上面的代码使用是将“姓名”列设置为居中

process1

动态样式处理就展示这么多,对于样式处理有一定的学习成本,EEC处理样式一定有这两步,第一步是清除当前样式,第二步是添加新的样式。样式之间使用|运算符连接,也可以使用Styles.modifyXX不简化

Clone this wiki locally