Write heading in excel using xlsx

Viewed 47

Suppose I have an array of Object which I want to export as excel.

const bookDetails = [{"bookName": "HarryPottar","Part":1},{"bookName": "HarryPottar","Part":2}, 
                     {"bookName": "HarryPottar","Part":3}]

To export as excel I use xlsx and FileSaver package, with steps as:

function downloadExcel() {

    let wb = XLSX.utils.book_new();
    let wsBookDetails = XLSX.utils.json_to_sheet(bookDetails);
    XLSX.utils.book_append_sheet(wb, wsBookDetails, "BookDetails");

    const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
    const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
    const data1 = new Blob([excelBuffer], { type: fileType });
    FileSaver.saveAs(data1, "BookDetail Summary.xlsx");
}

On call of this function I'm able to export data of bookDetails, but I want to write heading inside excel as, shown in image,

enter image description here

Is it possible to write header? I tried looking for different solutions, if anyone has got any idea please do let me know.

If header is not possible, can we have table tile. Is it possible to pass table title while exporting?

0 Answers
Related