In this article I will explain with an example, how to convert (export) HTML to Word Document in ASP.Net MVC Razor.
First the Grid (Html Table) will be populated from database using Entity Framework.
When the Export Button is clicked, the HTML string will be sent to the Controller and will be downloaded as Word file in ASP.Net MVC Razor.
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Configuring and connecting Entity Framework to database
Now I will explain the steps to configure and add Entity Framework and also how to connect it with the database.
You will need to add Entity Data Model to your project by right clicking the Solution Explorer and then click on Add and then New Item option of the Context Menu.
Convert (Export) HTML to Word in ASP.Net MVC
From the Add New Item window, select ADO.NET Entity Data Model and set its Name as NorthwindModel and then click Add.
Convert (Export) HTML to Word in ASP.Net MVC
Then the Entity Data Model Wizard will open up where you need to select EF Designer database option.
Convert (Export) HTML to Word in ASP.Net MVC
Now the wizard will ask you to connect and configure the Connection String to the database.
Convert (Export) HTML to Word in ASP.Net MVC
You will need to select the
1.     SQL Server Instance
2.     Database
And then click Test Connection to make sure all settings are correct.
Convert (Export) HTML to Word in ASP.Net MVC
Once the Connection String is generated, click Next button to move to the next step.
Convert (Export) HTML to Word in ASP.Net MVC
Next you will need to choose the Entity Framework version to be used for connection.
Convert (Export) HTML to Word in ASP.Net MVC
Now you will need to choose the Tables you need to connect and work with Entity Framework. Here Customers Table is selected.
Convert (Export) HTML to Word in ASP.Net MVC
The above was the last step and you should now have the Entity Data Model ready with the Customers Table of the Northwind Database.
Convert (Export) HTML to Word in ASP.Net MVC
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, the Top 10 Customer records are fetched 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.
Note: By default posting of HTML content via input fields which by default is disabled. For more details on enabling it, please refer What is ValidateInput(false) attribute, its Uses and Examples in ASP.Net MVC.
The HTML is written to the Output Response and it is exported and download as Word file.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        NorthwindEntities entities = new NorthwindEntities();
        return View(from customer in entities.Customers.Take(10)
                    select customer);
    public EmptyResult Export(string GridHtml)
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=Grid.doc");
        Response.Charset = "";
        Response.ContentType = "application/";
        return new EmptyResult();
Now you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.
The Name of the View is set to Index, the Template option is set to Empty, the Model class is set to Customer Entity (the one we have generated using Entity Framework) and finally the Data context class is set to NorthwindEntities.
Convert (Export) HTML to Word in ASP.Net MVC
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
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.
There is an HTML Hidden Field element which is used to send the Grid HTML content to the Controller’s Action method.
Finally there’s an HTML Submit button enclosed inside a Form with the Action method specified as Export.
When this Button will be 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.Customer>
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width"/>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 9pt;
    <div id="Grid">
        <table cellpadding="5" cellspacing="0" style="border: 1pxsolid#ccc;font-size: 9pt;font-family:Arial">
                <th style="background-color: #B8DBFD;border: 1pxsolid#ccc">CustomerID</th>
                <th style="background-color: #B8DBFD;border: 1pxsolid#ccc">ContactName</th>
                <th style="background-color: #B8DBFD;border: 1pxsolid#ccc">City</th>
                <th style="background-color: #B8DBFD;border: 1pxsolid#ccc">Country</th>
            @foreach (Customer customer in Model)
                    <td style="width:120px;border: 1pxsolid#ccc">@customer.CustomerID</td>
                    <td style="width:120px;border: 1pxsolid#ccc">@customer.ContactName</td>
                    <td style="width:120px;border: 1pxsolid#ccc">@customer.City</td>
                    <td style="width:120px;border: 1pxsolid#ccc">@customer.Country</td>
    @using (Html.BeginForm("Export", "Home", FormMethod.Post))
        <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)
Convert (Export) HTML to Word in ASP.Net MVC
Exported Word File
Convert (Export) HTML to Word in ASP.Net MVC