Using number formats
This example shows how to use built-in and custom number formats. See result in numformat.xls.#include "libxl.h"
using namespace libxl;
class NumFormatExample
{
public:
NumFormatExample()
{
book = xlCreateBook();
sheet = book->addSheet(L"Sheet1");
centerFormat = book->addFormat();
centerFormat->setAlignH(ALIGNH_CENTER);
}
~NumFormatExample()
{
book->release();
}
void show(unsigned short row, double value, NumFormat numFormat, const wchar_t* numFormatStr)
{
Format* format = book->addFormat();
format->setNumFormat(numFormat);
sheet->writeNum(row, 0, value);
sheet->writeStr(row, 1, numFormatStr, centerFormat);
sheet->writeNum(row, 2, value, format);
}
void show(unsigned short row, double value, const wchar_t* numFormatStr)
{
Format* format = book->addFormat();
format->setNumFormat(book->addCustomNumFormat(numFormatStr));
sheet->writeNum(row, 0, value);
sheet->writeStr(row, 1, numFormatStr, centerFormat);
sheet->writeNum(row, 2, value, format);
}
void run()
{
sheet->setCol(0, 0, 12);
sheet->setCol(1, 1, 38);
sheet->setCol(2, 2, 10);
// built-in number formats
show(3, 2.5681, NUMFORMAT_NUMBER_D2, L"NUMFORMAT_NUMBER_D2");
show(4, 2500000, NUMFORMAT_NUMBER_SEP, L"NUMFORMAT_NUMBER_SEP");
show(5, -500, NUMFORMAT_CURRENCY_NEGBRA, L"NUMFORMAT_CURRENCY_NEGBRA");
show(6, -0.25, NUMFORMAT_PERCENT, L"NUMFORMAT_PERCENT");
show(7, 890, NUMFORMAT_SCIENTIFIC_D2, L"NUMFORMAT_SCIENTIFIC_D2");
show(8, 0.75, NUMFORMAT_FRACTION_ONEDIG, L"NUMFORMAT_FRACTION_ONEDIG");
show(9, book->datePack(2010, 3, 11), NUMFORMAT_DATE, L"NUMFORMAT_DATE");
show(10, book->datePack(2010, 3, 11), NUMFORMAT_CUSTOM_MON_YY, L"NUMFORMAT_CUSTOM_MON_YY");
// custom number formats
show(12, 20.5627, L"#.###");
show(13, 4.8, L"#.00");
show(14, 1.23, L"0.00 \"dollars\"");
show(15, 60, L"[Red][<=100];[Green][>100]");
book->save(L"numformat.xls");
}
private:
Book* book;
Sheet* sheet;
Format* centerFormat;
};
int main()
{
NumFormatExample example;
example.run();
return 0;
}