Export PrimeNG DataTable to Excel, PDF and XML

PrimeNG DataTable has the function of exporting data to a CSV file. I need to provide functionality for exporting data to Excel, PDF and XML. How can I achieve this in my Angular2 application?

+5
source share
1 answer

PrimeNg does not support anything more than datatable for CSV initially. Therefore, you need to create this functionality from scratch.

A good approach would be:

  • You already have data in the object in your compoment. This will be the object you pass in your datatable as follows:

    <p-dataTable [value]="cars">

    therefore, our data is located at the car facility. In your compoment you can access it using

    this.cars

  • The next step is to add either your datatable header, or any other part that matches your design, a button that will read something in the "download pdf" lines. Something like that:

    <p-button label="Click to download PDF" styleClass="ui-button-info" (click)="download()"></p-button>

    this will cause the method to load in your component when clicked

  • Finally, during the download process, you need to convert your data to PDF or any other format that you need. This can be easily done using one of the many libraries on the Internet, for example for PDF: jsPDF. A working example with angular can be found here: plunker jsPDF angular4 example . Thus, in our code there is a download function:

     let doc = new jsPDF(); let col = ["Details", "Values"]; let rows = []; for(let key in this.cars){ let temp = [key, item[key]]; rows.push(temp); } doc.autoTable(col, rows); doc.save('Test.pdf'); 
0
source

Source: https://habr.com/ru/post/1261749/


All Articles