How to preserve long number while exporting in XlsIO?
This article explains how to preserve a long number while exporting using WinForms XlsIO.
A long number can be preserved as a string while exporting, by enabling the `range.IsStringsPreserved` property. Instead of using this property for every range, you can also enable `IWorksheet.IsStringsPreserved`, which preserves all the values in the worksheet as strings.
To know more about the `IsStringsPreserved` property, please refer to the documentation.
Code snippet to preserve a long number as a string while exporting
worksheet.IsStringsPreserved = true;
The following C#/VB complete code explains how to preserve a long number while exporting using XlsIO.
C#
using Syncfusion.XlsIO; using System.Data; using System.IO; namespace Preserve_LongNumber { class Program { static void Main(string[] args) { using (ExcelEngine excelEngine = new ExcelEngine()) { // Instantiate the Excel application object. IApplication application = excelEngine.Excel; // Creating a workbook. IWorkbook workbook = application.Workbooks.Create(1); // The first worksheet object in the worksheets collection is accessed. IWorksheet worksheet = workbook.Worksheets[0]; // Preserving data while exporting worksheet.IsStringsPreserved = true; worksheet["A1"].Value = "123456789987654321"; worksheet["A2"].Value = "TESTXXXXTESTXXXX"; DataTable dt = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.None); // Long number preserved in data table var value1 = dt.Rows[0].ItemArray.GetValue(0); var value2 = dt.Rows[1].ItemArray.GetValue(0); // Saving the workbook Stream stream = File.Create("Output.xlsx"); workbook.SaveAs(stream); } } } }
VB
Imports Syncfusion.XlsIO Imports System.Data Imports System.IO Namespace Preserve_LongNumber Class Program Private Shared Sub Main(ByVal args() As String) Using excelEngine As ExcelEngine = New ExcelEngine() ' Instantiate the excel application object. Dim application As IApplication = excelEngine.Excel ' Creating a workbook. Dim workbook As IWorkbook = application.Workbooks.Create(1) ' The first worksheet object in the worksheets collection is accessed. Dim worksheet As IWorksheet = workbook.Worksheets(0) ' Preserving data while exporting worksheet.IsStringsPreserved = True worksheet("A1").Value = "123456789987654321" worksheet("A2").Value = "TESTXXXXTESTXXXX" Dim dt As DataTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.None) ' Long number preserved in data table Dim value1 = dt.Rows(0).ItemArray.GetValue(0) Dim value2 = dt.Rows(1).ItemArray.GetValue(0) ' Saving the workbook Dim stream As Stream = File.Create("Output.xlsx") workbook.SaveAs(stream) End Using End Sub End Class End Namespace
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to preserve long number while exporting in XlsIO.
You can refer to our WinForms XIsIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms XIsIO documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms 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 WinForms XIsIO and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!