In this article I will explain with an example, how to export Grid (GridView) to Excel file using jQuery in ASP.Net Core MVC.
The Grid (GridView) will be exported (converted) to Excel file using the jQuery table2excel plugin in ASP.Net Core MVC.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
ASP.Net Core MVC: Export Grid (GridView) to Excel using jQuery
 
I have already inserted few records in the table.
ASP.Net Core MVC: Export Grid (GridView) to Excel using jQuery
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Model
The Model class consists of the following three properties.
public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
 
 
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core and Entity Framework, please refer my article ASP.Net Core: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core.
 
using Microsoft.EntityFrameworkCore;
 
namespace Export_GridView_Excel_jQuery_MVC_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
           
        }
       
        public DbSet<Customer> Customers { get; set; }
    }
}
 
 
Controller
The Controller consists of following Action method.
Action method for handling GET operation
Inside this Action method, all the records are fetched from the Customers table using Entity Framework and returned to the View.
public class HomeController : Controller
{
    private DBCtx Context { get; }
 
    public HomeController(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public IActionResult Index()
    {
        return View(this.Context.Customers.ToList());
    }
}
 
 
View
Inside the View, in the very first line the Customer Model is declared as IEnumerable which specifies that it will be available as a Collection.
Displaying the Records
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
 
Exporting Grid (GridView) to Excel file
Below the HTML Table, there is an HTML Button element.
Inside the jQuery document ready event handler, the Export Button has been assigned a jQuery Click event handler.
When the Export Button is clicked, the jQuery table2excel plugin is applied to the HTML Table and it is converted (exported) to Excel file.
The jQuery table2excel plugin accepts filename parameter which sets the name of the Excel file.
@using Export_GridView_Excel_jQuery_MVC_Core.Models;
@model IEnumerable<Customer>
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table id="tblCustomers">
        <tr>
            <th>Customer Id</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        @foreach (Customer customer in Model)
        {
            <tr>
                <td>@customer.CustomerId</td>
                <td>@customer.Name</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
    <br/>
    <input type="submit" id="btnExport" value="Export" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="~/Scripts/table2excel.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnExport").click(function () {
                $("#tblCustomers").table2excel({
                    filename: "Table.xls"
                });
            });
        });
    </script>
</body>
</html>
 
 
Screenshots
The Grid (GridView)
ASP.Net Core MVC: Export Grid (GridView) to Excel using jQuery
 
Excel file being downloaded when Export Button is clicked
ASP.Net Core MVC: Export Grid (GridView) to Excel using jQuery
 
The generated Excel file
ASP.Net Core MVC: Export Grid (GridView) to Excel using jQuery
 
 
Browser Compatibility

The above code has been tested in the following browsers.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Downloads