First of all generate your CSV content:
var table = document.getElementById(id);
var wb = XLSX.utils.table_to_book(table, { sheet: "Sheet JS" });
var ws1 = wb.Sheets[wb.SheetNames[0]];
var csv = XLSX.utils.sheet_to_csv(ws1, { strip: true });
download_file(csv, 'my_csv.csv', 'text/csv;encoding:utf-8');
And then the download_file function credit to Javascript: set filename to be downloaded:
function download_file(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) { //html5 A[download]
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
}
}