How to preserve data types during ImportDataTable in C#, VB.NET?
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);
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
I hope you enjoyed learning about how to preserve data types during ImportDataTable in C#, VB.NET.
You can refer to our .NET Excel library page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Excel library example to understand how to create and manipulate data.
For current customers, you can check out our 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 other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedbackportal. We are always happy to assist you!