Customizing fonts

This example shows how to use fonts. See result in font.xls.

#include "libxl.h"

using namespace libxl;

class Example
{
public:

    Example()
    {
        book = xlCreateBook();
        sheet = book->addSheet(L"Sheet1");        
    }

    ~Example()
    {
        book->release();
    }

    void showSize(unsigned short row, int size)
    {
        Font* font = book->addFont();
        font->setSize(size);        

        Format* format = book->addFormat();
        format->setFont(font);

        sheet->writeStr(row, 7, L"Text", format);
    }

    void showFont(unsigned short col, const wchar_t* name)
    {
        Font* font = book->addFont();
        font->setSize(16);
        font->setName(name);

        Format* format = book->addFormat();
        format->setFont(font);

        sheet->writeStr(col, 3, name, format);
    }

    void showVertical(unsigned short col)
    {
        Font* font = book->addFont();
        font->setSize(16);

        Format* format = book->addFormat();        
        format->setRotation(255);
        format->setFont(font);

        sheet->writeStr(2, col, L"Vertical", format);
        sheet->setMerge(2, 8, col, col);
    }

    Format* fontToFormat(Font* font)
    {
        Format* format = book->addFormat();
        format->setFont(font);
        return format;
    }

    void run()
    {        
        showFont(2, L"Arial");
        showFont(3, L"Arial Black");
        showFont(4, L"Comic Sans MS");
        showFont(5, L"Courier New");
        showFont(6, L"Impact");
        showFont(7, L"Times New Roman");
        showFont(8, L"Verdana");

        showSize(2, 8);
        showSize(3, 10);
        showSize(4, 12);
        showSize(5, 14);
        showSize(6, 16);
        showSize(7, 20);
        showSize(8, 25);  

        showVertical(9);

        Font* boldFont = book->addFont();
        boldFont->setBold();

        Font* italicFont = book->addFont();
        italicFont->setItalic(); 

        Font* underlineFont = book->addFont();
        underlineFont->setUnderline(UNDERLINE_SINGLE);

        Font* strikeoutFont = book->addFont();
        strikeoutFont->setStrikeOut();
        
        sheet->writeStr(2, 1, L"Normal");
        sheet->writeStr(3, 1, L"Bold", fontToFormat(boldFont));      
        sheet->writeStr(4, 1, L"Italic", fontToFormat(italicFont));      
        sheet->writeStr(5, 1, L"Underline", fontToFormat(underlineFont));      
        sheet->writeStr(6, 1, L"Strikeout", fontToFormat(strikeoutFont));      
                        
        book->save(L"font.xls");
    }

private:
    Book* book;
    Sheet* sheet;
};

int main() 
{
    Example example;
    example.run();

    return 0;
}