How to convert CSV to DataTable in C# with WinForms XlsIO?
Syncfusion® WinForms XIsIO’s feature tour page allows exporting sheet data to a DataTable by using the ExportDataTable method. This method provides various options that allow exporting data with specific requirement through ExcelExportDataTableOptions.
The following code snippet illustrates on how to export data from a CSV file to a DataGrid using a 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 DataGrid, likewise it can be bound 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 shown below.
CSV To DataTable
See Also:
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to convert CSV to DataTable in C# with WinForms XlsIO.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!