xlsgen > overview > Number format

Number formatting is the ability to control how a number is displayed by taking advantage of the culture-specific thousand and decimal separators, the amount of decimals as well as a currency symbol. Number format can also be used to format dates, percentages, and even custom formats.

A short listing of the great formatting capabilities is provided below, but let's not regard it as a replacement for the online reference Excel help on that matter.

Months as 1–12 m
Months as 01–12 mm
Months as Jan–Dec mmm
Months as January–December mmmm
Months as the first letter of the month mmmmm
Days as 1–31 d
Days as 01–31 dd
Days as Sun–Sat ddd
Days as Sunday–Saturday dddd
Years as 00–99 yy
Years as 1900–9999 yyyy
Hours as 0–23 H
Hours as 00–23 hh
Minutes as 0–59 m
Minutes as 00–59 mm
Seconds as 0–59 s
Seconds as 00–59 ss
Hours as 4 AM h AM/PM
Time as 4:36 PM h:mm AM/PM
Time as 4:36:03 P h:mm:ss A/P
Elapsed time in hours; for example, 25.02 [h]:mm
Elapsed time in minutes; for example, 63:46 [mm]:ss
Elapsed time in seconds [ss]
Fractions of a second h:mm:ss.00

 

In Excel, number formatting options appear after right-click a cell or group of cells and choosing Format Cell as in :


Some often used number formatting options are also available in the formatting toolbar, as showed below :


 

Reading and writing number formats

Using number formatting options from xlsgen is a breeze. Just pass the definition in the formatting style, and you are done. Below is a sample code that specifies separators and 2 decimals for the number 5 in a cell :

VB code

Dim style As IXlsStyle
Set style = wksht.NewStyle

style.Format = "###,###.00 F"
style.Apply

wksht.Float(3, 2) = 5 ' put 5.00 in row 3, column 2

C# code

IXlsStyle style = wksht.NewStyle();

style.Format = "###,###.00 F";
style.Apply();

wksht.set_Float(3, 2, 5.00); // put 5.00 in row 3, column 2

C/C++ code

xlsgen::IXlsStylePtr style = wksht->NewStyle();

style->Format = L"###,###.00 F";
style->Apply();

wksht->Float[3][2] = 5.00; // put 5.00 in row 3, column 2

Please note custom formatting can be used for instance to take advantage of a particular formatting when the number is positive, and another one when the number is negative. Each of those formattings need to be separated by a semi-colon, as in :

VB code

Dim style As IXlsStyle
Set style = wksht.NewStyle

style.Format = "[Black]###,###.00 F;[Red]###,###.00 F"
style.Apply

wksht.Float(3, 2) = 5 ' put 5.00 in row 3, column 2
wksht.Float(4, 2) = -5 ' put -5.00 in row 4, column 2

C# code

IXlsStyle style = wksht.NewStyle();

style.Format = "[Black]###,###.00 F;[Red]###,###.00 F";
style.Apply();

wksht.set_Float(3, 2, 5.00); // put 5.00 in row 3, column 2
wksht.set_Float(4, 2, -5.00); // put -5.00 in row 4, column 2

C/C++ code

xlsgen::IXlsStylePtr style = wksht->NewStyle();

style->Format = L"[Black]###,###.00 F;[Red]###,###.00 F";
style->Apply();

wksht->Float[3][2] = 5.00; // put 5.00 in row 3, column 2
wksht->Float[4][2] = -5.00; // put -5.00 in row 4, column 2

 

Projecting number formats on data

The xlsgen object model lets you access data and its formatting separately, but you can also evaluate the formatted data means by projecting the number format to the data in a cell. Because number formats may include conditional colors, such as [Red]###,###.00 F, a style instance reflects that color wherever it applies. Here is a source code showing how it works :

VB code

Dim style As IXlsStyle
Set style = wksht.NewStyle

style.Format = "[Black]###,###.00 F;[Red]###,###.00 F"
style.Apply

wksht.Float(3, 2) = 5 ' put 5.00 in row 3, column 2
wksht.Float(4, 2) = -5 ' put -5.00 in row 4, column 2

Dim formatted_positive As String
Set formatted_positive = wksht.FormattedLabel(3,2) ' returns "5.00 F"
Dim formatted_negative As String
Set formatted_negative = wksht.FormattedLabel(4,2) ' returns "-5.00 F"

Dim fontColor_positive
fontColor_positive = wksht.StyleComposedFromLocation(3,2).Font.Color ' returns &H000000 (black)
Dim fontColor_negative
fontColor_negative = wksht.StyleComposedFromLocation(4,2).Font.Color ' returns &HFF0000 (red)

C# code

IXlsStyle style = wksht.NewStyle();

style.Format = "[Black]###,###.00 F;[Red]###,###.00 F";
style.Apply();

wksht.set_Float(3, 2, 5.00); // put 5.00 in row 3, column 2
wksht.set_Float(4, 2, -5.00); // put -5.00 in row 4, column 2

String formatted_positive = wksht.get_FormattedLabel(3,2); // returns "5.00 F"
String formatted_negative = wksht.get_FormattedLabel(4,2); // returns "-5.00 F"

int fontColor_positive = wksht.get_StyleComposedFromLocation(3,2).Font.Color; // returns 0x000000 (black)
int fontColor_negative = wksht.get_StyleComposedFromLocation(4,2).Font.Color; // returns 0xFF0000 (red)

C/C++ code

xlsgen::IXlsStylePtr style = wksht->NewStyle();

style->Format = L"[Black]###,###.00 F;[Red]###,###.00 F";
style->Apply();

wksht->Float[3][2] = 5.00; // put 5.00 in row 3, column 2
wksht->Float[4][2] = -5.00; // put -5.00 in row 4, column 2

_bstr_t formatted_positive = wksht->FormattedLabel[3][2]; // returns "5.00 F"
_bstr_t formatted_negative = wksht->FormattedLabel[4][2]; // returns "-5.00 F"

int fontColor_positive = wksht->StyleComposedFromLocation[3][2]->Font->Color; // returns 0x000000 (black)
int fontColor_negative = wksht->StyleComposedFromLocation[4][2]->Font->Color; // returns 0xFF0000 (red)

In addition to colors, number format parts can hold conditions such as [>100]. When no condition is defined, the parts read as follow : part 1 is for positive numbers, part 2 is for negative numbers, part 3 is for zeros, part 4 applies to generic text. By defining conditions, it is possible to redefine the meaning of parts, therefore apply complicated alert-type formatting.

 

xlsgen documentation. © ARsT Design all rights reserved.