Example of Advanced 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 with advanced options in same document to desired format.
@Html.UiGrid(new UiGridOptions
{
Id = "AdvancedExporting",
OnToolbarClick = "toolbarClick",
Data = ViewData["dataSource"],
AllowGrouping = true,
PageCount = 5,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel", "ExcelExport", "PdfExport" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 200, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "QuantityPerUnit", HeaderText = "Quantity Per Unit", Width = 145 },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 140, AllowGrouping = false, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 140, Format = "C2", EditType = UiEditType.NumericEdit }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "AdvancedExporting",
OnToolbarClick = "toolbarClick",
Data = ViewData["dataSource"],
AllowGrouping = true,
PageCount = 5,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel", "ExcelExport", "PdfExport" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductID", HeaderText = "Product ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 200, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "QuantityPerUnit", HeaderText = "Quantity Per Unit", Width = 145 },
new UiGridColumn { Field = "UnitsInStock", HeaderText = "Units In Stock", Type = UiColumnType.Number, Width = 140, AllowGrouping = false, TextAlign = UiTextAlign.Right },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 140, Format = "C2", EditType = UiEditType.NumericEdit }
}
})
<script>
// Syncfusion's toolbarClick: the PDF and Excel items export with the invoice header and footer below (as lines of text here).
function toolbarClick(args) {
var grid = window.Lumen.get('AdvancedExporting');
if (args.item.id === 'pdfexport') { args.cancel = true; grid.pdfExport(getPdfExportProperties()); }
if (args.item.id === 'excelexport') { args.cancel = true; grid.excelExport(getExcelExportProperties()); }
}
function getExcelExportProperties() {
return {
header: ['INVOICE', 'Adventure Traders', '2501 Aerial Center Parkway', 'Tel +1 888.936.8638 Fax +1 919.573.0306', 'INVOICE NUMBER: 2034', 'DATE: ' + new Date().toLocaleDateString(), 'CUSTOMER ID: 564', 'TERMS: Net 30 days'],
footer: ['Thank you for your business!', '!Visit Again!'],
fileName: 'exceldocument.xlsx'
};
}
function getPdfExportProperties() {
return {
header: ['INVOICE', 'Adventure Traders', '2501 Aerial Center Parkway', 'Tel +1 888.936.8638 Fax +1 919.573.0306', 'INVOICE NUMBER: 223344', 'Date: ' + new Date().toLocaleDateString(), 'CUSTOMER ID: 223', 'TERMS: Net 30 days'],
footer: ['Thank you for your business !', '! Visit Again !'],
fileName: 'pdfdocument.pdf'
};
}
</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/AdvancedExporting.cshtml; it draws what ViewData holds.
public ActionResult AdvancedExporting()
{
var product = Product.GetAllRecords().ToList();
ViewData["datasource"] = product;
return View();
}
}
}
@* 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, excelexport and pdfexport items are defined in the toolbar. For these toolbar items, we have defined actions in toolbarClick event to export Grid with header and footer using the ExcelExport,PdfExportmethods.