Articles in this section
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

CSV To DataTable

See Also:

Working with Data using XlsIO

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