Import/export of any file type is the basic need of any development
environment whether web development, desktop development or application
development. In web development, import/export is commonly referred to as uploading/downloading of a file. I have recently created my own small
import/export CSV file library CSVLibraryAK
which is compatible with any C#.NET project of the 32-bit machine. The code
of the library is open sourced, so, anyone can compile the code to
target bit machines or to make new changes.
Today, I shall be demonstrating integration of CSVLibraryAK C#.NET library with ASP.NET MVC5 platform.
Web Development Import/Export CSV file Workflow
In
web development importing/exporting of CSV files or any file, in general, are
visualized as Uploading/Downloading of the files from the end user's perspective, i.e., the end-user will first upload his/her target CSV file to
the web server. The web server, then, saves that CSV file in the CSV format and
then imports that CSV file to target data structure into main memory for
processing. Then, it exports the resultant data into a CSV format and then
displays the uploaded/imported CSV file data to the end-user on the
target web page. The end-user then downloads the CSV file which behind
the scenes on the web server is already exported to a .CSV file format
after processing of the uploaded CSV file. The resultant file is then
available for the end user to download.
Prerequisites
Following are some prerequisites before you proceed any further in this tutorial.
- Install CSVLibraryAK NuGet Package.
- Knowledge of ASP.NET MVC5.
- Knowledge of HTML.
- Knowledge of JavaScript.
- Knowledge of Bootstrap.
- Knowledge of jQuery.
- Knowledge of C# Programming.
You can download the complete source code for this tutorial. The sample code is
being developed in Microsoft Visual Studio 2017 Professional.
Let's begin now.
Step 1
Create a new MVC web project and name it "MVCImportExportCSV".
Step 2
Create a new "Controllers\HomeController.cs" file and add the following methods in it.
In the above code, I have
created POST "Index(...)" method which will
receive uploaded CSV file input from the
end-user, then save the CSV file on the web server, then import the stored CSV file using CSVLibraryAK library, then process the import file
if applicable, then save & export the resultant CSV file on the web
server using CSVLibraryAK library, and finally, display the result of
CSV file on the web page. I have also created the DownloadFile(...)
method which will return the resultant CSV file after processing back
to the end-user.
Step 3
Create a simple view to
upload a file. Then, display the resultant data and also provide the export
feature to download the resultant data.
Step 4
Now, execute the project and you
will be able to see the following in
action i.e.:
Conclusion
In this article, you learned to integrate CSVLibraryAK
C#.NET library with ASP.NET MVC5. You also learned to upload CSV file
to the web server. You also learned to utilize
CSVLibraryAK.Import(...) method. You also learned to download the CSV
file and you also learned to utilize CSVLibraryAK.Export() method.