Importing and exporting are some of the basic operations used in any sort
of development. So, for this purpose, I have created a very simple C#.NET
library for importing/exporting the CSV data using Datatable as a primary data structure. You can use this library into your C#.NET project that supports
Datatable data structure. This library imports CSV files with or without
header and with any number of columns into C#.NET Datatable structure.
The import function will automatically detect the number of columns of
the CSV file. The export method will export your data from C#.NET Datatable
data structure to .csv format file.
Today, I shall be
demonstrating the basic use of my .NET library for importing/exporting CSV data using Datatable as a primary data structure.

Prerequisites
- Install CSVLibraryAK NuGet library.
- Knowledge of C# Programming.
Let's begin now.
Step 1
Create a new Console Application project and name it "CSVImportExport".
Step 2
Install 'CSVLibraryAK' Nuget library into your project.
Step 3
Create "CSVImportExport.cs" file and replace following code in it i.e.
- ...
- using CSVLibraryAK;
- ...
- public static void Main(string[] args)
- {
- // Initialization.
- bool hasHeader = true;
- string importFilePath = "E:\\import.csv";
- string exportFilePath = "E:\\export.csv";
- // Impot CSV file.
- DataTable data = CSVLibraryAK.Import(importFilePath, hasHeader);
- // Export CSV file.
- CSVLibraryAK.Export(exportFilePath, data);
- }
- ...
Step 4
Now, execute the project and you will be able to see the export file in your target destination folder.
Conclusion
In this article, you will learn to use my .NET library for importing/exporting CSV data using Datatable as a primary data structure. You will also learn to import/export your target CSV file data.

RaviPosted Jun 21, 2019, 12:42 AM
I checked your Import code and I can see that your code will fail when CSV have enclosed cell value (surrounded by " -quotes) with comma (considering only Comma as delimeter). Please improve your code to take care of various scenario and not just sunny day ones.