HTML Export in TypeScript Pivot Table
The TypeScript Pivot Table allows exporting the rendered table view into a standalone HTML file. To enable HTML export, set the allowHtmlExport property to true and inject the HtmlExport module into the Pivot Table. 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.
import { PivotView, IDataSet, HtmlExport, HtmlExportProperties } from '@syncfusion/ej2-pivotview';
import { Button } from '@syncfusion/ej2-buttons';
import { pivotData } from './datasource.ts';
PivotView.Inject(HtmlExport);
let pivotTableObj: PivotView = new PivotView({
dataSourceSettings: {
dataSource: pivotData as IDataSet[],
expandAll: false,
drilledMembers: [{ name: 'Country', items: ['France'] }],
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: []
},
allowHtmlExport: true,
height: 320
});
pivotTableObj.appendTo('#PivotTable');
let exportBtn: Button = new Button({ isPrimary: true });
exportBtn.appendTo('#pdf');
document.getElementById('pdf').onclick = function () {
let htmlExportProperties: 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://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<input id="pdf" type="button" value="HTML Export" name="HTML Export" />
<div id='PivotTable'></div>
<br />
<div id='PivotTable2'></div>
</div>
</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. |