3

Suppose I have multiple array of object as,

const books = [{"book":"harry","part":1},{"book":"harry","part":2},{"book":"harry","part":3}, 
               {"book":"lotr","part":1},{"book":"lotr","part":2}]

const personDetails = [{"name":"ram","age":21},{"name":"jack","age":22},{"name":"ryan","age":45}];

I can download for one array of object, by method as,

import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';

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

But how can I export multiple array of object in different sheets in excel. If anyone needs any further information please let me know.

2
  • Can you please provide information regarding which libraries you are using? Like FileSaer.js & any else? Commented Apr 19, 2021 at 5:03
  • I have added libraries. Could you please have a look now.:) Commented Apr 19, 2021 at 5:05

1 Answer 1

3

Try creating workbook in below manner using var wb = XLSX.utils.book_new();.

Create worksheets with XLSX.utils.json_to_sheet & append both worksheeet objects to workbook with XLSX.utils.book_append_sheet.

Reference : https://www.npmjs.com/package/xlsx#working-with-the-workbook

import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';

function downloadExcel() {
    
    /* create a new blank workbook */
    var wb = XLSX.utils.book_new();

    /* create a worksheet for books */
    var wsBooks = XLSX.utils.json_to_sheet(books);

    /* Add the worksheet to the workbook */
    XLSX.utils.book_append_sheet(wb, wsBooks, "Books");

    /* create a worksheet for person details */
    var wsPersonDetails = XLSX.utils.json_to_sheet(personDetails);

    /* Add the worksheet to the workbook */
    XLSX.utils.book_append_sheet(wb, wsPersonDetails, "PersonDetails");
    
    
    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");    
}
Sign up to request clarification or add additional context in comments.

4 Comments

I had one more query related to this question. Can I ask over here only or should I create new question?
Question is: I want to add a text for each sheet say in sheet1 contains text:" Sheet1" and sheet2 contains text :"sheet2" at top and after that there is table.
I briefly reviewed documentation from reference but do not found any function which could do so. You can try finding some workaround if possible.
No Problem. Thank you so much for your time and input. If I am able to find something I will post here :)

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.