Example of Default Exporting in ASP.NET Core Data Grid Control
This sample demonstrates the client-side exporting of the Grid, which allows you to export its data to the Excel, PDF and CSV formats. Use the toolbar buttons to export Grid data to desired format.
Properties
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
Data = ViewData["dataSource"],
Height = "350px",
AllowSorting = true,
AllowGrouping = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
Toolbar = new List<string> { "ExcelExport", "PdfExport", "CsvExport" },
OnQueryCellInfo = "queryCellInfo",
OnDataLoaded = "gridDataBound",
Columns = new List<UiGridColumn>
{
new UiGridColumn { HeaderText = "Employee Image", Template = "#imageCell", Width = 150, TextAlign = UiTextAlign.Center },
new UiGridColumn { Field = "FirstName", HeaderText = "Name", Width = 130 },
new UiGridColumn { Field = "Title", HeaderText = "Designation", Width = 180 },
new UiGridColumn { Field = "EmailID", HeaderText = "Email ID", Template = "#template2", Width = 180 },
new UiGridColumn { Field = "HireDate", HeaderText = "Hire Date", Type = UiColumnType.Date, Width = 120, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Address", Width = 120, AllowGrouping = false }
}
})
<style>
.image { display: inline-flex; align-items: center; justify-content: center; width: 55px; height: 55px; border-radius: 50px; color: #fff; font-weight: 600;
box-shadow: inset 0 0 1px #e0e0e0, inset 0 0 14px rgba(0, 0, 0, 0.2); }
</style>
<template id="imageCell"><span></span></template>
<template id="template2"><div class="link"><a href="mailto:${EmailID}">${EmailID}</a></div></template>
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
Data = ViewData["dataSource"],
Height = "350px",
AllowSorting = true,
AllowGrouping = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
Toolbar = new List<string> { "ExcelExport", "PdfExport", "CsvExport" },
OnQueryCellInfo = "queryCellInfo",
OnDataLoaded = "gridDataBound",
Columns = new List<UiGridColumn>
{
new UiGridColumn { HeaderText = "Employee Image", Template = "#imageCell", Width = 150, TextAlign = UiTextAlign.Center },
new UiGridColumn { Field = "FirstName", HeaderText = "Name", Width = 130 },
new UiGridColumn { Field = "Title", HeaderText = "Designation", Width = 180 },
new UiGridColumn { Field = "EmailID", HeaderText = "Email ID", Template = "#template2", Width = 180 },
new UiGridColumn { Field = "HireDate", HeaderText = "Hire Date", Type = UiColumnType.Date, Width = 120, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Address", Width = 120, AllowGrouping = false }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="Export">Export Template Column</label><input type="checkbox" id="Export" checked /></div>
</div>
<script>
// Syncfusion's Employee Image cell is a picture; here it is the initials in a circle (the photos are not part of this demo).
function queryCellInfo(args) {
if (args.column === null && args.headerText === 'Employee Image') {
var circle = document.createElement('div');
circle.className = 'image';
circle.style.background = 'hsl(' + (args.data.EmployeeID * 47 % 360) + ', 45%, 45%)';
circle.setAttribute('role', 'img');
circle.setAttribute('aria-label', args.data.FirstName);
circle.textContent = String(args.data.FirstName || '').charAt(0).toUpperCase() + String(args.data.LastName || '').charAt(0).toUpperCase();
args.content = circle;
}
}
// Syncfusion: the CSV item is hidden while the template columns (picture, e-mail link) are exported.
var templateColumns = ['Employee Image', 'Email ID'];
function csvTool() { return document.querySelector('#Grid .ui-grid-tool[data-id="csvexport"]') || document.querySelector('#Grid .ui-grid-tools .ui-grid-tool:last-child'); }
function gridDataBound() {
var tool = csvTool();
if (tool) { tool.style.display = document.getElementById('Export').checked ? 'none' : ''; }
}
document.getElementById('Export').addEventListener('change', function (e) {
var grid = window.Lumen.get('Grid');
if (e.target.checked) { grid.showColumns(templateColumns); } else { grid.hideColumns(templateColumns); }
var tool = csvTool();
if (tool) { tool.style.display = e.target.checked ? 'none' : ''; }
});
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/DefaultExporting.cshtml; it draws what ViewData holds.
public ActionResult DefaultExporting()
{
var employee = ExportEmployee.GetAllRecords();
ViewData["dataSource"] = employee;
return View();
}
public class EmployeeView
{
public EmployeeView()
{
}
public EmployeeView(int EmployeeID, string FirstName, string LastName, string Title, DateTime BirthDate, DateTime HireDate, int ReportsTo, string Address, string PostalCode, string Phone, string City, string Country)
{
this.EmployeeID = EmployeeID;
this.FirstName = FirstName;
this.LastName = LastName;
this.Title = Title;
this.BirthDate = BirthDate;
this.HireDate = HireDate;
this.ReportsTo = ReportsTo;
this.Address = Address;
this.PostalCode = PostalCode;
this.Phone = Phone;
this.City = City;
this.Country = Country;
}
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public DateTime BirthDate { get; set; }
public DateTime HireDate { get; set; }
public int ReportsTo { get; set; }
public string Address { get; set; }
public string PostalCode { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Country { get; set; }
public static List<EmployeeView> GetAllRecords()
{
List<EmployeeView> Emp = new List<EmployeeView>();
Emp.Add(new EmployeeView(1, "Nancy", "Davolio", "Sales Representative", new DateTime(1948, 12, 08), new DateTime(1992, 05, 01), 2, "507 - 20th Ave. E.Apt. 2A ", " 98122", "(206) 555-9857 ", "Seattle ", "USA"));
Emp.Add(new EmployeeView(2, "Andrew", "Fuller", "Vice President, Sales", new DateTime(1952, 02, 19), new DateTime(1992, 08, 14), 4, "908 W. Capital Way", "98401 ", "(206) 555-9482 ", "Kirkland ", "USA"));
Emp.Add(new EmployeeView(3, "Janet", "Leverling", "Sales Representative", new DateTime(1963, 08, 30), new DateTime(1992, 04, 01), 3, " 4110 Old Redmond Rd.", "98052 ", "(206) 555-8122", "Redmond ", "USA "));
Emp.Add(new EmployeeView(4, "Margaret", "Peacock", "Sales Representative", new DateTime(1937, 09, 19), new DateTime(1993, 05, 03), 6, "14 Garrett Hill ", "SW1 8JR ", "(71) 555-4848 ", "London ", "UK "));
Emp.Add(new EmployeeView(5, "Steven", "Buchanan", "Sales Manager", new DateTime(1955, 03, 04), new DateTime(1993, 10, 17), 8, "Coventry HouseMiner Rd. ", "EC2 7JR ", " (206) 555-8122", "Tacoma ", " USA"));
Emp.Add(new EmployeeView(6, "Michael", "Suyama", "Sales Representative", new DateTime(1963, 07, 02), new DateTime(1993, 10, 17), 2, " 7 Houndstooth Rd.", " WG2 7LT", "(71) 555-4444 ", "London ", "UK "));
Emp.Add(new EmployeeView(7, "Robert", "King", "Sales Representative", new DateTime(1960, 05, 29), new DateTime(1994, 01, 02), 7, "Edgeham HollowWinchester Way ", "RG1 9SP ", "(71) 555-5598 ", "London ", " UK"));
Emp.Add(new EmployeeView(8, "Laura", "Callahan", "Inside Sales Coordinator", new DateTime(1958, 01, 09), new DateTime(1994, 03, 05), 9, "722 Moss Bay Blvd. ", "98033 ", " (206) 555-3412", "Seattle ", "USA "));
Emp.Add(new EmployeeView(9, "Anne", "Dodsworth", "Sales Representative", new DateTime(1966, 01, 27), new DateTime(1994, 11, 15), 5, "4726 - 11th Ave. N.E. ", "98105 ", "(71) 555-5598 ", " London", "UK "));
return Emp;
}
}
public class ExportEmployee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string EmailID { get; set; }
public DateTime HireDate { get; set; }
public string Address { get; set; }
public static List<ExportEmployee> GetAllRecords()
{
// the employees of the Grouping sample, with a mail address each
return EmployeeView.GetAllRecords().Select(e => new ExportEmployee
{
EmployeeID = e.EmployeeID,
FirstName = e.FirstName,
LastName = e.LastName,
Title = e.Title,
EmailID = e.FirstName.ToLowerInvariant() + "@adventure-works.com",
HireDate = e.HireDate,
Address = e.Address
}).ToList();
}
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
Grid supports client-side exporting which allows you to export its data to the Excel, PDF and CSV formats.
In this demo, while exporting, we have included images and hyperlinks from the template columns i.e Image and URL using excelQueryCellInfo and pdfQueryCellInfo events. The ExcelExport, PdfExport, and CsvExport items are defined in the toolbar, for which we have defined actions in toolbarClick event to export the Grid data using the ExcelExport,PdfExport and CsvExport methods.
Note: Since CSV format is supported only plain text, images and hyperlinks will not be exported on this.