using IronXL;
using System;
using System.Linq;
// Load an existing WorkSheet
WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// Set data display format to cell
// The cell value will look like 12300%
workSheet["A2"].Value = 123;
workSheet["A2"].FormatString = "0.0%";
// The cell value will look like 123.0000
workSheet["A2"].First().FormatString = "0.0000";
// Set data display format to range
DateTime dateValue = new DateTime(2020, 1, 1, 12, 12, 12);
workSheet["A3"].Value = dateValue;
workSheet["A4"].First().Value = new DateTime(2022, 3, 3, 10, 10, 10);
workSheet["A5"].First().Value = new DateTime(2021, 2, 2, 11, 11, 11);
var range = workSheet["A3:A5"];
// The cell(A3) value will look like 1/1/2020 12:12:12 PM
range.FormatString = "MM/dd/yy h:mm:ss";
workBook.SaveAs("numberFormats.xls");
Imports IronXL
Imports System
Imports System.Linq
' Load an existing WorkSheet
Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Set data display format to cell
' The cell value will look like 12300%
Private workSheet("A2").Value = 123
Private workSheet("A2").FormatString = "0.0%"
' The cell value will look like 123.0000
workSheet("A2").First().FormatString = "0.0000"
' Set data display format to range
Dim dateValue As New DateTime(2020, 1, 1, 12, 12, 12)
workSheet("A3").Value = dateValue
workSheet("A4").First().Value = New DateTime(2022, 3, 3, 10, 10, 10)
workSheet("A5").First().Value = New DateTime(2021, 2, 2, 11, 11, 11)
Dim range = workSheet("A3:A5")
' The cell(A3) value will look like 1/1/2020 12:12:12 PM
range.FormatString = "MM/dd/yy h:mm:ss"
workBook.SaveAs("numberFormats.xls")
Install-Package IronXL.Excel
Excel Number Format
We can use FormatString property to format the display value of any Excel Cell or Range in C# with IronXL.
Using workSheet ["A2"] will only select Range of the specified address. To access Cell, the First() method has to be used. However, FormatString property can be set from both Cell and Range. Therefore, the First() method can be taken out completely. More Excel number formats can be applied using the same method as the code example have shown.