Category / Section
How to convert CSV to DataTable in C#?
1 min read
Syncfusion XlsIO allows to export the sheet data to a DataTable by using the ExportDataTable method. This method provides various options that allows to export data with specific requirement through ExcelExportDataTableOptions.
The following code snippet illustrates on how to export data from CSV file to DataGrid using DataTable.
using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Xlsx; IWorkbook workbook = application.Workbooks.Open("../../Data/CSVFile.csv"); IWorksheet worksheet = workbook.Worksheets[0]; //Read data from the worksheet and Export to the DataTable DataTable customersTable = worksheet.ExportDataTable(worksheet.UsedRange, ExcelExportDataTableOptions.ColumnNames); //Binding exported DataTable to data grid, likewise it can binded to any //user interface control which supports binding DataGrid dataGrid = new DataGrid(); dataGrid.DataSource = customersTable; workbook.Close(); }
A complete working sample can be downloaded from CSVToDataTable.zip.
On executing the sample, you will get the data in DataGrid as below.
CSV To DataTable
See Also: