We often come across requirement where we need Excel import functionality to upload bulk data to database using Excel file. Bulk Import through excel file helps in bulk data insertion which facilitates the application.Watch Video 1Watch Video 2
In this article we will cover following points :

Downloading sample excel file from the application.

Uploading the downloaded excel file with data.

Storing the excel file at physical path.

Reading the Excel file.

Validating the Excel file.

Processing the excel data and respective updates to database.

UI:

We have created a simple UI. The UI is as below :

The user can download the excel on clicking the excel image. The use can then upload the same downloaded excel file by choosing and clicking the submit button. The excel file is validated and if the excel file is validated successfully, the data is inserted to database.

Downloading sample excel file from application :

Usually in Bulk upload functionalities the Excel file is provided or downloaded from application because the excel file should be in particular format. The user can download the file, add data to it and can upload the same excel file with data.

In our example we have kept the excel file in a folder.

When user clicks the the excel image, the above excel file is converted to bytes and the file is downloaded to user.

We have created a static class named ExtensionMethods which has a static method named GetFileData. This method accepts the filename and filepath and returns the bytes of the excel file. Finally the File content type is returned which downloads the excel file to user.Uploading the downloaded excel file with data.Uploading the downloaded excel file with data:
Once the the excel is downloaded, the excel is filled with data and uploaded.View:

The File property in the model is of type HttpPostedFileBase class. This class exposes SaveAs method which accepts path at which the file should be save.Reading the Excel file:
The excel file which is saved at physical path using the above code, is read and the data is filled in the DataTable.

The above code reads the data from first sheet. The data is then filled in DataTable.Validating the excel file:
The data in the DataTable is then validated for blank and invalid values. The code for validating the Excel data is present above under service class.Processing the excel data and respective updates to database:
The uploaded excel data is saved to database, if the excel validation is true. The code for saving the data is present above under service class.Database Used :

Excel File with Data:

Database Data:

When the excel is uploaded with empty cell or completely blank, the validation fails and data is not saved and Processing status is set to failure with failure reason.

In the above excel we kept LastName blank and uploaded the excel file.

The processing status is failure and Failure reason is logged in database and user details are not saved.

So, this is how we can implement buld upload excel imlementation in MVC3 Razor.