Formatting
Helps to make data representation better based on requirement. Excel offers various formatting methods to make data look and feel great, like colors, background, shapes, images, hyperlinks, adding currency symbols, putting decimals, date time formatting and so on. In this article we will look for basic number formats which we can handle through code dynamically.
In Excel formatting is necessary as makeup for an actress. To represent data with its criticality make others focus so he/she can make sense what you intended for.
Prerequisites
Followings are the prerequisites which you may need to have to build the solution:
- Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
- Create Excel Addin in C# code style (Visual Studio Tools for Office)
- Create a ribbon designer and put button
Data Source

If you notice the Ribbon which is active in depicted screenshot “Chart Work VSTO“, I have placed Five buttons with following labels:
- Bold: will make font bold
- Italic : will make font italic
- Underline : will make font underline
- Fill Color : will fill color in given selected range or cells
- Font Color : will change font color for selected range or cells
C# code to make font Bold
private void btnBold_Click(object sender, RibbonControlEventArgs e) { //Capture user selection excel.Range oRange = Globals.ThisAddIn.Application.Selection; //Make font Bold oRange.Font.Bold = true; }
Output

C# code to make font Italic
private void btn_italic_Click(object sender, RibbonControlEventArgs e) { //Capture user selection excel.Range oRange = Globals.ThisAddIn.Application.Selection; //Make font italic oRange.Font.Italic = true; }
Output

C# code to make font Underline
private void btnUnderline_Click(object sender, RibbonControlEventArgs e) { //Capture user selection excel.Range oRange = Globals.ThisAddIn.Application.Selection; //Make font Underline oRange.Font.Underline = true; }
Output

C# code to fill background Color
private void btnFillColor_Click(object sender, RibbonControlEventArgs e) { //Capture user selection excel.Range oRange = Globals.ThisAddIn.Application.Selection; //Fil Cell color oRange.Interior.Color = excel.XlRgbColor.rgbBlue; }
Output

C# code to change Font Color
private void btnFontColor_Click(object sender, RibbonControlEventArgs e) { excel.Range oRange = Globals.ThisAddIn.Application.Selection; //Make font Underline oRange.Font.Color = excel.XlRgbColor.rgbRed; }
Output

NumberFormat
A range object offers NumberFormat property by which we can apply number formatting over the data. Let’s see an example for all basic formatting which can be applied over cells using VBA, VSTO (Visual Studio Tools for Office) C# and Vb.Net.
VBA code example
Public Sub NumberFormat() 'Capture user selection Dim oRange As Range Set oRange = Selection 'Convert into number oRange.NumberFormat = "0.00" 'Convert into General oRange.NumberFormat = "General" 'Convert into Currency oRange.NumberFormat = "$#,##0.00" 'Convert into Accounting oRange.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" 'Convert into Short Date oRange.NumberFormat = "m/d/yyyy" 'Convert into Long Date oRange.NumberFormat = "[$-x-sysdate]dddd, mmmm dd, yyyy" 'Convert into Time oRange.NumberFormat = "[$-x-systime]h:mm:ss AM/PM" 'Convert into Percentage oRange.NumberFormat = "0.00%" 'Convert into Fraction oRange.NumberFormat = "# ?/?" 'Convert into Scientific oRange.NumberFormat = "0.00E+00" 'Convert into Text oRange.NumberFormat = "@" 'Increase 3 decimal in current Range oRange.NumberFormat = "0.000" 'Decrease 1 decimal in current Range oRange.NumberFormat = "0.00" 'Memory cleanup Set oRange = Nothing End Sub
C# code example
private void btnNumberFormat_Click(object sender, RibbonControlEventArgs e) { //Capture user selection excel.Range oRange = Globals.ThisAddIn.Application.Selection; //Convert into number oRange.NumberFormat="0.00"; //Convert into General oRange.NumberFormat="General"; //Convert into Currency oRange.NumberFormat="$#,##0.00"; //Convert into Accounting oRange.NumberFormat="_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"; //Convert into Short Date oRange.NumberFormat = "m/d/yyyy"; //Convert into Long Date oRange.NumberFormat = "[$-x-sysdate]dddd, mmmm dd, yyyy"; //Convert into Time oRange.NumberFormat = "[$-x-systime]h:mm:ss AM/PM"; //Convert into Percentage oRange.NumberFormat = "0.00%"; //Convert into Fraction oRange.NumberFormat = "# ?/?"; //Convert into Scientific oRange.NumberFormat = "0.00E+00"; //Convert into Text oRange.NumberFormat = "@"; //Increase 3 decimal in current Range oRange.NumberFormat = "0.000"; //Decrease 1 decimal in current Range oRange.NumberFormat = "0.00" }
VB.Net code example
Private Sub btnNumberFormat_Click(sender As Object, e As RibbonControlEventArgs) Handles btnNumberFormat.Click 'Capture user selection Dim oRange As excel.Range oRange= Globals.ThisAddIn.Application.Selection 'Convert into number oRange.NumberFormat="0.00" 'Convert into General oRange.NumberFormat="General" 'Convert into Currency oRange.NumberFormat="$#,##0.00" 'Convert into Accounting oRange.NumberFormat="_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)" 'Convert into Short Date oRange.NumberFormat = "m/d/yyyy" 'Convert into Long Date oRange.NumberFormat = "[$-x-sysdate]dddd, mmmm dd, yyyy" 'Convert into Time oRange.NumberFormat = "[$-x-systime]h:mm:ss AM/PM" 'Convert into Percentage oRange.NumberFormat = "0.00%" 'Convert into Fraction oRange.NumberFormat = "# ?/?" 'Convert into Scientific oRange.NumberFormat = "0.00E+00" 'Convert into Text oRange.NumberFormat = "@" 'Increase 3 decimal in current Range oRange.NumberFormat = "0.000" 'Decrease 1 decimal in current Range oRange.NumberFormat = "0.00" End Sub
Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.
Next: Conditional formatting in Excel VSTO C#