Articles in this section
Category / Section

How to preserve long number while exporting in XlsIO?

2 mins read

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 `range.IsStringsPreserved` property. Instead of using this property for every range, you can also enable `IWorksheet.IsStringsPreserved` which preserves all the values in worksheet as string.

To know more about `IsStringsPreserved` property, please refer documentation.

Code snippet to preserve long number as string while exporting

worksheet.IsStringsPreserved = true;

 

Download Complete Sample

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

 

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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied