Header Ads

How to Import/Export CSV File Data in ASP.NET Core MVC Web App

I have previously created a simple C#.NET nuget library for importing/exporting CSV data from file using Datatable as a primary data structure. Now, I have migrated this library exclusively for C#.NET Core projects into a separate nuget library i.e. CSVLibraryAK.Core. You can also utilize this library into your any .NET Core web project as well.

Today, I shall be demonstrating integration of CSVLibraryAK.Core a C#.NET Core library with ASP.NET Core MVC web platform.

Prerequisites:

Following are some prerequisites before you proceed any further in this tutorial:
  1. Install CSVLibraryAK.Core NuGet library.
  2. Knowledge of ASP.NET Core MVC.
  3. Knowledge of HTML.
  4. Knowledge of JavaScript.
  5. Knowledge of Bootstrap.
  6. Knowledge of Jquery.
  7. Knowledge of C# Programming.
You can install the library via Nuget packages. The example code is being developed in Microsoft Visual Studio 2019 Professional.

Download Now!

Let's begin now.

1) Create a new MVC web project and name it "MVCImportExportCSV.Core".  
 
2) Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".

3) Install CSVLibraryAK.Core Nuget Package.

4) Create a new "Controllers\HomeController.cs" file and add this library into the using directives as shown below i.e.

...

namespace MVCImportExportCSV.Controllers
{
    ...

    using CSVLibraryAK.Core;

    ...
}

...

In the above code, I have simply included the CSVlibraryAK.Core library into my using directive.

5) Now, create a simple view with action method in the controller and view file to enable user to upload his/her CSV file to your web server and use this library's import method to import and display the CSV data into display view via Datatables data structure and then use this library's export method to export and save that uploaded CSV data from the display view grid to a CSV file on the web server with the help of following lines of code i.e.

...

// Impot CSV file.
model.Data = CSVLibraryAK.Import(importFilePath, model.HasHeader);

// Export CSV file.
CSVLibraryAK.Export(exportFilePath, model.Data);

...

In the above code, I am using the library's import and export methods to import and export the CSV file data on to my asp.net core mvc web server.

6) Now, execute the project and you will be able to see the following in action i.e.

 

Conclusion

In this article, you will learn to integrate CSVLibraryAK.Core a C#.NET Core library with ASP.NET Core MVC web platform. You will also learn about the strategy to upload and display CSV file data to the web server and you will learn about the strategy to download and save the CSV file data onto and from the web server.

4 comments: