Documentation

Data export to PDF, Excel

Datatable and Treetable data can be exported to the following formats:

  1. PDF;
  2. Excel.

The components don't require any special configurated to be exported.

For the treetable it is possible to add indentations for tree nodes:

// "value" column will be indented in the exported file
view:"treetable",
columns:[
    { id:"id"},
    { id:"value", exportAsTree:true },
    { id:"chapter"}
]

Related sample:  Export to PDF, Excel

After that all you need is to apply an appropriate export method to this datatable or treetable.

Export to PDF

To export data from datatable or treetable into a PDF document you need to call method exportToPDF. For example, if you add a button by clicking on which the grid will start exporting, then its code will look like this:

<input type="button" value="Get as PDF" style='width:400px;margin:25px;' 
onclick="grid.exportToPDF();">

Related sample:  DataTable Export to PDF

Above solution uses online data export service, but you can configure a local copy as well - you can download and install the Export package. It must be unpacked inside the root of your web server.

The latest packages for installation can be found here:

The code will look like this:

grid.exportToPDF("generate.php"); //provide the correct relative path

Export to Excel

To export data from datatable/tretable into an Excel document you need to call method exportToExcel. For example, if you add a button by clicking on which the grid will start exporting, then its code will look like this:

<input type="button" value="Get as PDF" style='width:400px;margin:25px;' 
onclick="grid.exportToExcel();">

Related sample:  Export to Excel

Above solution uses online data export service, but you can configure a local copy as well - you can download and install the Export package. It must be unpacked inside the root of your web server.

The code will look like this:

grid.exportToExcel("generate.php"); //provide corrent relative path

Customizing Export

Both exportToPDF and exportToExcel return data from all currently visible columns of your datatable without any data formatting (as it is in the dataset). However, you may need to get data from other fields as well as restrict the number of fields in the export file, or apply some format to data values.

All customizing happens within the needed export function:

//in case you use online service
grid.exportToExcell(null, {..setting custom values..});
 
//in case you use local version of export script
grid.exportToExcell("generate.php", {..setting custom values..});

Export API allows

  • setting columns you'd like to see in export (including ID column). Here column names should be specified:
grid.exportToExcel(null, {
    id:true, // note where id export is set
    columns:{ 
        customer:true,
        region_id:true,
        country_id:true
    }
});
  • defining a custom template and width for data from specified column:
grid.exportToPDF(null, {
    id:true, 
    columns:{ 
        customer:{ 
            template:"#customer#",
            width:500
        }
    }
});

The column will be rendered with the stated template and width, which may differ from datatable parameters.

  • including extra field into export by forming them right within the export function:
grid.exportToPDF(null, { 
    columns:{ 
        custom1:{
            template:function(obj){
                return "Year " + obj.year;
            },
            width:50,
            header:[{ text:"Date" }]
        },
        //other columns
        title:true
    }
});

"Custom1" (any name can be used) receives data from year field even if it is not seen in this datatable but is present in its dataset. The field is rendered with width, template and header that will be the header of the corresponding column in the export table.

Related sample:  DataTable - Export Configuration

Configuration

You can define how datatable will look in PDF or Excel document after it is exported, configuring available options in the server side code. For these needs, please download the local script version.

In addition, you gain extra security with local scripts as well as get rid of a text that shows download source.

Back to top