- Exporting with column template
- Exporting with detail template
- Exporting with caption template
Contact Support
Exporting grid with templates in EJ2 JavaScript Grid control
23 Sep 202324 minutes to read
The grid offers the option to export the column, detail, and caption templates to an Excel document. The template contains images, hyperlinks, and customized text.
Exporting with column template
The Excel export functionality allows you to export Grid columns that include images, hyperlinks, and custom text to an Excel document.
In the following sample, the hyperlinks and images are exported to Excel using hyperlink and image properties in the excelQueryCellInfo event.
Excel Export supports base64 string to export the images.
ej.grids.Grid.Inject(ej.grids.Toolbar, ej.grids.ExcelExport);
var grid = new ej.grids.Grid({
dataSource: employeeData,
allowExcelExport: true,
toolbar: ['ExcelExport'],
columns: [
{
headerText: 'Employee Image', textAlign: 'Center',
template: '#imageTemplate', width: 150
},
{ field: 'EmployeeID', headerText: 'Employee ID', textAlign: 'Right', width: 125 },
{ field: 'FirstName', headerText: 'Name', width: 120 },
{ field: 'EmailID', headerText: 'Email ID', template: '#mailTemplate', width: 170 }
],
toolbarClick: toolbarClick,
excelQueryCellInfo: excelQueryCellInfo,
height: 273
});
grid.appendTo('#Grid');
function toolbarClick(args) {
if (args.item.id === 'Grid_excelexport') {
grid.excelExport();
}
}
function excelQueryCellInfo(args) {
if (args.column.headerText === 'Employee Image') {
args.image = {
base64: args.data.EmployeeImage,
height: 70,
width: 70,
};
}
if (args.column.headerText === 'Email ID') {
args.hyperLink = {
target: 'mailto:' + args.data.EmailID,
displayText: args.data.EmailID,
};
}
}
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Grid</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Grid Control">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-calendars/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-grids/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<script id="imageTemplate" type="text/x-template">
<div class="image">
<img src="${EmployeeID}.png" alt="${EmployeeID}" />
</div>
</script>
<script id="mailTemplate" type="text/x-template">
<div class="link">
<a href="mailto:${EmailID}">${EmailID}</a></div>
</div>
</script>
<div id="Grid"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Exporting with detail template
By default, the grid will export the parent grid with expanded detail rows alone. Change the exporting option by using the ExcelExportProperties.hierarchyExportMode
property. The available options are:
Mode | Behavior |
---|---|
Expanded | Exports the parent grid with expanded detail rows. |
All | Exports the parent grid with all the detail rows. |
None | Exports the parent grid alone. |
The detail rows in the exported Excel can be customized or formatted using the exportDetailTemplate event. In this event, the detail rows of the Excel document are formatted in accordance with their parent row details.
In the following sample, the detail row content is formatted by specifying the columnHeader and rows properties using its parentRow details. This allows for the creation of detail rows in the Excel document. Additionally, custom styles can be applied to specific cells using the style property.
When using rowSpan, it is essential to provide the cell’s index for proper functionality.
ej.grids.Grid.Inject(ej.grids.DetailRow, ej.grids.Toolbar, ej.grids.ExcelExport);
var grid= new ej.grids.Grid({
dataSource: employeeData,
detailTemplate: '#detailtemplate',
toolbar: ['ExcelExport'],
allowExcelExport: true,
toolbarClick: toolbarClick,
exportDetailTemplate: exportDetailTemplate,
columns: [
{ field: 'Category', headerText: 'Category', width: 120 },
{ field: 'ProductID', headerText: 'Product ID', width: 140 },
{ field: 'Status', headerText: 'Status', width: 200 }
],
height: 273
});
grid.appendTo('#Grid');
function toolbarClick(args) {
if (args['item'].id === 'Grid_excelexport') {
var exportProperties = {
hierarchyExportMode: "Expanded"
};
grid.excelExport(exportProperties);
}
}
function exportDetailTemplate(args) {
args.value = {
columnHeader: [
{
cells: [{
index: 0, colSpan: 2, value: 'Product Details',
style: { backColor: '#ADD8E6', excelHAlign: 'Center', bold: true }
}]
}
],
rows: [
{
cells: [
{
index: 0, rowSpan: 4, image: {
base64: args.parentRow.data['ProductImg'],
height: 80, width: 100
}
},
{
index: 1, value: "Offers: " + args.parentRow.data['Offers'],
style: { bold: true, fontColor: '#0a76ff' }
},
]
},
{
cells: [
{
index: 1, value: 'Available: ' + args.parentRow.data['Available']
}]
},
{
cells: [
{
index: 1, value: 'Contact: ',
hyperLink: {
target: 'mailto:' + args.parentRow.data['Contact'],
displayText: args.parentRow.data['Contact']
}
}]
},
{
cells: [
{
index: 1, value: 'Ratings: ' + args.parentRow.data['Ratings'],
style: { bold: true, fontColor: '#0a76ff' }
}
]
},
{
cells: [
{
index: 0, value: args.parentRow.data['productDesc'],
style: { excelHAlign: 'Center' }
},
{ index: 1, value: args.parentRow.data['ReturnPolicy'] }
]
},
{
cells: [
{
index: 0, value: args.parentRow.data['Cost'],
style: { excelHAlign: 'Center', bold: true }
},
{ index: 1, value: args.parentRow.data['Cancellation'] }
]
},
{
cells: [
{
index: 0, value: args.parentRow.data['Status'],
style: {
bold: true, fontColor: args.parentRow.data['Status'] === 'Available' ? '#00FF00' : '#FF0000',
excelHAlign: 'Center'
}
},
{
index: 1, value: args.parentRow.data['Delivery'],
style: { bold: true, fontColor: '#0a76ff' }
}
]
}
],
};
}
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Grid</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Grid Control">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-grids/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-calendars/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<script id="detailtemplate">
<table class="detailtable" width="100%">
<colgroup>
<col width="40%" />
<col width="60%" />
</colgroup>
<thead>
<tr>
<th colspan="2" style="font-weight: 500;text-align: center;background-color: #ADD8E6;">
Product Details
</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="4" style="text-align: center;">
<img class='photo' src="data:image/jpeg;base64,${ProductImg}" alt="${EmployeeID}" />
</td>
<td>
<span style="font-weight: 500;color: #0a76ff;">Offers: ${Offers} </span>
</td>
</tr>
<tr>
<td>
<span>Available: ${Available} </span>
</td>
</tr>
<tr>
<td>
<span class="link">
Contact: <a href="mailto:${Contact}">${Contact}</a>
</span>
</td>
</tr>
<tr>
<td>
<span style="font-weight: 500;color: #0a76ff;"> Ratings: ${Ratings}</span>
</td>
</tr>
<tr>
<td style="text-align: center;">
<span> ${productDesc}</span>
</td>
<td>
<span>${ReturnPolicy}</span>
</td>
</tr>
<tr>
<td style="text-align: center;">
<span style="font-weight: 500;" > ${Cost}</span>
</td>
<td>
<span>${Cancellation}</span>
</td>
</tr>
<tr>
<td style="text-align: center;">
<span class="${Status}" style="font-weight: 500;" > ${Status}</span>
</td>
<td>
<span style="font-weight: 500;color: #0a76ff;">${Delivery}</span>
</td>
</tr>
</tbody>
</table>
</script>
<div id="container">
<div id="Grid"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Exporting with caption template
The Excel export feature enables exporting of Grid with a caption template to an Excel document.
In the following sample, the customized caption text is exported to Excel using captionText property in the exportGroupCaption event.
ej.grids.Grid.Inject(ej.grids.Group, ej.grids.Toolbar, ej.grids.ExcelExport);
var grid = new ej.grids.Grid({
dataSource: employeeData,
allowGrouping: true,
groupSettings: { captionTemplate: '#captiontemplate', columns: ['EmployeeID'] },
allowExcelExport: true,
toolbar: ['ExcelExport'],
columns: [
{ field: 'EmployeeID', headerText: 'Employee ID', width: 120 },
{ field: 'FirstName', headerText: 'Name', width: 120 },
{ field: 'City', headerText: 'City' },
{ field: 'Title', headerText: 'Title', width: 170 }
],
toolbarClick: toolbarClick,
exportGroupCaption: exportGroupCaption,
height: 273
});
grid.appendTo('#Grid');
function toolbarClick(args) {
if (args.item.id === 'Grid_excelexport') {
grid.excelExport();
}
}
function exportGroupCaption(args) {
args.captionText = args.data.field + ' - ' + args.data.key;
}
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Grid</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript Grid Control">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-base/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-popups/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-lists/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-calendars/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/29.1.33/ej2-grids/styles/material.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.1.33/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<script id="captiontemplate" type="text/x-template">
${field} - ${key}
</script>
<div id="Grid"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>