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.

Abhi
  • 614
  • 2
  • 12
  • 26
  • give more details like what is component code and template code which library using for this. – Avinash Dalvi Dec 17 '19 at 05:12
  • please check this answer : https://stackoverflow.com/questions/58926366/is-there-a-way-to-download-a-csv-file-of-the-data-from-a-sql-server-database-tab/58926575#58926575 – Joel Joseph Dec 17 '19 at 05:14
  • Have you checked https://stackoverflow.com/questions/333537/how-to-generate-excel-through-javascript/44690670#answer-44690670 The GitHub repo URL - https://github.com/agershun/alasql – Sujit Kumar Singh Dec 17 '19 at 06:10

2 Answers2

1

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");
}
Abhi
  • 614
  • 2
  • 12
  • 26
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);
Spandan
  • 221
  • 1
  • 4