Conditional formatting: creating a gradated color scale on the cells

This example creates a gradated color scale for the range C4:C11.



#include "libxl.h"

using namespace libxl;

int main()
{
    Book* book = xlCreateXMLBook();

    book->setRgbMode(true);
    
    Sheet* sheet = book->addSheet(L"my");

    sheet->writeStr(2, 1, L"Country");
    sheet->writeStr(2, 2, L"Road injures");
    sheet->writeStr(3, 1, L"USA");
    sheet->writeStr(4, 1, L"UK");
    sheet->writeStr(5, 1, L"Switzerland");
    sheet->writeStr(6, 1, L"Spain");
    sheet->writeStr(7, 1, L"Italy");
    sheet->writeStr(8, 1, L"London");
    sheet->writeStr(9, 1, L"Greece");
    sheet->writeStr(10, 1, L"Japan");

    sheet->writeNum(3, 2, 64);
    sheet->writeNum(4, 2, 94);
    sheet->writeNum(5, 2, 88);
    sheet->writeNum(6, 2, 93);
    sheet->writeNum(7, 2, 86);
    sheet->writeNum(8, 2, 75);
    sheet->writeNum(9, 2, 67);
    sheet->writeNum(10, 2, 91);

    ConditionalFormatting* cf = sheet->addConditionalFormatting();
    cf->addRange(3, 10, 2, 2);
    cf->add2ColorScaleRule(book->colorPack(0xFF, 0x71, 0x28), book->colorPack(0xFF, 0xEF, 0x9C));
   
    book->save(L"out.xlsx");

    book->release();
    
    return 0;
}

See also: