We have this in some projects:
`export type Row = (string | number)[];
export type Payload = Row[];
/**
- Handles downloading an array as CSV list.
*/
export default function downloadCsv(payload: Payload, fileName: string, delimiter = ','): void {
const content = \ufeff${payload.map(row => row.join(delimiter)).join('\n')};
const anchorElement = document.createElement('a');
const mimeType = 'text/csv;encoding:utf-8';
anchorElement.href = URL.createObjectURL(new Blob([content], {
type: mimeType,
}));
anchorElement.setAttribute('download', fileName);
document.body.appendChild(anchorElement);
anchorElement.click();
document.body.removeChild(anchorElement);
}
`