How to Export data table to excel in ASP.Net MVC

In this tutorial i am going to explain about how to Export data table to excel in ASP.Net MVC. In my previous tutorial i have explained about

Implementation :

Now we will see the implementation of how to Export data table to excel in ASP.Net MVC. Lets create a demo application to see Export data table to excel in ASP.Net MVC. Open visual studio and Create a new ASP.Net MVC Project. Right click on Models folder and choose Add=>Class option and name the class as Employee.cs. Refer the below screenshot.

Step 1 of How to Export data table to excel in ASP.Net MVC

Now add the required attributes in Employee.cs class. Below is the code.

Once employee model is added then right click on Controllers folder and choose Add=>Controller option and name it as ExportController.cs Refer the below screenshot.

Step 2 of How to Export data table to excel in ASP.Net MVC

ExportController.cs Code:

Now add below two action methods in the export controller.

  1. Index – This action method is used for displaying some sample records.
  2. ExportToExcel – This action method is used for exporting the records in the index view to excel.

Below is the complete code of ExportController.cs

Index.cshtml Code:

Right click on index action method and choose Add View option and add the below code.

Output:

Now if you run the program you can see the output of how to Export data table to excel in ASP.Net MVC

Output of How to Export data table to excel in ASP.Net MVC

Source Code:

Source code of how to Export data table to excel in ASP.Net MVC

Conclusion:

Hope you learned how to Export data table to excel in ASP.Net MVC. Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Leave a Reply

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