'How Can I export two p-table (PrimeNG) in the same .csv file?

I have two primeNG tables and I would like to export only one csv file with the two tables information.

When I only have one table, I can use the exportCSV() method, but I dont know how to do it with two tables.

Is there any way to obtain the data and create the csv file manually? Do you know another solution?

Thank you so much.



Solution 1:[1]

Step: 1 Create two separate sheets from two tables data

  const productsWorksheet = xlsx.utils.json_to_sheet(this.products);
  const carsWorksheet = xlsx.utils.json_to_sheet(this.cars);

Step: 2 Create an empty workbook

const workbook = xlsx.utils.book_new();

Step: 3 Append the sheets to the workbook

xlsx.utils.book_append_sheet(workbook, productsWorksheet, 'Products');
xlsx.utils.book_append_sheet(workbook, carsWorksheet, 'Cars');

Step: 4 Create the file

const excelBuffer: any = xlsx.write(workbook, {
    bookType: 'xlsx',
    type: 'array',
  });

Step: 5 Save the csv file with file-saver

this.saveAsExcelFile(excelBuffer, 'tables');

saveAsExcelFile(buffer: any, fileName: string): void {
    import('file-saver').then((FileSaver) => {
      let EXCEL_TYPE =
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
      let EXCEL_EXTENSION = '.xlsx';
      const data: Blob = new Blob([buffer], {
        type: EXCEL_TYPE,
      });
      FileSaver.saveAs(
        data,
        fileName + '_export_' + new Date().getTime() + EXCEL_EXTENSION
      );
    });
  }

Working Stackblitz

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yoelb00