We have updated Flexmonster Software License Agreement, effective as of September 30, 2025 (list of changes)
All documentation

exportTo

exportTo(type: String, params: ParamsObject, callbackHandler: Function)

Exports the current view to Excel, PDF, CSV, HTML, or image format. The file can be saved to the local file system or to your server (you need to have a script on the server side).

To learn more about export, see this guide: Export the report.

Parameters

Parameter/TypeDescription
type
String
A type of export.
The following types are available: "excel", "pdf", "csv", "html", "image".
params
ParamsObject
optional Contains export parameters.
callbackHandler
Function
optional A JS function that is called when the data is ready to be exported. The following data is passed to the callbackHandler:
  • resultResultObject. Describes the result of the export. If the export fails, the result will be null.
  • errorErrorObject. Describes the error with export. If the export is successful, the error will be null. Check out how to handle errors when exporting to a server — Live example.

ParamsObject

Property/TypeDescription
alwaysEnclose
Boolean
optional Indicates whether to enclose all CSV fields in quotes.
When set to true, the fields are always enclosed in quotes. Otherwise, they will be enclosed only when necessary (e.g., if a field contains a comma: Bike, "$15,000", blue).
Only for CSV export.
Default value: false.
filename
String
optional A default name of the resulting file.
destinationType
String
optional It defines where the component's contents will be exported. The destinationType can have the following values:
  • "file" — contents are exported as a file to a user's device.
  • "server" — contents are exported as a file to the server-side script specified in the url property. Flexmonster creates an XMLHttpRequest with the exported file and sends it as a POST request to the server-side script.
  • "plain" — contents are passed as an object to the callbackHandler in the result.data property. Note that the contents must be saved manually. Depending on the export type, result.data will have the following type:
    • String — for HTML or CSV export.
    • Uint8Array — for Excel export.
    • HTMLCanvasElement — for image export.
    • jsPDF object — for PDF export.
      You can modify the contents using jsPDF API before saving it.
    Use the "plain" destination type to have full control over how the contents are exported Live example.
Default value: "file".
excelSheetName
String
optional To configure the sheet name when exporting to an Excel file.
fieldSeparator
String
optional Defines specific fields separator to split CSV row in the export file.
Only for CSV export.
Default value: ,.
fontUrl
String
optional The URL to the TTF font file for saving PDF reports in Chinese, Arabic, or any other language. Check out the list of ready-to-use Google Noto Fonts that you can use to support almost any language in the world. Only fonts in standard TTF format are supported.
Live example
header
String | HTML String
optional String that will be displayed in the header section of the exported file. The header's type and how it works depend on the export type:
  • For the "excel" export type — String. You can also add a multirow header using the "\n" character: "Row1\nRow2\nRow3".
    A cell with the header spans multiple columns in the exported Excel file, with a maximum of 10 columns. This ensures that the header is always visible to a user.
  • For the "pdf" export type — String | HTML string. You can set the header as a regular string or as an HTML string with tags, inline styles, and Base64 images. The string is rendered in the browser and added as an image to the exported file.
    You can also use the ##CURRENT-DATE## and ##PAGE-NUMBER## tokens in the string.
  • For the "csv" export type — String. You can also add a multirow header using the "\n" character: "Row1\nRow2\nRow3".
  • For the "html" export type — String | HTML string. You can set the header as a regular string or as an HTML string with tags, inline styles, and Base64 images.
    You can also use the ##CURRENT-DATE## token in the string.
  • For the "image" export type — String | HTML string. You can set the header as a regular string or as an HTML string with tags, inline styles, and Base64 images. The string is rendered in the browser and added as an image to the exported file.
    You can also use the ##CURRENT-DATE## token in the string.
Live example
footer
String | HTML String
optional String that will be displayed in the footer section of the exported file. The footer's type and how it works depend on the export type:
  • For the "excel" export type — String. You can also add a multirow footer using the "\n" character: "Row1\nRow2\nRow3".
    A cell with the footer spans multiple columns in the exported Excel file, with a maximum of 10 columns. This ensures that the footer is always visible to a user.
  • For the "pdf" export type — String | HTML string. You can set the footer as a regular string or as an HTML string with tags, inline styles, and Base64 images. The string is rendered in the browser and added as an image to the exported file.
    You can also use the ##CURRENT-DATE## and ##PAGE-NUMBER## tokens in the string.
  • For the "csv" export type — String. You can also add a multirow footer using the "\n" character: "Row1\nRow2\nRow3".
  • For the "html" export type — String | HTML string. You can set the footer as a regular string or as an HTML string with tags, inline styles, and Base64 images.
    You can also use the ##CURRENT-DATE## token in the string.
  • For the "image" export type — String | HTML string. You can set the footer as a regular string or as an HTML string with tags, inline styles, and Base64 images. The string is rendered in the browser and added as an image to the exported file.
    You can also use the ##CURRENT-DATE## token in the string.
Live example
pageFormat
String
optional It defines the page format for a PDF file. There are such types available: "A0", "A1", "A2", "A3", "A4", "A5".
Default value: "A4".
pageOrientation
String
optional It defines the page orientation for a PDF file. Page orientation can be the following:
  • "portrait" (by default) - defines portrait page orientation for a PDF file.
  • "landscape" - defines landscape page orientation for a PDF file.
requestHeaders
Object
optional It allows adding custom request headers when the destinationType property is "server". The object consists of "key": "value" pairs, where "key" is a header name and "value" is its value.
showFilters
Boolean
optional Indicates whether the exported file should list all filtered fields (true) or filtered fields only from report filters (false).
If there are no fields in report filters, this property is ignored.
Only for Excel export.
Default value: false.
url
String
optional A path to a server-side script that can save the file. The url property is required when the destinationType property is "server".
useOlapFormattingInExcel
Boolean
optional To configure how to export grid cells in an Excel file if formatting is taken from OLAP cube – as a formatted string (true) or as numbers without formatting (false).
Only for Excel export.
useCustomizeCellForData
Boolean
optional Indicates whether cells modified by customizeCell() are exported as formatted strings (true) or as numbers without formatting (false). Learn more about how the modified cells are displayed in the exported file.
Not available for the "image" export type.
Default value: true.

ResultObject

Property/TypeDescription
data
String | Uint8Array
The data to be exported. The data’s type depends on the type of the export: 
  • String — for CSV or HTML export.
  • Uint8Array — for Excel, image, or PDF export.
Note that the types are different when the destinationType is set to "plain"learn more here.
filename
String
The name of the resulting file.
response
String
optional The server’s response. This property is defined only when the destinationType is set to "server".
type
String
The export type specified in the type parameter.

ErrorObject

Property/TypeDescription
message
String
The error message.
response
String
optional The server’s response. This property is defined only when the destinationType is set to "server".
status
Number
optional The response status code. This property is defined only when the destinationType is set to "server".

Examples

1) This example on JSFiddle demonstrates all types of export: CSV, HTML, PDF, Image and Excel.

2) Export to PDF, modify generated file and save locally:

pivot.exportTo("pdf", { destinationType: "plain" }, function(res) {
let pdf = res.data;
pdf.addPage();
pdf.text('Hello world!', 10, 10);
pdf.save(res.filename);
});

3) Export to CSV, save as a local file and add a callback handler:

pivot.exportTo('csv', {filename : 'flexmonster.csv'}, 
function(result) { console.log(result.data); }
);

4) Export to HTML and save as local file:

const params = { 
  filename : 'flexmonster.html'
};
pivot.exportTo('html', params);

5) Export to PDF file, change page orientation to landscape and save file to the server:

const params = { 
  filename : 'flexmonster.pdf',
 pageOrientation : 'landscape',
  destinationType : 'server',
  url : 'your server'
};
pivot.exportTo('pdf', params);

6) Export to Excel and save as local file:

pivot.exportTo('excel');

7) Export to PDF and set TTF font file:

pivot.exportTo('pdf', { 
fontUrl: 'https://cdn.flexmonster.com/fonts/NotoSansCJKtc-Regular.ttf'
});

Live example

8) Export reports from multiple Flexmonster instances into one file.

Each report is exported separately and then merged with others into one file. The exact approach is different depending on the export type Live example.

See also

Export tutorial
exportcomplete
exportstart