Excel Number Style in C#, VB.NET

  • Demo
  • C# source
  • VB.Net source


The sample demonstrates how to set number style in an excel workbook.



using System.Drawing;
using Spire.Xls;

namespace Style_Number
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\..\..\Data\Blank.xls",ExcelVersion.Version97to2003);

            Worksheet worksheet = workbook.Worksheets[0];

            //set the text of range
            worksheet.Range["A1"].Text = "Number";
            worksheet.Range["B1"].Text = "Format";
            worksheet.Range["C1"].Text = "Value";
            worksheet.Range["A2:A6"].Text = "9875.65";

            worksheet.Range["A1:C1"].Style.Font.IsBold = true;
            
            worksheet.Range["B2"].Text = "0";
            worksheet.Range["C2"].NumberValue = 9875.65;
            worksheet.Range["C2"].NumberFormat = "0";

            worksheet.Range["B3"].Text = "0.0";
            worksheet.Range["C3"].NumberValue = 9875.65;
            worksheet.Range["C3"].NumberFormat = "0.0";

            worksheet.Range["B4"].Text = "0.00";
            worksheet.Range["C4"].NumberValue = 9875.65;
            worksheet.Range["C4"].NumberFormat = "0.00";

            worksheet.Range["B5"].Text = "0.00E+00";
            worksheet.Range["C5"].NumberValue = 9875.65;
            worksheet.Range["C5"].NumberFormat = "0.00E+00";

            worksheet.Range["B6"].Text = "#,###.0";
            worksheet.Range["C6"].NumberValue = 9875.65;
            worksheet.Range["C6"].NumberFormat = "#,###.0";

            worksheet.Range["A7"].Text = "987.65";
            worksheet.Range["B7"].Text = "0.0%";
            worksheet.Range["C7"].NumberValue = 987.65;
            worksheet.Range["C7"].NumberFormat = "0.0%";

            //set the style 
            worksheet.Range["A2:C7"].Style.Font.Color = Color.Red;
            worksheet.Range["A1:C1"].Style.Color = Color.SkyBlue;
            worksheet.Range["A2:C7"].Style.Color = Color.Silver;
            worksheet.AllocatedRange.Borders.LineStyle = LineStyleType.Thin;
            worksheet.AllocatedRange.Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
            worksheet.AllocatedRange.Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;

            worksheet.AutoFitColumn(1);
            worksheet.AutoFitColumn(2);

            workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}

Imports System.Drawing
Imports Spire.Xls

Namespace Style_Number
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("..\..\..\..\Data\Blank.xls",ExcelVersion.Version97to2003)

			Dim worksheet As Worksheet = workbook.Worksheets(0)

			'set the text of range
			worksheet.Range("A1").Text = "Number"
			worksheet.Range("B1").Text = "Format"
			worksheet.Range("C1").Text = "Value"
			worksheet.Range("A2:A6").Text = "9875.65"

			worksheet.Range("A1:C1").Style.Font.IsBold = True

			worksheet.Range("B2").Text = "0"
			worksheet.Range("C2").NumberValue = 9875.65
			worksheet.Range("C2").NumberFormat = "0"

			worksheet.Range("B3").Text = "0.0"
			worksheet.Range("C3").NumberValue = 9875.65
			worksheet.Range("C3").NumberFormat = "0.0"

			worksheet.Range("B4").Text = "0.00"
			worksheet.Range("C4").NumberValue = 9875.65
			worksheet.Range("C4").NumberFormat = "0.00"

			worksheet.Range("B5").Text = "0.00E+00"
			worksheet.Range("C5").NumberValue = 9875.65
			worksheet.Range("C5").NumberFormat = "0.00E+00"

			worksheet.Range("B6").Text = "#,###.0"
			worksheet.Range("C6").NumberValue = 9875.65
			worksheet.Range("C6").NumberFormat = "#,###.0"

			worksheet.Range("A7").Text = "987.65"
			worksheet.Range("B7").Text = "0.0%"
			worksheet.Range("C7").NumberValue = 987.65
			worksheet.Range("C7").NumberFormat = "0.0%"

			'set the style 
			worksheet.Range("A2:C7").Style.Font.Color = Color.Red
			worksheet.Range("A1:C1").Style.Color = Color.SkyBlue
			worksheet.Range("A2:C7").Style.Color = Color.Silver
			worksheet.AllocatedRange.Borders.LineStyle = LineStyleType.Thin
			worksheet.AllocatedRange.Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
			worksheet.AllocatedRange.Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None

			worksheet.AutoFitColumn(1)
			worksheet.AutoFitColumn(2)

			workbook.SaveToFile("sample.xls", ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start(workbook.FileName)
		End Sub
	End Class
End Namespace