In this article I will explain with an example, how to export data from database to HTML file in ASP.Net Core Razor Pages.
The data from the database is fetched using Entity Framework and dynamic HTML string is generated using StringBuilder class.
Finally, the generated HTML string will be exported to HTML (.htm) file in ASP.Net Core Razor Pages.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Namespaces
You need to import the following namespace.
using System.Text;
 
 
Model
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 Razor Pages: 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_Data_Database_HTML_File_Razor_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
           
        }
       
        public DbSet<Customer> Customers { get; set; }
    }
}
 
 
Razor PageModel (Code-Behind)
The PageModel consists of following two Handler methods.
Handler method for handling GET operation
Inside this Handler method, the Top 10 records are fetched from the Customers table using Entity Framework and assigned to the public property Customers and returned to the Razor Page.
 
Handler method for handling the HTML File Export and Download operation
This Handler method is executed when the Export Button is clicked.
Note: The following Handler method performs File Download operation. Hence the return type is set to FileResult. For more details on FileResult, please refer my article FileResult return type in ASP.Net Core Razor Pages.
 
Inside this Handler Method, the Top 10 Customer records are fetched from the Customers Table using Entity Framework and then using the StringBuilder class, a string consisting of an HTML Table is generated.
Finally, the HTML string is converted to Byte Array and exported and downloaded as HTML (.htm) file using the File function.
public class IndexModel : PageModel
{
    private DBCtx Context { get; }
 
    public IndexModel(DBCtx _context)
    {
        this.Context = _context;
    }
 
    public List<Customer> Customers { get; set; }
 
    public void OnGet()
    {
        this.Customers = this.Context.Customers.Take(10).ToList();
    }
 
    public FileResult OnPostExport()
    {
        List<object> customers = (from customer in this.Context.Customers.Take(10)
                                  select new[] {
                                      customer.CustomerID,
                                      customer.ContactName,
                                      customer.City,
                                      customer.Country
                                }).ToList<object>();
 
        //Building an HTML string.
        StringBuilder sb = new StringBuilder();
 
        //Table start.
        sb.Append("<table border='1' cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;'>");
 
        //Building the Header row.
        sb.Append("<tr>");
        sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>CustomerID</th>");
        sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>ContactName</th>");
        sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>City</th>");
        sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>Country</th>");
        sb.Append("</tr>");
 
        //Building the Data rows.
        for (int i = 0; i < customers.Count; i++)
        {
            string[] customer = (string[])customers[i];
            sb.Append("<tr>");
            for (int j = 0; j < customer.Length; j++)
            {
                //Append data.
                sb.Append("<td style='border: 1px solid #ccc'>");
                sb.Append(customer[j]);
                sb.Append("</td>");
            }
            sb.Append("</tr>");
        }
 
        //Table end.
        sb.Append("</table>");
 
        return File(Encoding.ASCII.GetBytes(sb.ToString()), "text/html", "Grid.htm");
    }
}
 
 
Razor Page (HTML)
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 database data to HTML (.htm) file
Below the HTML Table, there is an HTML Form within which a Submit Button has been placed.
The Export Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostExport but here it will be specified as Export when calling from the Razor HTML Page.
 
When the Submit button is clicked, the Form is submitted.
@page
@model Export_Data_Database_HTML_File_Razor_Core.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div id="Grid">
        <table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;">
            <tr>
                <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>
            </tr>
            @foreach (var customer in Model.Customers)
            {
                <tr>
                    <td style="border: 1px solid #ccc">@customer.CustomerID</td>
                    <td style="border: 1px solid #ccc">@customer.ContactName</td>
                    <td style="border: 1px solid #ccc">@customer.City</td>
                    <td style="border: 1px solid #ccc">@customer.Country</td>
                </tr>
            }
        </table>
    </div>
    <br />
    <form method="post">
        <input type="submit" id="btnSubmit" value="Export" asp-page-handler="Export" />
    </form>
</body>
</html>
 
 
Screenshots
GridView
ASP.Net Core Razor Pages: Export data from database to HTML File
 
GridView exported as HTML (.htm) file
ASP.Net Core Razor Pages: Export data from database to HTML File
 
 
Downloads