Number Formats in Excel
In Excel, number formats can be assigned to cells through the Format Cells dialog. To apply a new number format to cells in Excel:
- Select a cell or group of cells.
- From the Format menu, select Cells, or, right-click the highlighted cells and select Format Cells.
- Select the Number tab.
- Select a number format category from the list on the left.
- Set additional attributes (if available) on the right and click Ok.
How to Set Number Formats with ExcelWriter
To assign a number format with ExcelWriter's ExcelApplication, use the Style object's NumberFormat property. The following lines create a percentage style and assign it to a cell:
ExcelWriter includes a set of built-in format constants to help you quickly assign commonly used display formats. For example, the constant NumberFormat.DateFormat.DayOfWeekMonthDayYear represents the format string "dddd, mmmm dd, yyyy". The following assigns this format to a date style:
For a complete list of number format constants, see The NumberFormat Object.
The NumberFormat Object
The NumberFormat object is a helper class that you can use to create custom number format strings. NumberFormat contains methods for creating accounting, currency, fraction, number, percentage, and scientifice display formats. For example, the method NumberFormat.CreateAccounting creates an accounting format string. This method takes three parameters:
numDecimalPlaces | The number of decimal places to display. |
---|---|
useDollarSign | If true, the dollar sign will be displayed. |
negativeColor | A color to use when displaying negative numbers. This parameter may be null. |
The following example creates an accounting style with two decimal places, a dollar sign and negative values in red: