JSFiddle - React, Tailwind, and code Playground
by techsin
HTML
<button>Download Excel CSV</button>
JavaScript
var asUtf16, downloadExcelCsv, makeExcelCsvBlob, rows, toTsv;
asUtf16 = function(str) {
var buffer, bufferView, i, val, _i, _ref;
buffer = new ArrayBuffer(str.length * 2);
bufferView = new Uint16Array(buffer);
bufferView[0] = 0xfeff;
for (i = _i = 0, _ref = str.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
val = str.charCodeAt(i);
bufferView[i + 1] = val;
}
return bufferView;
};
makeExcelCsvBlob = function(rows) {
return new Blob([asUtf16(toTsv(rows)).buffer], {
type: "text/csv;charset=UTF-16"
});
};
toTsv = function(rows) {
var escapeValue;
escapeValue = function(val) {
if (typeof val === 'string') {
return '"' + val.replace(/"/g, '""') + '"';
} else if (val != null) {
return val;
} else {
return '';
}
};
return rows.map(function(row) {
return escapeValue(row);
}).join('\n') + '\n';
};
downloadExcelCsv = function(rows, attachmentFilename) {
var a, blob;
blob = makeExcelCsvBlob(rows);
a = document.createElement('a');
a.style.display = 'none';
a.download = attachmentFilename;
document.body.appendChild(a);
a.href = URL.createObjectURL(blob);
a.click();
URL.revokeObjectURL(a.href);
a.remove();
};
rows = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]",...