Skip to content

Instantly share code, notes, and snippets.

@kemsky
Created October 13, 2018 14:59
Show Gist options
  • Save kemsky/97d993a25e9d0e192df19602387b4256 to your computer and use it in GitHub Desktop.
Save kemsky/97d993a25e9d0e192df19602387b4256 to your computer and use it in GitHub Desktop.
ExcelJS example
<html>
<head>
<title>exceljs sample</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/exceljs/1.6.2/exceljs.js"></script>
<script>
function save(blob, fileName) {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
function create() {
var wb = new ExcelJS.Workbook();
var ws = wb.addWorksheet('blort');
var cell = ws.getCell('A1');
cell.value = '12345\r\n56789';
cell.alignment = {wrapText: true};
cell = ws.getCell('A2');
cell.value = '"000000000"';
cell.alignment = {wrapText: true};
cell = ws.getCell('A3');
cell.alignment = {wrapText: true};
cell.value = '"000000001"';
wb.xlsx.writeBuffer()
.then(function (buffer) {
console.log(buffer);
var blob = new Blob([buffer], {type: 'application/octet-stream'});
console.log(blob);
save(blob, 'excel.xlsx');
});
}
</script>
</head>
<body>
<button onclick="create()">Save</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment