Export data as excel in h5

In case you have a tabular data, and you want to export the data as excel. Your best option in h5 is to export the data as csv file. CSV file can be easily imported into excel and by default is recognized by excel on windows.

Here's the function to export the data as a csv file download.

export function exportTableToExcel(table: string[][], filename: string = 'excel_data'){
  let text = table.map(row => row.map((s = '') => {
    if (s.indexOf(',') != -1) {
      return `"${s.replace(/"/g, '""')}"`;
    }
    return s;
  }).join(',')).join('\r\n');

  let dataType = 'text/csv;charset=utf-8';

  // \ufeff is BOM for utf-8 encoding
  let blob = new Blob(['\ufeff' + text], {
    type: dataType,
  });
  let url = URL.createObjectURL(blob);

  // Create download link element
  let downloadLink = document.createElement("a");
  document.body.appendChild(downloadLink);
  // Create a link to the file
  downloadLink.href = url;

  // Setting the file name
  downloadLink.download = filename + '.csv';

  //triggering the function
  downloadLink.click();
  downloadLink.remove();
  URL.revokeObjectURL(url);
}

call it like this

exportTableToExcel([
  ['Name', 'Email'],
  ['Sam', 'sam@mail.com'],
  ['Tom', 'tom@mail.com'],
]);