|
| 1 | +--- |
| 2 | +title: Preserve leading zeros when importing data to Excel | Syncfusion |
| 3 | +description: Discover how to keep leading zeros in text when importing a DataTable to Excel using Syncfusion .NET Excel library. |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to preserve leading zeros when importing DataTable to Excel? |
| 10 | + |
| 11 | +Excel often treats numeric-looking text as numbers, removing leading zeros. The same behavior is followed when importing DataTable to Excel by the Syncfusion .NET Excel (XlsIO) library. The leading zeros can be preserved by providing the parameter "PreserveDataTypes" in the ImportDataTable method as "True" when importing the data. |
| 12 | + |
| 13 | +The following code snippet shows how to use the PreserveDataTypes option in XlsIO. |
| 14 | + |
| 15 | +{% tabs %} |
| 16 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 17 | + |
| 18 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 19 | +{ |
| 20 | + IApplication application = excelEngine.Excel; |
| 21 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 22 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 23 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 24 | + |
| 25 | + #region Import from Data Table |
| 26 | + //Initialize the DataTable |
| 27 | + DataTable table = SampleDataTable(); |
| 28 | + //Set the final parameter (preserveTypes) to true to preserve the data types. |
| 29 | + worksheet.ImportDataTable(table, true, 1, 1, true); |
| 30 | + #endregion |
| 31 | + |
| 32 | + #region Save |
| 33 | + //Saving the workbook |
| 34 | + workbook.SaveAs(Path.GetFullPath("Output/ImportDataTable.xlsx")); |
| 35 | + #endregion |
| 36 | +} |
| 37 | +static DataTable SampleDataTable() |
| 38 | +{ |
| 39 | + //Create a DataTable with four columns |
| 40 | + DataTable table = new DataTable(); |
| 41 | + table.Columns.Add("Dosage", typeof(int)); |
| 42 | + table.Columns.Add("Drug", typeof(string)); |
| 43 | + table.Columns.Add("Patient", typeof(string)); |
| 44 | + table.Columns.Add("Date", typeof(DateTime)); |
| 45 | + |
| 46 | + //Add five DataRows |
| 47 | + table.Rows.Add(25, "032132", "David", DateTime.Now); |
| 48 | + table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); |
| 49 | + table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); |
| 50 | + table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); |
| 51 | + table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now); |
| 52 | + |
| 53 | + return table; |
| 54 | +} |
| 55 | + |
| 56 | +{% endhighlight %} |
| 57 | + |
| 58 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 59 | + |
| 60 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 61 | +{ |
| 62 | + IApplication application = excelEngine.Excel; |
| 63 | + application.DefaultVersion = ExcelVersion.Excel2016; |
| 64 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 65 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 66 | + |
| 67 | + //Initialize the DataTable |
| 68 | + DataTable table = SampleDataTable(); |
| 69 | + |
| 70 | + //Set the final parameter (preserveTypes) to true to preserve the data types. |
| 71 | + worksheet.ImportDataTable(table, true, 1, 1, true); |
| 72 | + |
| 73 | + workbook.SaveAs("ImportFromDT.xlsx"); |
| 74 | +} |
| 75 | +private static DataTable SampleDataTable() |
| 76 | +{ |
| 77 | + //Create a DataTable with four columns |
| 78 | + DataTable table = new DataTable(); |
| 79 | + table.Columns.Add("Dosage", typeof(int)); |
| 80 | + table.Columns.Add("Drug", typeof(string)); |
| 81 | + table.Columns.Add("Patient", typeof(string)); |
| 82 | + table.Columns.Add("Date", typeof(DateTime)); |
| 83 | + |
| 84 | + //Add five DataRows |
| 85 | + table.Rows.Add(25, "032132", "David", DateTime.Now); |
| 86 | + table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); |
| 87 | + table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); |
| 88 | + table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); |
| 89 | + table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now); |
| 90 | + |
| 91 | + return table; |
| 92 | +} |
| 93 | + |
| 94 | +{% endhighlight %} |
| 95 | + |
| 96 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 97 | + |
| 98 | +Using excelEngine As ExcelEngine = New ExcelEngine() |
| 99 | + Dim application As IApplication = excelEngine.Excel |
| 100 | + application.DefaultVersion = ExcelVersion.Excel2016 |
| 101 | + Dim workbook As IWorkbook = application.Workbooks.Create(1) |
| 102 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 103 | + |
| 104 | + 'Initialize the DataTable |
| 105 | + Dim table As DataTable = sampleDataTable() |
| 106 | + 'Set the final parameter (preserveTypes) to true to preserve the data types. |
| 107 | + worksheet.ImportDataTable(table, True, 1, 1, True) |
| 108 | + |
| 109 | + workbook.SaveAs("ImportFromDT.xlsx") |
| 110 | +End Using |
| 111 | +Private Function SampleDataTable() As DataTable |
| 112 | + ' Create a DataTable with four columns |
| 113 | + Dim table As New DataTable() |
| 114 | + table.Columns.Add("Dosage", GetType(Integer)) |
| 115 | + table.Columns.Add("Drug", GetType(String)) |
| 116 | + table.Columns.Add("Patient", GetType(String)) |
| 117 | + table.Columns.Add("Date", GetType(Date)) |
| 118 | + |
| 119 | + ' Add five DataRows |
| 120 | + table.Rows.Add(25, "032132", "David", DateTime.Now) |
| 121 | + table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now) |
| 122 | + table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now) |
| 123 | + table.Rows.Add(21, "Combivent", "Janet", DateTime.Now) |
| 124 | + table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now) |
| 125 | + Return table |
| 126 | +End Function |
| 127 | + |
| 128 | +{% endhighlight %} |
| 129 | +{% endtabs %} |
| 130 | + |
| 131 | +## See Also |
| 132 | + |
| 133 | +* [How to import data table with its data type using template markers?](https://help.syncfusion.com/document-processing/excel/excel-library/net/faqs/how-to-import-data-table-with-its-data-type-using-template-markers) |
| 134 | +* [Import to Excel Document](https://help.syncfusion.com/document-processing/excel/excel-library/net/import-export/import-to-excel) |
0 commit comments