How To generate an Excel File
Use the ExcelFile and CsvFile class. an ExcelFile instance represents an entire Excel file and a CsvFile instance represents a sheet of the Excel File..
- First create an instance of the ExcelFile class
- Create an instance of the CsvFile class for each sheet you want to add
- Use the writeLine(array) method of the CsvFile to write a line in the sheet. The parameter is an array of string
- Use the addSheet(name, buffer) method of the ExcelFile to add a sheet to the Excel File. name is the name of the sheet and buffer is the content of the sheet, you can obtain using the getContent() method of the CsvFile
- Finally, use the download(filename) methd on the ExcelFile to download the Excel File
// 1. Create blank Excel File var excel = new ExcelFile(); // 2. Create the first sheet using the CsvFile class var sheet1 = new CsvFile(); // 3. Add lines to the sheet sheet1.writeLine(["id", "name", "email"]); sheet1.writeLine([12", "John", "john@gmail.com]); // 4. Create a second sheet and add some lines var sheet2 = new CsvFile(); sheet2.writeLine(["id", "note", "date"]); sheet2.writeLine(["2", "This is a note", Format.date(Date.now())]); // 5. Add the 2 sheets to the excel file excel.addSheet(sheet1.getContent()); excel.addSheet(sheet2.getContent()); // 6. Download the excel file excel.download("export.xlsx");