Format Excel cell with Accounting number format in C#, VB.NET
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can format Excel cells with accounting number format.
Steps to format Excel cells with accounting number format, programmatically:
Step 1: Create a new C# console application project.
Create a new C# console application project
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
Install NuGet package to the project
Step 3: Include the following namespace in Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
You can set the number format to a cell using the NumberFormat property available under IRange interface.
Step 4: Include the following code snippet in main method of Program.cs file to format Excel cells with accounting number format type.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
//Instantiate the Excel application object
IApplication application = excelEngine.Excel;
//Create a new Excel workbook
IWorkbook workbook = application.Workbooks.Create(1);
//Get the first worksheet in workbook into IWorkbook
IWorksheet worksheet = workbook.Worksheets[0];
//Represent the number formt type
worksheet.Range["A1"].Text = "Accounting Number Format";
worksheet.Range["A1:C1"].Merge();
worksheet.Range["A1"].CellStyle.Font.Bold = true;
//Set the accounting number format to a range of cells
worksheet.Range["B3:B5"].NumberFormat = "_(\"$\"* #,##0_);_(\"$\"* (#,##0);_(\"$\"* \"-\"_);_(@_)";
//Assign data in the worksheet
worksheet.Range["A3"].Text = "Positve Number";
worksheet.Range["B3"].Number = 5;
worksheet.Range["A4"].Text = "Negative Number";
worksheet.Range["B4"].Number = -5;
worksheet.Range["A5"].Text = "Zero";
worksheet.Range["B5"].Number = 0;
//Autofit the column
worksheet.AutofitColumn(1);
//Save the Excel document
workbook.SaveAs("Output.xlsx");
}
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine()
'Instantiate the Excel application object
Dim application As IApplication = excelEngine.Excel
'Create a new Excel workbook
Dim workbook As IWorkbook = application.Workbooks.Create(1)
'Get the first worksheet in workbook into IWorkbook
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Represent the number format type
worksheet.Range("A1").Text = "Accounting Number Format"
worksheet.Range("A1:C1").Merge()
worksheet.Range("A1").CellStyle.Font.Bold = True
'Set the accounting number format to a range of cells
worksheet.Range("B3:E5").NumberFormat = "_(""$""* #,##0_);_(""$""* (#,##0);_(""$""* ""-""_);_(@_)"
'Assign data in the worksheet
worksheet.Range("A3").Text = "Positve Number"
worksheet.Range("B3").Number = 5
worksheet.Range("A4").Text = "Negative Number"
worksheet.Range("B4").Number = -5
worksheet.Range("A5").Text = "Zero"
worksheet.Range("B5").Number = 0
'Autofit the column
worksheet.AutofitColumn(1)
'Save the Excel document
workbook.SaveAs("Output.xlsx")
End Using
A complete working sample to format Excel cells with accounting number format can be downloaded from Accounting-Format.zip.
By executing the program, you will get the output Excel document as follows.
Output Excel document
Take a moment to peruse the documentation, where you will find other options like row and column style, applying number formats to cells, cell text alignment, merging and unmerging of cells, font, color and border setting, HTML string formatting and Rich-Text formatting with code examples.
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to format Excel cells with Accounting number format in C#, VB.NET.
You can refer to our WinForms Excel feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Excel example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!