0

I have an array that looks like this:

const data = [
  {id:28797,name:"משה"},
  {id:23423,name:"אהרן"},
  {id:435,name:"יוסף"},
  {id:6768,name:"לוי"}
]

I have already been able to export to CSV and to JSON I would love to understand how I can export to excel.

export to CSV:

const csv = data.map((item) => {
      return item.join(",");
    });
downloadBlob(csv.join("\n"), name + ".csv", "text/csv");

export to JSON:

downloadBlob([JSON.stringify(data)], name + ".json", "application/json");

downloadBlob function:

const downloadBlob = (content, filename, contentType) => {
    // Create a blob
    const blob = new Blob(["\uFEFF" + content], {
      type: contentType + "; charset=utf-8;",
    });
    const url = URL.createObjectURL(blob);
    // Create a link to download it
    const a = document.createElement("a");
    a.href = url;
    a.setAttribute("download", filename);
    a.click();
};
j08691
  • 204,283
  • 31
  • 260
  • 272
  • Either (a) [Learn the Excel data format](https://stackoverflow.com/questions/4886027/looking-for-a-clear-description-of-excels-xlsx-xml-format) and write a library that generates data in it in JS or (b) Find a library that someone else wrote that does that already. – Quentin May 04 '22 at 15:11
  • https://stackoverflow.com/questions/32008593/export-array-of-objects-into-excel-using-javascript this will be helpful – poo May 04 '22 at 15:11

0 Answers0