Export Data to Excel with ASP.NET MVC

In creating a system there are times that we need to export data to excel, it could either be for reports or for data examination. This is what this article is all about.

Below are one of those method. To start, we need to create a fresh project of ASP.NET MVC. If this is your first time in creating ASP.NET MVC, you may refer to this article “How to start with ASP.NET MVC“.

Output Preview:

Export data to excel

Final Output

Let’s start:

I assume you have already created a new project with default MVC template.

First, open HomeController. Located inside Controller folder, remove all action result except for index().

It should look like this:

Export data to excel

Home Controller

Second, let’s create a view where we can display sample data into a table.To open your view navigate to Views->Home->index.cshtml or simply right click inside index action result and select Go to View.

Export data to excel

Navigate to View

Replace all code from your index.cshtml with the code below.

Export to excel

Employee Age Address BirthDate BirthPlace

 

 

Third, create a model class inside Models folder. This will be used in creating our sample data and declare all properties shown below.

Code:

Image preview:

Export data to excel

TableModel

Fourth, Create a sample data. Navigate back to your HomeController and create method sample_data. 

Code:

Fifth, fill html table that we created a while ago using Jquery. Navigate back to your controller view.Add the code below.

Code:

Lastly, create exportToExcel method inside your HomeController.

Code:

And we’re done… To run project simply press F5 key from your keyboard. Thank you for reading.. Happy coding!!

To review your work please refer from the full code below… 🙂

Full Code:

TableModel.cs

HomeController.cs

Index.cshtml

Final output:

Export data to excel

Final Output


Post you may like:

How to create custom 404 error handler in ASP.NET MVC

Custom 404 error handler - 404 Error refer to a not found resource/URL . Here’s how a default 404 Error ...
Read More

AngularJS CRUD Operation using ASP.NET MVC

I am a .NET Developer and I’m new in AngularJS. I started using AngularJS in about a week ago and ...
Read More
SignalR Chat App With ASP.NET

SignalR Chat Application with ASP.NET MVC

SignalR Chat App With ASP.NET - This article will show you an actual application using SignalR in ASP.NET MVC. The ...
Read More
AgularJS

Submit Forms using AgularJS with ASP.NET MVC

Ways on how to submit forms using AngularJS: I’m new to AngularJS and I was trying to create a CRUD application ...
Read More
Self Signed Certificate

How: Creating Self Signed Certificate

As defined in sslshopper.com. A self-signed certificate is a certificate that is signed by the person creating it rather than a trusted ...
Read More
AngularJS

AngularJS with Asp.Net MVC

What is Angular? AngularJS is a JavaScript framework that extends HTML attribute to help develop a dynamic Web application. AngularJS ...
Read More

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 ...
Read More

Export Data to Excel with ASP.NET MVC

In creating a system there are times that we need to export data to excel, it could either be for ...
Read More

Easy Steps to Implement Partial View in Asp.Net MVC

One of the best ways to implement a dynamic table without loading the whole page is by using Jquery Ajax ...
Read More

Creating ASP.NET Web Service

ASP.NET Web Service - Web service is a software that is used to exchange XML-based information that uses the internet ...
Read More

Leave a Reply

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