In this article I will explain with an example, how to export Grid (Html Table) data from database to Word file in ASP.Net Core MVC.
The Grid (Html Table) will be populated from database using Entity Framework and then the records from the database will be exported and downloaded as Word file in ASP.Net Core MVC.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
You need to import the following namespaces.
using System.Text;
using Microsoft.AspNetCore.Http;
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_Word_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 Word File Export and Download operation
This Action method is executed when the Export Submit button is clicked.
Note: The following Action method performs File Download using HTTP Response and nothing is returned from the View. Hence the return type is set to EmptyResult.
The HTML of the Grid sent from the View is extracted from the GridHtml parameter and converted to Byte Array using the GetBytes method of Encoding class.
Finally, using the IHttpContextAccessor interface, the HttpContext property is accessed and the Byte Array is written to the Response and it is exported and download as Word file.
Note: For more details on IHttpContextAccessor interface, please refer my article Using HttpContext in ASP.Net Core.
public class HomeController : Controller
    private DBCtx Context { get; }
    private IHttpContextAccessor Accessor;
    public HomeController(DBCtx _context, IHttpContextAccessor _accessor)
        this.Context = _context;
        this.Accessor = _accessor;
    public IActionResult Index()
        return View(this.Context.Customers.Take(10).ToList());
    public EmptyResult Export(string GridHtml)
        byte[] bytes = Encoding.UTF8.GetBytes(GridHtml);
        HttpContext context = this.Accessor.HttpContext;
        context.Response.Headers.Add("content-disposition", "attachment;filename=Grid.doc");
        context.Response.ContentType = "application/";
        context.Response.Body.Write(bytes, 0, bytes.Length);
        return new EmptyResult();
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 HTML Table to Word Document
Below the HTML Table 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 Hidden Field and a Submit Button.
The Hidden Field element is used to send the Grid HTML content to the Controller’s Action method.
When the Export Button is clicked, first the HTML of the Grid (Html Table) is extracted and set into the Hidden Field element and finally, the Form is submitted.
@model IEnumerable<Export_Word_MVC_Core.Models.Customer>
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <div id="Grid">
        <table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;font-size: 9pt;font-family:Arial">
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">CustomerID</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">ContactName</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">City</th>
                <th style="background-color: #B8DBFD;border: 1px solid #ccc">Country</th>
            @foreach (var customer in Model)
                    <td style="width:120px;border: 1px solid #ccc">@customer.CustomerID</td>
                    <td style="width:120px;border: 1px solid #ccc">@customer.ContactName</td>
                    <td style="width:120px;border: 1px solid #ccc">@customer.City</td>
                    <td style="width:120px;border: 1px solid #ccc">@customer.Country</td>
    <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 () {
            $("#btnSubmit").click(function () {
Grid (Html Table)
ASP.Net Core MVC: Export Grid (Html Table) data from database to Word
Exported Word File
ASP.Net Core MVC: Export Grid (Html Table) data from database to Word