0

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.

3

2 Answers 2

2

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");
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.