Header Ads

C#.Net Import/Export CSV Library

Importing/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 CSV data using Datatable as a primary data structure. You can use this library into your any C#.NET project that supports Datatable data structure. This library imports CSV file with or without header and with any number of columns into C#.NET Datatable structure. The import function will automatically detects the number of columns of the CSV file. 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:

Following are some prerequisites before you proceed any further in this tutorial:
  1. Install CSVLibraryAK NuGet library.
  2. Knowledge of C# Programming.
You can install my library via Nuget packages. The example code is developed in Microsoft Visual Studio 2017 Professional.

Download Now! Nuget Installation

Let's begin now.

1) Create a new Console Application project and name it "CSVImportExport". 

2) Install 'CSVLibraryAK' Nuget library into your project.

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);
        }
...

In the above code, I am simply importing my target CSV file into DataTable data structure and then export my data from DataTable data structure to my destination CSV file using my CSVLibraryAK Nuget library.

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.

2 comments:

  1. Hello Asma,

    While copying this code to my project I am getting the error at

    Strings.COL_HEADER_1

    it does not exist.

    ReplyDelete
    Replies
    1. Have you executed the provided solution? It's working fine.

      Delete