Articles in this section
Category / Section

How to preserve data types during ImportDataTable in C#, VB.NET?

2 mins read

This article explains how to preserve data types while importing DataTable to worksheet.

While importing a DataTable to a worksheet using XlsIO, the data types can be preserved by setting the data type of column with values in the DataTable as string and preserveTypes as true in ImportDataTable method.

Code snippet to preserve a data types while importing DataTable to worksheet

//Importing a DataTable to Worksheet
worksheet.ImportDataTable(dt, true, 1, 1, -1, -1, true);

Download complete sample

To know more about importing and exporting data, please refer the documentation.

The following C#/VB complete code snippet illustrates how to preserve data types while importing DataTable to worksheet.

C#

using System;
using System.Data;
using System.IO;
using Syncfusion.XlsIO;
 
namespace PreserveLongNumber
{
    class Program
    {
            static void Main(string[] args)
 {
     using (ExcelEngine excelEngine = new ExcelEngine())
     {
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
 
  //Create new workbook
  IWorkbook workbook = application.Workbooks.Create(1);
 
  //Accessing first worksheet in the workbook
  IWorksheet worksheet = workbook.Worksheets[0];
 
  //Creating a DataTable to import
  DataTable dt = new DataTable();
  dt.Columns.Add("Name", typeof(string));
  dt.Columns.Add("Number", typeof(long));
  dt.Columns.Add("StringNumber", typeof(string));
  dt.Columns.Add("StringDate", typeof(string));
  dt.Columns.Add("Date", typeof(DateTime));
 
  DataRow dr1 = dt.NewRow();
  dr1["Name"] = "aaa";
  dr1["Number"] = 6755510089231745873;
  dr1["StringNumber"] = "6755510089231745873";
  dr1["Date"] = DateTime.Now.ToString();
  dr1["StringDate"] = DateTime.Now.ToString();
  dt.Rows.Add(dr1);
 
  DataRow dr2 = dt.NewRow();
  dr2["Name"] = "bbb";
  dr2["Number"] = 6755510089231745873;
  dr2["StringNumber"] = "6755510089231745873";
  dr2["Date"] = DateTime.Now.ToString();
  dr2["StringDate"] = DateTime.Now.ToString();
  dt.Rows.Add(dr2);
 
  DataRow dr3 = dt.NewRow();
  dr3["Name"] = "ccc";
  dr3["Number"] = 6755510089231745873;
  dr3["StringNumber"] = "6755510089231745873";
  dr3["Date"] = DateTime.Now.ToString();
  dr3["StringDate"] = DateTime.Now.ToString();
  dt.Rows.Add(dr3);
 
  //Importing a DataTable to Worksheet
  worksheet.ImportDataTable(dt, true, 1, 1, -1, -1, true);
 
  //Save the workbook as stream
  Stream stream = File.Create("Output.xlsx");
  workbook.SaveAs(stream);
     }
           }
     }
}

VB

Imports Syncfusion.XlsIO
Imports System
Imports System.Data
Imports System.IO
 
Namespace PreserveLongNumber
    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 new workbook
  Dim workbook As IWorkbook = application.Workbooks.Create(1)
 
  'Accessing first worksheet in the workbook
  Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
  'Creating a DataTable to import
  Dim dt As DataTable = New DataTable
  dt.Columns.Add("Name", GetType(System.String))
  dt.Columns.Add("Number", GetType(System.Int64))
  dt.Columns.Add("StringNumber", GetType(System.String))
  dt.Columns.Add("StringDate", GetType(System.String))
  dt.Columns.Add("Date", GetType(DateTime))
 
  Dim dr1 As DataRow = dt.NewRow
  dr1("Name") = "aaa"
  dr1("StringNumber") = "6755510089231745873"
  dr1("Date") = DateTime.Now.ToString
  dr1("StringDate") = DateTime.Now.ToString
  dt.Rows.Add(dr1)
 
  Dim dr2 As DataRow = dt.NewRow
  dr2("Name") = "bbb"
  dr2("StringNumber") = "6755510089231745873"
  dr2("Date") = DateTime.Now.ToString
  dr2("StringDate") = DateTime.Now.ToString
  dt.Rows.Add(dr2)
 
  Dim dr3 As DataRow = dt.NewRow
  dr3("Name") = "ccc"
  dr3("StringNumber") = "6755510089231745873"
  dr3("Date") = DateTime.Now.ToString
  dr3("StringDate") = DateTime.Now.ToString
  dt.Rows.Add(dr3)
 
  'Importing a DataTable to Worksheet
  worksheet.ImportDataTable(dt, True, 1, 1, -1, -1, True)
 
  'Save the workbook as stream
  Dim stream As Stream = File.Create("Output.xlsx")
  workbook.SaveAs(stream)
 End Using
        End Sub
    End Class
End Namespace

 

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