How to maintain leading zeros in WinForms XlsIO Excel cell
Leading zeros will be omitted from the value in an Excel cell, and this is the behavior of Microsoft Excel. There are two ways to maintain leading zeros in an Excel cell. They are:
1. Set the number format as “Text” and then assign the number with leading zeros using the Value property.
C#
worksheet.Range["A1"].NumberFormat = "@"; worksheet.Range["A1"].Value = "0123456789";
VB.NET
worksheet.Range("A1").NumberFormat = "@" worksheet.Range("A1").Value = "0123456789"
2. Set the number format as “0000000000” and then assign the number with leading zeros using the SetValueRowCol method.
C#
worksheet.Range["A2"].NumberFormat = "0000000000"; worksheet.SetValueRowCol("0123456789", 2, 1);
VB.NET
worksheet.Range("A2").NumberFormat = "0000000000" worksheet.SetValueRowCol("0123456789", 2, 1)
The following complete code snippet explains both the above scenarios.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2016;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
// Increasing column width
worksheet.Range["A1"].ColumnWidth = 15;
// Assign the number format as "Text" and then set a number with leading zeros using the Value property
worksheet.Range["A1"].NumberFormat = "@";
worksheet.Range["A1"].Value = "0123456789";
// Assign the number format and then set a number with leading zeros using the SetValueRowCol method
worksheet.Range["A2"].NumberFormat = "0000000000";
worksheet.SetValueRowCol("0123456789", 2, 1);
workbook.SaveAs("Output.xlsx");
System.Diagnostics.Process.Start("Output.xlsx");
}
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Excel2016
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
' Increasing column width
worksheet.Range("A1").ColumnWidth = 15
' Assign the number format as "Text" and then set a number with leading zeros using the Value property
worksheet.Range("A1").NumberFormat = "@"
worksheet.Range("A1").Value = "0123456789"
' Assign the number format and then set a number with leading zeros using the SetValueRowCol method
worksheet.Range("A2").NumberFormat = "0000000000"
worksheet.SetValueRowCol("0123456789", 2, 1)
workbook.SaveAs("Output.xlsx")
System.Diagnostics.Process.Start("Output.xlsx")
End Using
A complete working sample can be downloaded from LeadingZeros.Zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering the Syncfusion license key in your application to use the components without a trial message
Conclusion
I hope you enjoyed learning about how to maintain leading zeros in WinForms XlsIO Excel cell in XlsIO.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!