Aligning, colors and borders

This example shows how to use aligning, colors, borders and fill patterns.
See the result in the acb.xlsx file.



#include "libxl.h"

using namespace libxl;

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

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

    sheet->setDisplayGridlines(false);

    sheet->setCol(1, 1, 30);
    sheet->setCol(3, 3, 11.4);
    sheet->setCol(4, 4, 2);
    sheet->setCol(5, 5, 15);
    sheet->setCol(6, 6, 2);
    sheet->setCol(7, 7, 15.4);

    const wchar_t* nameAlignH[] = {L"ALIGNH_LEFT", L"ALIGNH_CENTER", L"ALIGNH_RIGHT"};
    AlignH alignH[] = {ALIGNH_LEFT, ALIGNH_CENTER, ALIGNH_RIGHT};

    for(int i = 0; i < sizeof(nameAlignH) / sizeof(const wchar_t*); ++i)
    {
        Format* format = book->addFormat();
        format->setAlignH(alignH[i]);
        format->setBorder();
        sheet->writeStr(i * 2 + 2, 1, nameAlignH[i], format);
    }    

    const wchar_t* nameAlignV[] = {L"ALIGNV_TOP", L"ALIGNV_CENTER", L"ALIGNV_BOTTOM"};
    AlignV alignV[] = {ALIGNV_TOP, ALIGNV_CENTER, ALIGNV_BOTTOM};

    for(int i = 0; i < sizeof(nameAlignV) / sizeof(const wchar_t*); ++i)
    {
        Format* format = book->addFormat();
        format->setAlignV(alignV[i]);
        format->setBorder();
        sheet->writeStr(2, i * 2 + 3, nameAlignV[i], format);
        sheet->setMerge(2, 6, i * 2 + 3, i * 2 + 3);
    }

    const wchar_t* nameBorderStyle[] = {L"BORDERSTYLE_MEDIUM", L"BORDERSTYLE_DASHED", 
                                        L"BORDERSTYLE_DOTTED", L"BORDERSTYLE_THICK", 
                                        L"BORDERSTYLE_DOUBLE", L"BORDERSTYLE_DASHDOT"};
    BorderStyle borderStyle[] = {BORDERSTYLE_MEDIUM, BORDERSTYLE_DASHED, BORDERSTYLE_DOTTED, 
                                 BORDERSTYLE_THICK, BORDERSTYLE_DOUBLE, BORDERSTYLE_DASHDOT};

    for(int i = 0; i < sizeof(nameBorderStyle) / sizeof(const wchar_t*); ++i)
    {       
        Format* format = book->addFormat();
        format->setBorder(borderStyle[i]);
        sheet->writeStr(i * 2 + 12, 1, nameBorderStyle[i], format);
    }   

    const wchar_t* nameColors[] = {L"COLOR_RED", L"COLOR_BLUE", L"COLOR_YELLOW", 
                                   L"COLOR_PINK", L"COLOR_GREEN", L"COLOR_GRAY25"};
    Color colors[] = {COLOR_RED, COLOR_BLUE, COLOR_YELLOW, COLOR_PINK, COLOR_GREEN, 
                      COLOR_GRAY25};
    FillPattern fillPatterns[] = {FILLPATTERN_GRAY50, FILLPATTERN_HORSTRIPE, 
                                  FILLPATTERN_VERSTRIPE, FILLPATTERN_REVDIAGSTRIPE,
                                  FILLPATTERN_THINVERSTRIPE, FILLPATTERN_THINHORCROSSHATCH};

    for(int i = 0; i < sizeof(nameColors) / sizeof(const wchar_t*); ++i)
    {  
        Format* format1 = book->addFormat();
        format1->setFillPattern(FILLPATTERN_SOLID);
        format1->setPatternForegroundColor(colors[i]);
        sheet->writeBlank(i * 2 + 12, 3, format1);
     
        Format* format2 = book->addFormat();
        format2->setFillPattern(fillPatterns[i]);
        format2->setPatternForegroundColor(colors[i]);
        sheet->writeBlank(i * 2 + 12, 5, format2);
    
        Font* font = book->addFont();
        font->setColor(colors[i]);
        Format* format3 = book->addFormat();
        format3->setBorder();
        format3->setBorderColor(colors[i]);        
        format3->setFont(font);
        sheet->writeStr(i * 2 + 12, 7, nameColors[i], format3);
    }

    book->save(L"acb.xlsx");

    book->release();
    
    return 0;
}