Generate Excel document

Hi :grinning:

I have previously generated PDF files using the “Generate Document” action node, but now I would like to generate an Excel-file that contains data from my app. I can only choose PDF as the document type in this action node, and I am wondering if it is possible to somehow make an Excel-file instead? If not, are there any other ways to generate an Excel-file from Appfarm? :slight_smile:

Thanks,
Molly

Hi @Molly

There are several ways to do this:

  1. Use the “Export data” action node and you will get a CSV. You can find all the information in the documentation and there’s also an example in the showroom.

  2. Use the built-in “Download as CSV” from a table. You’ll also find an example for this in the showroom.

  3. Use an external library and export the Excel file with “Run code”. The advantage here is that you can build the file with different data sources and structure it the way you want. First, you need to add the excelJS as a Custom Header Tag under Environments.


    (Script URL: https://cdnjs.cloudflare.com/ajax/libs/exceljs/4.4.0/exceljs.min.js). Then you can create your file by using “Run code”. Here is an example of the function in “Run code”. Projects is the datasource and name, projectlead, startdate, state, costs, comment are properties of this datasource.

const workbook = new window.ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Worksheetname');

// Write some rows
worksheet.addRow(['Project:', projects.name]);
worksheet.addRow(['Project Lead:', projects.projectlead]);

// Empty rows
worksheet.addRow([]);
worksheet.addRow([]);

// Write column headers
worksheet.addRow(['Startdate', 'State', 'Costs', 'Comment']);

// Write columns
projects.forEach((line) => {
  worksheet.addRow([line.startdate, line.state, line.costs, line.comment]);
});

// Download the file
workbook.xlsx.writeBuffer()
  .then(function(buffer) {
      const blob = new Blob([buffer], { type: 'application/vnd.ms-excel' });
      const blobURL = URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = blobURL;
      link.download =  'filename.xlsx';
      link.click();
      resolve()
  })
  .catch(function() {
      reject()
  })

Hope this helps :slight_smile: