eec icon indicating copy to clipboard operation
eec copied to clipboard

更改表头的配置

Open pocketchew opened this issue 1 year ago • 3 comments

请问如何在例子

    new Workbook()
        .setAutoSize(true)
        .addSheet(new ListSheet<Item>("Item"
            , new Column("ID", "id")
            , new Column("NAME", "name"))
            .setData(expectList))
        .writeTo(defaultTestPath.resolve(fileName));
        

在其中一个表头配置如wraptext,horizontal, vertical, font, fill 等 尝试过了Column.setWrapText, setStyleProcessor等 只会更改表头下的数据的配置 而不是表头自己的配置

pocketchew avatar Sep 10 '24 03:09 pocketchew

https://github.com/wangguanquan/eec/wiki/4-%E9%9D%99%E6%80%81%E8%AE%BE%E7%BD%AE%E6%A0%B7%E5%BC%8F

wangguanquan avatar Sep 10 '24 06:09 wangguanquan

可以请问如果想要达成像下图A列的形式 每个表头有各自不同的字体颜色和bgFill 应该要怎么写 image

暂时尝试的做法是建了个新的ListSheet, 然后在里面建了以下方法在construct ListSheet时调用 想要根据headerStyle的参数来配置表头 可是发现更改Font的颜色不会影响到表头的字体颜色

public void setColumnHeaderStyle(Column column, HeaderStyle headerStyle) {
    column.setWidth(headerStyle.width());
    column.setHeaderHeight(headerStyle.height());
    column.setHeaderStyle(buildHeaderStyle(headerStyle));
}

public int buildHeaderStyle(HeaderStyle headerStyle) {
    final int fontsize = 12;
    final int cachecolor = 191;
    Styles styles = Styles.create();
    int style = styles.addFont(new Font("宋体", fontsize, Font.Style.BOLD, Styles.toColor("black")))
            | styles.addNumFmt(NumFmt.of("@")) | styles.addFill(Fill.parse("#E9EAEC"))
            | styles.addBorder(new Border(BorderStyle.THIN, new Color(cachecolor, cachecolor, cachecolor)))
            | headerStyle.vertical().getConfig() | headerStyle.horizontal().getConfig();
    return styles.modifyWrapText(style, true);
}

pocketchew avatar Sep 11 '24 10:09 pocketchew

以下是示例代码,参照上图设置的样式

@Test public void test() throws IOException {
    Workbook workbook = new Workbook();
    Styles styles = workbook.getStyles();
    // 将字体和填充预先添加到样式表(这里也可以不添加,实例化Column时再处理也可以)
    int fontYahei20Red = styles.addFont(new Font("微软雅黑", 20, Color.RED));
    int fontYahei20Black = styles.addFont(new Font("微软雅黑", 20, Color.BLACK));
    int fontYahei20Blue = styles.addFont(new Font("微软雅黑", 20, Color.BLUE));
    int fillGrey = styles.addFill(new Fill(Styles.toColor("#E9EAEC")));
    int fillYellow = styles.addFill(new Fill(Color.YELLOW));

    // 设置表头样式,多重表头使用addSubColumn添加
   // 列高可以设置在第一列的Column上使用方法setHeaderHeight设置列高
   // 行宽使用setWidth方法
    workbook.addSheet(new ListSheet<>(new Column("headerlonglong").setHeaderStyle(fontYahei20Red | fillGrey | Horizontals.LEFT).setHeaderHeight(40)
            .addSubColumn(new Column("middle column").setHeaderStyle(fontYahei20Black | fillYellow | Horizontals.CENTER).setHeaderHeight(30))
            .addSubColumn(new Column("StuName", "stuName").setHeaderStyle(fontYahei20Blue | fillGrey | Horizontals.CENTER).setHeaderHeight(20).setWidth(50))
        , new Column("header3").setHeaderStyle(fontYahei20Black | fillGrey | Horizontals.CENTER)
            .addSubColumn(new Column("header2").setHeaderStyle(fontYahei20Black | fillGrey | Horizontals.CENTER))
            .addSubColumn(new Column("header3").setHeaderStyle(fontYahei20Black | fillGrey | Horizontals.CENTER)).setWidth(30)));

    workbook.writeTo(Paths.get(".测试.xlsx"));
}

wangguanquan avatar Sep 11 '24 12:09 wangguanquan