Reputation: 684
I want to export an excel file from my angular project. Headers of excel should be taken from a string array which looks like:
excelHeaders:string[] = ["Name","Age","Email","Contact Number","Location"];
Excel file will have only headers without any other data.
Please help.
Upvotes: 0
Views: 14378
Reputation: 684
Well, this xlsx documentation helped me to solve the question. XLSX
import * as XLSX from 'xlsx';
...
excelHeaders:string[] = ["Name","Age","Email","Contact Number","Location"];
templateToExcel:string[][] = [this.excelHeaders,[]];
...
exportTemplateAsExcel()
{
const ws: XLSX.WorkSheet=XLSX.utils.aoa_to_sheet(this.templateToExcel);
const wb: XLSX.WorkBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
XLSX.writeFile(wb,"test"+".xlsx");
}
Upvotes: 2
Reputation: 259
You can use any of the client side excel generation libs via npm like - XLSX, XLSX-Style, ExcelJS.
Each of them has well defined set of methods to generate the workbook and inside the workbook creating the rows, header-rows etc. For example while using excel-js -
excelHeaders:string[] = ["Name","Age","Email","Contact Number","Location"];
let workbook = new Workbook();
let worksheet = workbook.addWorksheet('Test Sheet');
let headerRow = worksheet.addRow(excelHeaders);
Upvotes: 0