Aligning, colors and borders
This example shows how to use aligning, colors, borders and fill patterns.See result in acb.xls.
#include "libxl.h"
using namespace libxl;
class Example
{
public:
Example()
{
book = xlCreateBook();
sheet = book->addSheet(L"Sheet1");
}
~Example()
{
book->release();
}
void showH(unsigned short row, AlignH align, const wchar_t* alignStr)
{
Format* format = book->addFormat();
format->setAlignH(align);
format->setBorder();
sheet->writeStr(row, 1, alignStr, format);
}
void showV(unsigned short col, AlignV align, const wchar_t* alignStr)
{
Format* format = book->addFormat();
format->setAlignV(align);
format->setBorder();
sheet->writeStr(2, col, alignStr, format);
sheet->setMerge(2, 6, col, col);
}
void showB(unsigned short row, BorderStyle style, const wchar_t* styleStr)
{
Format* format = book->addFormat();
format->setBorder(style);
sheet->writeStr(row, 1, styleStr, format);
}
void showC(unsigned short row, Color color, const wchar_t* colorStr)
{
Font* font = book->addFont();
font->setColor(color);
Format* format = book->addFormat();
format->setBorder();
format->setBorderColor(color);
format->setFont(font);
sheet->writeStr(row, 7, colorStr, format);
}
void showP(unsigned short row, Color color, FillPattern pattern = FILLPATTERN_SOLID)
{
Format* format = book->addFormat();
format->setFillPattern(pattern);
format->setPatternForegroundColor(color);
sheet->writeBlank(row, pattern == FILLPATTERN_SOLID ? 3 : 5, format);
}
void run()
{
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);
showH(2, ALIGNH_LEFT, L"ALIGNH_LEFT");
showH(4, ALIGNH_CENTER, L"ALIGNH_CENTER");
showH(6, ALIGNH_RIGHT, L"ALIGNH_RIGHT");
showV(3, ALIGNV_TOP, L"ALIGNV_TOP");
showV(5, ALIGNV_CENTER, L"ALIGNV_CENTER");
showV(7, ALIGNV_BOTTOM, L"ALIGNV_BOTTOM");
showB(12, BORDERSTYLE_MEDIUM, L"BORDERSTYLE_MEDIUM");
showB(14, BORDERSTYLE_DASHED, L"BORDERSTYLE_DASHED");
showB(16, BORDERSTYLE_DOTTED, L"BORDERSTYLE_DOTTED");
showB(18, BORDERSTYLE_THICK, L"BORDERSTYLE_THICK");
showB(20, BORDERSTYLE_DOUBLE, L"BORDERSTYLE_DOUBLE");
showB(22, BORDERSTYLE_DASHDOT, L"BORDERSTYLE_DASHDOT");
showP(12, COLOR_RED);
showP(14, COLOR_BLUE);
showP(16, COLOR_YELLOW);
showP(18, COLOR_PINK);
showP(20, COLOR_GREEN);
showP(22, COLOR_GRAY25);
showP(12, COLOR_RED, FILLPATTERN_GRAY50);
showP(14, COLOR_BLUE, FILLPATTERN_HORSTRIPE);
showP(16, COLOR_YELLOW, FILLPATTERN_VERSTRIPE);
showP(18, COLOR_PINK, FILLPATTERN_REVDIAGSTRIPE);
showP(20, COLOR_GREEN, FILLPATTERN_THINVERSTRIPE);
showP(22, COLOR_GRAY25, FILLPATTERN_THINHORCROSSHATCH);
showC(12, COLOR_RED, L"COLOR_RED");
showC(14, COLOR_BLUE, L"COLOR_BLUE");
showC(16, COLOR_YELLOW, L"COLOR_YELLOW");
showC(18, COLOR_PINK, L"COLOR_PINK");
showC(20, COLOR_GREEN, L"COLOR_GREEN");
showC(22, COLOR_GRAY25, L"COLOR_GRAY25");
book->save(L"acb.xls");
}
private:
Book* book;
Sheet* sheet;
};
int main()
{
Example example;
example.run();
return 0;
}