Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更改表头的配置 #391

Open
pocketchew opened this issue Sep 10, 2024 · 3 comments
Open

更改表头的配置 #391

pocketchew opened this issue Sep 10, 2024 · 3 comments

Comments

@pocketchew
Copy link

请问如何在例子

    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
Copy link
Author

pocketchew commented Sep 11, 2024

可以请问如果想要达成像下图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);
}

@wangguanquan
Copy link
Owner

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

@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"));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants