React PivotGrid - export
A user can click the Export button to save an Excel file with the exported data. Data types, sort, filter, and group settings are maintained.
The following instructions show how to enable and configure client-side export:
Install or reference the required libraries
This feature requires ExcelJS v4.4.0+ and FileSaver v2.0.2+. If you apply CSP rules, refer to the ExcelJS CSP Treats section to read more about potential vulnerabilities.- npm install --save exceljs file-saver
Enable the export UI
Set the export.enabled property to true:App.js- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import PivotGrid, {
- Export,
- Column
- } from 'devextreme-react/pivot-grid';
- export default function App() {
- return (
- <PivotGrid ... >
- <Export enabled={true} />
- </PivotGrid>
- );
- }
Export the PivotGrid
Implement the onExporting handler and call the exportPivotGrid(options) method in it. In the code below, this method exports the PivotGrid as is, but you can use PivotGridExportOptions to configure export settings, including cell customization. The PivotGrid is exported to an Excel worksheet that is created using the ExcelJS API. To save the Excel document, call the FileSaver's saveAs method.App.js- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import { Workbook } from 'exceljs';
- import saveAs from 'file-saver';
- import PivotGrid, { Export } from 'devextreme-react/pivot-grid';
- import { exportPivotGrid } from 'devextreme/excel_exporter';
- export default function App() {
- return (
- <PivotGrid ...
- onExporting={onExporting}>
- <Export enabled={true} />
- </PivotGrid>
- );
- }
- function onExporting(e) {
- const workbook = new Workbook();
- const worksheet = workbook.addWorksheet('Main sheet');
- exportPivotGrid({
- component: e.component,
- worksheet: worksheet,
- customizeCell: function(options) {
- const excelCell = options;
- excelCell.font = { name: 'Arial', size: 12 };
- excelCell.alignment = { horizontal: 'left' };
- }
- }).then(function() {
- workbook.xlsx.writeBuffer()
- .then(function(buffer) {
- saveAs(new Blob([buffer], { type: 'application/octet-stream' }), 'PivotGrid.xlsx');
- });
- });
- }
The following restrictions apply when users export PivotGrid:
- Only XLSX files are supported out of the box. To export PivotGrid to CSV, call the exportPivotGrid(options) method as shown in the following ticket: Export PivotGrid into CSV file.
- Only visible rows and columns are exported.
If you have technical questions, please create a support ticket in the DevExpress Support Center.