Category / Section
How to display very long numbers in a cell without rounding off in C#, VB.NET?
2 mins read
This article explains how to display very long numbers without rounding off in XlsIO.
Microsoft Excel displays all digits of a numeric value in the cell if its digit count does not exceed the maximum count, with respect to system resolution. Otherwise it is displayed in exponential format and displayed. This is same in XlsIO and is not possible to retrieve the entire cell value as entered, through DisplayText. Therefore, the cell’s number format should be set to text to retrieve the number as entered.
To know more about applying number formats, please refer the documentation.
The following C#/VB complete code snippet explains how to display very long numbers without rounding off using XlsIO.
C#
using Syncfusion.XlsIO; using System.IO; namespace LongNumber_WithoutRounding { class Program { static void Main(string[] args) { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; //Create a new workbook IWorkbook workbook = application.Workbooks.Create(1); //Accessing first worksheet in the workbook IWorksheet worksheet = workbook.Worksheets[0]; //Entering a long number with text number format worksheet.Range["A1"].NumberFormat = "@"; worksheet.Range["A1"].Value = "1234567890987654321"; //Autofit column to view view entire cell contents worksheet.AutofitColumn(1); //Saving the workbook as stream Stream outputStream = File.Create("Output.xlsx"); workbook.SaveAs(outputStream); } } } }
VB
Imports System.IO Imports Syncfusion.XlsIO Namespace LongNumber_WithoutRounding Class Program Private Shared Sub Main(ByVal args() As String) Using excelEngine As ExcelEngine = New ExcelEngine Dim application As IApplication = excelEngine.Excel application.DefaultVersion = ExcelVersion.Excel2013 'Create a new workbook Dim workbook As IWorkbook = application.Workbooks.Create(1) 'Accessing first worksheet in the workbook Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Entering a long number with text number format worksheet.Range("A1").NumberFormat = "@" worksheet.Range("A1").Value = "1234567890987654321" 'Autofit column to view view entire cell contents worksheet.AutofitColumn(1) 'Saving the workbook as stream Dim outputStream As Stream = File.Create("Output.xlsx") workbook.SaveAs(outputStream) End Using End Sub End Class End Namespace