Writing formulas

This example writes formulas to Excel sheet. See the result in the formula.xls file.



#include "libxl.h"

using namespace libxl;

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

    Format* alFormat = book->addFormat();
    alFormat->setAlignH(ALIGNH_LEFT);

    Format* arFormat = book->addFormat();
    arFormat->setAlignH(ALIGNH_RIGHT);

    Format* alignDateFormat = book->addFormat(alFormat);
    alignDateFormat->setNumFormat(NUMFORMAT_DATE);

    Font* linkFont = book->addFont();
    linkFont->setColor(COLOR_BLUE);
    linkFont->setUnderline(UNDERLINE_SINGLE);
    Format* linkFormat = book->addFormat(alFormat);
    linkFormat->setFont(linkFont);
   
    Sheet* sheet = book->addSheet(L"Sheet1");

    if(sheet)
    {   
        sheet->setCol(0, 0, 27);
        sheet->setCol(1, 1, 10);

        sheet->writeNum(2, 1, 40, alFormat);
        sheet->writeNum(3, 1, 30, alFormat);
        sheet->writeNum(4, 1, 50, alFormat);

        sheet->writeStr(6, 0, L"SUM(B3:B5) = ", arFormat);        
        sheet->writeFormula(6, 1, L"SUM(B3:B5)", alFormat);        
        sheet->writeStr(7, 0, L"AVERAGE(B3:B5) = ", arFormat);        
        sheet->writeFormula(7, 1, L"AVERAGE(B3:B5)", alFormat);        
        sheet->writeStr(8, 0, L"MAX(B3:B5) = ", arFormat);        
        sheet->writeFormula(8, 1, L"MAX(B3:B5)", alFormat);        
        sheet->writeStr(9, 0, L"MIX(B3:B5) = ", arFormat);        
        sheet->writeFormula(9, 1, L"MIN(B3:B5)", alFormat);
        sheet->writeStr(10, 0, L"COUNT(B3:B5) = ", arFormat);      
        sheet->writeFormula(10, 1, L"COUNT(B3:B5)", alFormat);

        sheet->writeStr(12, 0, L"IF(B7 > 100;\"large\";\"small\") = ", arFormat);      
        sheet->writeFormula(12, 1, L"IF(B7 > 100;\"large\";\"small\")", alFormat);

        sheet->writeStr(14, 0, L"SQRT(25) = ", arFormat);      
        sheet->writeFormula(14, 1, L"SQRT(25)", alFormat);
        sheet->writeStr(15, 0, L"RAND() = ", arFormat);      
        sheet->writeFormula(15, 1, L"RAND()", alFormat);
        sheet->writeStr(16, 0, L"2*PI() = ", arFormat);      
        sheet->writeFormula(16, 1, L"2*PI()", alFormat);

        sheet->writeStr(18, 0, L"UPPER(\"libxl\") = ", arFormat);      
        sheet->writeFormula(18, 1, L"UPPER(\"libxl\")", alFormat);
        sheet->writeStr(19, 0, L"LEFT(\"window\";3) = ", arFormat);      
        sheet->writeFormula(19, 1, L"LEFT(\"window\";3)", alFormat);
        sheet->writeStr(20, 0, L"LEN(\"string\") = ", arFormat);      
        sheet->writeFormula(20, 1, L"LEN(\"string\")", alFormat);

        sheet->writeStr(22, 0, L"DATE(2010;3;11) = ", arFormat);      
        sheet->writeFormula(22, 1, L"DATE(2010;3;11)", alignDateFormat);
        sheet->writeStr(23, 0, L"DAY(B23) = ", arFormat);      
        sheet->writeFormula(23, 1, L"DAY(B23)", alFormat);
        sheet->writeStr(24, 0, L"MONTH(B23) = ", arFormat);      
        sheet->writeFormula(24, 1, L"MONTH(B23)", alFormat);
        sheet->writeStr(25, 0, L"YEAR(B23) = ", arFormat);      
        sheet->writeFormula(25, 1, L"YEAR(B23)", alFormat);
        sheet->writeStr(26, 0, L"DAYS360(B23;TODAY()) = ", arFormat);      
        sheet->writeFormula(26, 1, L"DAYS360(B23;TODAY())", alFormat);

        sheet->writeStr(28, 0, L"B3+100*(2-COS(0)) = ", arFormat);      
        sheet->writeFormula(28, 1, L"B3+100*(2-COS(0))", alFormat);
        sheet->writeStr(29, 0, L"ISNUMBER(B29) = ", arFormat);      
        sheet->writeFormula(29, 1, L"ISNUMBER(B29)", alFormat);
        sheet->writeStr(30, 0, L"AND(1;0) = ", arFormat);      
        sheet->writeFormula(30, 1, L"AND(1;0)", alFormat);
        
        sheet->writeStr(32, 0, L"HYPERLINK() = ", arFormat);
        sheet->writeFormula(32, 1, L"HYPERLINK(\"http://www.libxl.com\")", linkFormat);
    }

    book->save(L"formula.xls");
    book->release();

    return 0;
}