Your search did not match any results.

Data Grid - Multiple Grids Export

The exportDataGrid() method allows users to export multiple grids to one Excel document. Grids are exported consequently in a chain of Promises.

In this demo, this functionality is used to export two DataGrids into separate worksheets in the same workbook. Starting position of each exported grid is set using the topLeftCell property.

Use the customizeCell method to customize the exported worksheets.

Backend API
@using DevExtreme.NETCore.Demos.Models; @model IEnumerable<DataGridProduct> @section ExternalDependencies { <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.10.1/polyfill.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/devextreme-exceljs-fork@4.4.4/dist/dx-exceljs-fork.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script> } @{ string priceCaption = "Price"; string ratingCaption = "Rating"; } <div id="exportContainer"> @(Html.DevExtreme().Button() .Text("Export multiple grids") .Icon("xlsxfile") .OnClick("exportGrids")) </div> @(Html.DevExtreme().TabPanel() .ID("tabPanel") .Items(items => { items.Add() .Title(priceCaption) .Template(@<text> @(Html.DevExtreme().DataGrid<DataGridProduct>() .ID("priceDataGrid") .DataSource(Model) .DataSourceOptions(dso => dso .Select(new string[] { "Product_ID", "Product_Name", "Product_Sale_Price", "Product_Retail_Price" }) .Filter(new object[] { "Product_ID", "<", 10 }) ) .ShowBorders(true) .RowAlternationEnabled(true) .Width("100%") .Columns(c => { c.AddFor(m => m.Product_ID) .Caption("ID") .Width(50); c.AddFor(m => m.Product_Name) .Caption("Name"); c.AddFor(m => m.Product_Sale_Price) .Caption("Sale Price") .DataType(GridColumnDataType.Number) .Format(Format.Currency); c.AddFor(m => m.Product_Retail_Price) .Caption("Retail Price") .DataType(GridColumnDataType.Number) .Format(Format.Currency); }) ) </text>); items.Add() .Title(ratingCaption) .Template(@<text> @(Html.DevExtreme().DataGrid<DataGridProduct>() .ID("ratingDataGrid") .DataSource(Model) .DataSourceOptions(dso => dso .Select(new string[] { "Product_ID", "Product_Name", "Product_Consumer_Rating", "Product_Category" }) .Filter(new object[] { "Product_ID", "<", 10 }) ) .ShowBorders(true) .RowAlternationEnabled(true) .Width("100%") .Columns(c => { c.AddFor(m => m.Product_ID) .Caption("ID") .Width(50); c.AddFor(m => m.Product_Name) .Caption("Name"); c.AddFor(m => m.Product_Consumer_Rating) .Caption("Rating"); c.AddFor(m => m.Product_Category) .Caption("Category"); }) ) </text>); }) .ItemTitleTemplate(@<text><span class="dx-tab-text"><%- title %></span></text>) .DeferRendering(false) ) <script> function exportGrids() { var dataGrid1 = $("#priceDataGrid").dxDataGrid("instance"); var dataGrid2 = $("#ratingDataGrid").dxDataGrid("instance"); var workbook = new ExcelJS.Workbook(); var priceSheet = workbook.addWorksheet('@priceCaption'); var ratingSheet = workbook.addWorksheet('@ratingCaption'); priceSheet.getRow(2).getCell(2).value = "Price"; priceSheet.getRow(2).getCell(2).font = { bold: true, size: 16, underline: "double" }; ratingSheet.getRow(2).getCell(2).value = "Rating"; ratingSheet.getRow(2).getCell(2).font = { bold: true, size: 16, underline: "double" }; function setAlternatingRowsBackground(gridCell, excelCell) { if (gridCell.rowType === "header" || gridCell.rowType === "data") { if (excelCell.fullAddress.row % 2 === 0) { excelCell.fill = { type: "pattern", pattern: "solid", fgColor: { argb: "D3D3D3" }, bgColor: { argb: "D3D3D3" } }; } } } DevExpress.excelExporter.exportDataGrid({ worksheet: priceSheet, component: dataGrid1, topLeftCell: { row: 4, column: 2 }, customizeCell: function (options) { setAlternatingRowsBackground(options.gridCell, options.excelCell) } }).then(function () { return DevExpress.excelExporter.exportDataGrid({ worksheet: ratingSheet, component: dataGrid2, topLeftCell: { row: 4, column: 2 }, customizeCell: function (options) { setAlternatingRowsBackground(options.gridCell, options.excelCell) } }); }).then(function () { workbook.xlsx.writeBuffer().then(function (buffer) { saveAs(new Blob([buffer], { type: "application/octet-stream" }), "MultipleGrids.xlsx"); }); }); } </script>
using DevExtreme.NETCore.Demos.Models; using DevExtreme.NETCore.Demos.Models.DataGrid; using DevExtreme.NETCore.Demos.Models.SampleData; using Microsoft.AspNetCore.Mvc; using System.Linq; namespace DevExtreme.NETCore.Demos.Controllers { public class DataGridController : Controller { public ActionResult ExcelJSExportMultipleGrids() { return View(SampleData.DataGridProducts); } } }
#priceDataGrid, #ratingDataGrid { padding: 10px; } #exportContainer { text-align: right; } #tabPanel { padding-top: 10px; }