Xlnt 库。有没有办法以rgb格式存储excel单元格颜色信息?

问题描述 投票:0回答:1

我找不到任何以 RGB 格式存储单元格颜色数据的函数。我只是想从一个单元格获取颜色并用该颜色自由填充另一个单元格。

为了填充单元格,我使用此函数,但找不到存储颜色的方法。

cell.fill(xlnt::fill::solid(xlnt::rgb_color(242, 34, 15)));

c++ excel colors libraries fill
1个回答
0
投票

最终,答案就在于使用

cell.fill().pattern_fill().foreground().rgb()
。这是检索颜色的最简单方法。但是,我还创建了您仅作为示例描述的实现。

//This is what you asked for
xlnt::rgb_color get_cell_color(const xlnt::cell &cell) {
    if (cell.has_fill() && cell.fill().type() == xlnt::fill_type::solid) {
        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);
© www.soinside.com 2019 - 2024. All rights reserved.