In this article I will explain with an example, how to export WebGrid to Excel file in ASP.Net Core MVC.
The WebGrid will be populated from database using Entity Framework and then the WebGrid will be sent as HTML string to the Controller which will be exported to Excel file in ASP.Net Core MVC.
MVC6 Grid for ASP.Net Core
This article makes use of MVC6 Grid library for implementing WebGrid, as it is not available by default in .Net Core.
For more details on how to use MVC6 Grid, please refer the article Using MVC6 Grid in .Net Core.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
You need to import the following namespace.
using System.Text;
The Model class consists of the following four properties.
public class Customer
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string City { 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_WebGrid_Excel_MVC_Core
    public class DBCtx : DbContext
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        public DbSet<Customer> Customers { get; set; }
The Controller consists of following two Action methods.
Action method for handling GET operation
Inside this Action method, the Top 10 records are fetched from the Customers table using Entity Framework and returned to the View.
Action method for handling the Excel file Export and Download operation
This Action method is executed when the Export Button is clicked.
Note: The following Action method performs File Download operation. Hence the return type is set to FileResult.
The HTML string sent from the View is extracted from the GridHtml parameter.
Then, the HTML string is read and converted to a Byte Array using the GetBytes method of Encoding class.
Finally, the Byte Array is exported and downloaded as Excel file using the File function.
public class HomeController : Controller
    private DBCtx Context { get; }
    public HomeController(DBCtx _context)
        this.Context = _context;
    public IActionResult Index()
        return View(this.Context.Customers.Take(10).ToList());
    public FileResult Export(string GridHtml)
        return File(Encoding.ASCII.GetBytes(GridHtml), "application/", "Grid.xls");
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.
The ASP.Net TagHelpers, NonFactors.Mvc.Grid namespace and mvc-grid.css file is inherited inside the View.
Displaying the records
For displaying the records, MVC6 Grid is used.
The IEnumerable collection of Customer Model class object is passed to the Grid function of the MVC6 Grid HTML Helper class.
Exporting WebGrid to Excel file
Below the MVC6 WebGrid, there is an HTML Form created with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is Export.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of a Submit Button and an HTML Hidden Field element which will be used to send the MVC6 WebGrid content to the Controller’s Action method.
When the Export Button is clicked, first the HTML of the MVC6 WebGrid is extracted and set into the Hidden Field element and finally, the Form is submitted.
@model IEnumerable<Export_WebGrid_Excel_MVC_Core.Models.Customer>
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using NonFactors.Mvc.Grid;
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <link href="~/css/mvc-grid/mvc-grid.css" rel="stylesheet" />
    <hr />
    <div id="Grid">
        @(Html.Grid(Model).Build(columns =>
                columns.Add(model => model.CustomerID).Titled("CustomerID");
                columns.Add(model => model.ContactName).Titled("ContactName");
                columns.Add(model => model.City).Titled("City");
                columns.Add(model => model.Country).Titled("Country");
    <br /><br />
    <form method="post" asp-action="Export" asp-controller="Home">
        <input type="hidden" name="GridHtml" />
        <input type="submit" id="btnSubmit" value="Export" />
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(function () {
            //Add CSS to the Table for formatting.
            $("#Grid table").attr("cellpadding", "5");
            $("#Grid table").css({ "border-collapse": "collapse", "border": "1px solid #ccc" });
            $("#Grid th").css({ "background-color": "#B8DBFD", "border": "1px solid #ccc" });
            $("#Grid td").css({ "width": "150px", "border": "1px solid #ccc" });
            //Assign Click event to Button.
            $("#btnSubmit").click(function () {
ASP.Net Core MVC: Export WebGrid to Excel file
Exported Excel file
ASP.Net Core MVC: Export WebGrid to Excel file