HTML Export in JavaScript Pivot Table

The JavaScript Pivot Table allows exporting the rendered table view into a standalone HTML file. To enable HTML export, set the allowHtmlExport property to true. The HtmlExport module is included in the Syncfusion script bundle used by the sample. Once enabled, call the exportToHTML method programmatically, or use the HTML option in the built-in Toolbar export menu.

In the following example, an external button is used to start the HTML export. When the button is clicked, the exportToHTML method is called with HtmlExportProperties so that the Pivot Table is saved as an HTML file.

var pivotTableObj = new ej.pivotview.PivotView({
    dataSourceSettings: {
        dataSource: pivotData,
        expandAll: false,
        columns: [{ name: 'Year', caption: 'Production Year' }, { name: 'Quarter' }],
        values: [{ name: 'Sold', caption: 'Units Sold' }, { name: 'Amount', caption: 'Sold Amount' }],
        rows: [{ name: 'Country' }, { name: 'Products' }],
        formatSettings: [{ name: 'Amount', format: 'C0' }],
        filters: []
    },
     height: 320,
    allowHtmlExport: true
});
pivotTableObj.appendTo('#PivotTable');

var exportBtn = new ej.buttons.Button({ isPrimary: true });
exportBtn.appendTo('#pdf');

document.getElementById('pdf').onclick = function () {
    var htmlExportProperties = { fileName: 'PivotTable.html', title: 'Sales Analysis Report', includeStyles: true };
    pivotTableObj.exportToHTML(htmlExportProperties);
};
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Pivot Grid</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Pivot Grid Control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/tailwind3.css" rel="stylesheet" />

    <script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
    <script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>

    <div id="container">
        <input id="pdf" type="button" value="HTML Export" name="HTML Export">
        <div id="PivotTable"></div>
        <br>
        <div id="PivotTable2"></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>

exportToHTML method

The exportToHTML(properties, isBlob) method returns a Promise that resolves to the exported HTML content. It accepts the following parameters:

Parameter Type Description
properties HtmlExportProperties Optional. An object that customizes the export.
isBlob boolean Optional. When set to true, the method returns a Promise containing the Blob or HTML string instead of triggering a direct browser download.

The HtmlExportProperties object supports the following options:

Property Type Default Description
fileName string - Specifies the file name of the downloaded HTML file, for example 'PivotTable.html'.
title string - Sets the <title> tag of the exported HTML document.
includeStyles boolean true Indicates whether the inline CSS styles of the Pivot Table are included in the exported file.