Xlnt库,有没有一种方法可以将excel单元格的颜色信息存储为rgb格式?

wribegjk  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(79)

我找不到任何以rgb格式存储单元格颜色数据的函数。我只是想从一个单元格中获取颜色,然后用这种颜色自由地填充另一个单元格。
为了填充单元格,我使用了这个函数,但是找不到存储颜色的方法。cell.fill(xlnt::fill::solid(xlnt::rgb_color(242, 34, 15)));

yx2lnoni

yx2lnoni1#

最终,答案就在于使用cell.fill().pattern_fill().foreground().rgb()。这是检索颜色的最简单方法。然而,我也创建了您所描述的实现,只是作为示例。

//This is what you asked for
xlnt::rgb_color get_cell_color(const xlnt::cell &cell) {
    if (cell.fill().type() == xlnt::fill_type::pattern) {
        xlnt::pattern_fill pattern = cell.fill().pattern_fill();
        if (pattern.foreground().type() == xlnt::color_type::rgb) {
            return pattern.foreground().rgb();
        }
    }
    return xlnt::rgb_color(0, 0, 0); // Default to black if no color is found
}

//I took the function you used to fill, and I made it fit in a little bit better 
void color_fill_cell(xlnt::cell &cell, const xlnt::rgb_color &rgb) {
    cell.fill(xlnt::fill::solid(rgb));
}

字符串
现在我们有了这些函数,我们可以很容易地实现它们:

xlnt::worksheet ws = ; // Just plop your worksheet here

xlnt::cell source_cell = ws.cell("A1"); //Obv A1 is a placeholder
xlnt::rgb_color rgb = get_cell_color(source_cell);

xlnt::cell target_cell = ws.cell("B1"); //placeholder again
color_fill_cell(target_cell, rgb);

相关问题