Importing Excel with ASP.NET MVC

Importing excel, In my previous article, I demonstrated how to perform simple way of downloading data into excels. Now, this time I’m going to create a simple way of uploading data from excels.

Let’s Start
Output preview
Importing excel

Import Excel

First, create a new MVC project. You can name it whatever you want, In my case, I name it ImportExcel.

If this is your first time creating  MVC project please refer to this article How to start with ASP.Net MVC.

Second, add reference Excel Data reader using your NuGet Package Manager. To add right click on your project name, from your solution explorer. Like shown below.

Importing excel

Manage NuGet Packages

Then select Manage NuGet Packages and search for ExcelDataReader. Install ExcelDataReader and ExcelDataReader.Dataset.  When a green check is shown like the image below it means that the package is  already installed.

Importing excel

Add Excel Data Reader

Third, create a view for your application. In this topic, the view will look like the image shown below.

Importing excel

Navigate to your default view. If you are using default template of MVC your default controller name is HomeController that makes Home -> index.cshtml as your default view. Navigate to this file and replace all code inside this file with the code below.

Fourth, create post action result in your HomeController, this will be used to read excel file.

Note : We have created two index action result in our HomeController . First index handle HttpGet request and the other one handle HttpPost request.

HttpGet index:
  • This loads default view.

HttpPost index:
  • This received post request from the view.Which is the excel data.

Lastly, run your project by pressing F5 from you keyboard and try exporting sample excel file. Excel file should be in table format.

Sample Excel file:

Importing excel

Excel Format

Click on choose button in importing excel and select your excel file.

Importing excel

Import Excel File

Then click upload. Your final view should look like this.

Importing excel

And we are all set. Thank you for reading… Happy Coding!!

Code Summary:

Home Controller

View > Index.cshtml


Leave a Reply

Your email address will not be published. Required fields are marked *