JavaScript/jQuery 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+ and FileSaver v2.0.2+.HTML- <head>
- <!-- ... -->
- <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.4.0/polyfill.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/exceljs/4.1.1/exceljs.min.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.2/FileSaver.min.js"></script>
- <!-- reference the DevExtreme sources here -->
- </head>
Enable the export UI
Set the export.enabled property to true:JavaScript- $(function () {
- $("#pivotGridContainer").dxPivotGrid({
- export: {
- enabled: true
- }
- });
- });
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 ExcelExportPivotGridProps 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. Thee.cancel
parameter disables the deprecated built-in export implementation with fewer capabilities.index.js- $('#gridContainer').dxPivotGrid({
- export: {
- enabled: true
- },
- onExporting: function(e) {
- var workbook = new ExcelJS.Workbook();
- var worksheet = workbook.addWorksheet('Main sheet');
- DevExpress.excelExporter.exportPivotGrid({
- worksheet: worksheet,
- component: e.component,
- customizeCell: function(options) {
- var 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');
- });
- });
- e.cancel = true;
- }
- });
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.
fileName
Since v20.2, we recommend ExcelJS-based export which does not use this property.
ignoreExcelErrors
Since v20.2, we recommend ExcelJS-based export which does not use this property.
proxyUrl
Specifies the URL of the server-side proxy that streams the resulting file to the end user to enable exporting in the Safari browser.
Generally, exporting is performed using client-side API in browsers. However, the Safari (integrated in Mac OS) browser does not implement an API for saving files. In this instance, the PivotGrid UI component can POST the content to a server-side proxy, which will stream the file back to the end user. To enable this functionality, set the export.proxyUrl property to the proxy, which will stream the file to the end user. When implementing the proxy, take the following information into account.
- Your proxy will receive a POST request with the following parameters in the request body: fileName, contentType (the MIME type of the file) and base64 (the base-64 encoded file content).
- The proxy should return the decoded file with the "Content-Disposition" header set to attachment; filename="
" .
If you have technical questions, please create a support ticket in the DevExpress Support Center.